问题描述
- 图片上传返回JSON数据,前台弹出下载保存文件框, 文件里面是JSON数据,页面上没显示图片怎么解决 10C
- @RequestMapping(value = ""kinduploadfile"" method = RequestMethod.POST)
public @ResponseBody
JSONObject KindUploadFile(HttpServletRequest requestHttpServletResponse 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;}
解决方案
http://www.zhangxinxu.com/wordpress/2011/09/%E5%9F%BA%E4%BA%8Ehtml5%E7%9A%84%E5%8F%AF%E9%A2%84%E8%A7%88%E5%A4%9A%E5%9B%BE%E7%89%87ajax%E4%B8%8A%E4%BC%A0/
http://www.wzsky.net/html/article/php/php2/125287.html
解决方案二:
如何解决“文件上传返回JSON数据,在IE下提示下载文件”的问题?
时间: 2024-08-12 12:59:43