Java实现单文件、多文件上传
前端上传文件代码
<input type="file" name="file"/>
后端接收文件代码
//单文件上传
public String uploadFile(@RequestParam("file1") MultipartFile file1,HttpServletRequest request)throws Exception{
Integer infoId=Integer.parseInt(request.getParameter("infoId"));
String filePath=request.getServletContext().getRealPath("/");
System.out.println(filePath);
file1.transferTo(new File(filePath+"upload/"+file1.getOriginalFilename()));
return "forward:/show/read.do?infoId="+infoId;
}
//多文件上传
public String uploadFiles(@RequestParam("file") MultipartFile[] files,HttpServletRequest request)throws Exception{
String filePath=request.getServletContext().getRealPath("/");
System.out.println(filePath);
for(MultipartFile file:files){
file.transferTo(new File(filePath+"upload/"+file.getOriginalFilename()));
}
return "redirect:/success.jsp";
}