码迷,mamicode.com
首页 > 编程语言 > 详细

springboot+thymleaf处理多文件上传

时间:2018-07-13 13:17:37      阅读:533      评论:0      收藏:0      [点我收藏+]

标签:more   .com   mit   filename   index   sem   try   buffer   pos   

1thymleaf页面表单

<form method="POST" enctype="multipart/form-data" action="/moreupload">
<p>文件1:<input type="file" name="file" /></p>
<p>文件2:<input type="file" name="file" /></p>
<p>文件3:<input type="file" name="file" /></p>
<p><input type="submit" value="上传" /></p>
</form>

2写一个controller跳转到页面

@RequestMapping(value = "/moresave")

public String moresave(){
return "index2";
}

3写一个controller处理多文件上传

@RequestMapping(value="/moreupload", method= RequestMethod.POST)
@ResponseBody
public String handleFileUpload(HttpServletRequest request){
List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("file");
MultipartFile file = null;
BufferedOutputStream stream = null;
for (int i =0; i< files.size(); ++i) {
file = files.get(i);
if (!file.isEmpty()) {
try {

String contentType = file.getContentType();
String fileName = file.getOriginalFilename();
System.out.println("fileName-->" + fileName);
System.out.println("getContentType-->" + contentType);
String filePath = request.getSession().getServletContext().getRealPath("imgupload/");
System.out.println("filePath -->"+filePath );
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(filePath + fileName)));
System.out.println("bufferedOutputStream --->>>"+bufferedOutputStream );
bufferedOutputStream.write(file.getBytes());
bufferedOutputStream.flush();
bufferedOutputStream.close();

} catch (Exception e) {
return "You failed to upload " + i + " => " + e.getMessage();
}
} else {
return "You failed to upload " + i + " because the file was empty.";
}
}
return "上传成功";
}

4展示
技术分享图片


技术分享图片

 

技术分享图片

 

 

 

springboot+thymleaf处理多文件上传

标签:more   .com   mit   filename   index   sem   try   buffer   pos   

原文地址:https://www.cnblogs.com/lr12339/p/9304155.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!