谁有springmvc multipartfile html5 多文件上传的示例代码,急!急!急! 50
2个回答
2017-06-07
展开全部
<form id="fileUpload" action="<%=basePath%>template/fileUpload.do" enctype="multipart/form-data" method="post" class="form-horizontal">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel" style="color: #15428b;">上传</h4>
</div>
<div class="modal-body">
<input type="file" name="targetFile">
<input type="file" name="targetFile">
<input type="file" name="targetFile">
</div>
<div class="modal-footer">
<button id="cancel" type="button" class="btn btn-default" data-dismiss="modal">取消
</button>
<button type="button" class="btn btn-primary" onclick="uploadFile();" >确认</button>
</div>
</form>
@RequestMapping(value = "/fileUpload.do")
public String fileUpload(Model model, @RequestParam MultipartFile[] targetFile, HttpServletRequest request, PageSplit<BankVo> pageSplit){
try {
FileOperationsUtil.uploadFileMVC(request, targetFile, "/files/temporary");
model.addAttribute("msg", "文件上传成功!");
} catch (Exception e) {
logger.error(e);
model.addAttribute("msg", "文件上传失败!");
}
return this.allButton(model, request, pageSplit);
}
public static void uploadFileMVC(HttpServletRequest request, MultipartFile[] fileSource, String fileTarget){
for(MultipartFile file : fileSource){
if(file.isEmpty()){
System.out.println("文件未上传");
}else{
//如果用的是Tomcat服务器,则文件会上传到\\%TOMCAT_HOME%\\webapps\\项目\\fileTarget\\文件夹中
String realPath = request.getSession().getServletContext().getRealPath(fileTarget);
//这里不必处理IO流关闭的问题,因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉,我是看它的源码才知道的
try {
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(realPath, file.getOriginalFilename()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询