xheditor-文件上传-Spring3 MVC-支持html5-application/octet-stream

xheditor1.1.5 版本文件上传使用html5-application/octet-stream
Spring3 MVC之前写的上传程序无法支持

@RequestMapping(value = "/upload")
public String uploadFile(Model m, MultipartHttpServletRequest request) throws IOException {
String path = UploadController.class.getResource("/").getPath().split("WEB-INF")[0] + "upload/";
Iterator<String> iterator = request.getFileNames();
while (iterator.hasNext()) {
String next = iterator.next();
List<MultipartFile> files = request.getFiles(next);
for (int i = 0; i < files.size(); i++) {
if (!files.get(i).isEmpty()) {
byte[] bytes = files.get(i).getBytes();
String uploadFile = UploadUtils.generateFilename(path, UploadUtils.getExtension(files.get(i).getOriginalFilename(), "jpg"));
File file = new File(uploadFile);
FileOutputStream fos = new FileOutputStream(file);
fos.write(bytes);
fos.close();
m.addAttribute("url", uploadFile.replace(path, "/upload"));
m.addAttribute("local", uploadFile);
}
}
}
return "upload";
}


需要改造如下:

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String upload(Model m, HttpServletRequest request) throws IOException {
String path = UploadController.class.getResource("/").getPath().split("WEB-INF")[0] + "upload/";
if ("application/octet-stream".equals(request.getContentType())) { //HTML 5 上传
try {
String dispoString = request.getHeader("Content-Disposition");
int iFindStart = dispoString.indexOf("filename =\"") + 10;
int iFindEnd = dispoString.indexOf("\"", iFindStart);
String sFileName = dispoString.substring(iFindStart, iFindEnd);
int i = request.getContentLength();
byte buffer[] = new byte[i];
int j = 0;
while (j < i) { //获取表单的上传文件
int k = request.getInputStream().read(buffer, j, i - j);
j += k;
}
if (buffer.length >= 0) { //文件是否为空
String uploadFile = UploadUtils.generateFilename(path, UploadUtils.getExtension(sFileName, "jpg"));
File file = new File(uploadFile);
OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true));
out.write(buffer);
out.close();
m.addAttribute("url", uploadFile.replace(path, "/upload"));
m.addAttribute("local", uploadFile);
}
} catch (Exception ex) {
m.addAttribute("err", ex.getMessage());
}
}
return "upload";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值