标签: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展示
标签:more .com mit filename index sem try buffer pos
原文地址:https://www.cnblogs.com/lr12339/p/9304155.html