当前位置: 代码迷 >> J2EE >> 图片上传返回JSON数据,前台页面不显示图片,提示下载框打开是返回的JSON字符串请教如何解决
  详细解决方案

图片上传返回JSON数据,前台页面不显示图片,提示下载框打开是返回的JSON字符串请教如何解决

热度:78   发布时间:2016-04-17 22:58:07.0
图片上传返回JSON数据,前台页面不显示图片,提示下载框打开是返回的JSON字符串请问怎么解决
@RequestMapping(value = "kinduploadfile", method = RequestMethod.POST)
public @ResponseBody
JSONObject KindUploadFile(HttpServletRequest request,HttpServletResponse response)
throws FileUploadException {
response.setContentType("text/html;charset=UTF-8");
JSONObject obj = new JSONObject();
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = (cal.get(Calendar.MONTH)) + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
Map map = new HashMap();
String rootPath = "/upload/";
rootPath += year + "/" + month + "/" + day + "/";
String realRootPath = request.getServletContext().getRealPath(rootPath);
java.io.File f = new File(realRootPath);
if (f.exists() == false) {
f.mkdirs();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest
.getFile("imgFile");// 这里是表单的名字,在swfupload.js中this.ensureDefault("file_post_name",
// "filedata");
try {

InputStream stream = file.getInputStream();
String oldfileName = file.getOriginalFilename();
String type = oldfileName.split("\\.")[1];// 获取文件类型
oldfileName = new String(oldfileName.getBytes(), "utf-8");
String filename = rootPath + sdf.format(new Date())
+ (int) ((Math.random() + 1) * 100000) + "." + type;
OutputStream bos = new FileOutputStream(request.getServletContext()
.getRealPath(filename));
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}

bos.close();
// close the stream
stream.close();

obj.put("url", filename);
obj.put("error",0);

} catch (Exception e) {

obj.put("error", "1");
obj.put("message", e.getMessage());
}
// 上传操作

return obj;

}
------解决思路----------------------
有的浏览器接到json格式的http请求的行为就是下载,想把它显示在页面上可以用Ajax提交
------解决思路----------------------
返回的json数据里面包含图片地址么? 如果包含,直接拿出来展示
------解决思路----------------------
这是下载?
响应头呢?响应体呢?
------解决思路----------------------
你可以尝试不返回json数据,只进行下载。当你同时返回多种东西时,就无法辨认了
------解决思路----------------------
引用:
Quote: 引用:

返回的json数据里面包含图片地址么? 如果包含,直接拿出来展示
  JSON数据就是返回的图片路径,我在其他浏览器上都可以上传显示的,在IE上就不行了


会不会是浏览器模式的问题? 你的ie版本是多少
  相关解决方案