1thymleaf頁面表單app
<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跳轉到頁面spa
@RequestMapping(value = "/moresave")
public String moresave(){
return "index2";
}
3寫一個controller處理多文件上傳orm
@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展現