导包:
在pom. 中导入FileUpload所需的包:

commons-fileupload commons-fileupload 1.3.3

注:FileUpload需要commons-fileupload-1.3.3.jar和commons-io-2.4.jar两个包,导入FileUpload后两个包都存在。


H5实现选择多个文件:




springMVC的核心配置

Controoler代码
@Autowired
private ServletContext context;
@RequestMapping(value = “/fileUpload”,method = RequestMethod.POST)

public String upload(@RequestParam(\"fieldName\")  MultipartFile fieldName,Model model,HttpServletRequest request) throws  Exception{

// 判断文件是否为空
if(!fieldName.isEmpty()){
//获取源文件名
String filename = fieldName.getOriginalFilename();
//拼路径
String filePath =context.getRealPath(\"\")+“upload”+ File.separator;

//判断路径是否存在
File file = new File(filePath);
if(!file.exists()){
file.mkdir();
}
//截取后缀名
String lastName=filename.substring(filename.lastIndexOf(\".\"));
//设置时间戳
//拼接完整的路径
filePath = filePath + System.currentTimeMillis() +lastName;
System.out.println(filePath);
//上传
fieldName.transferTo(new File(filePath));
model.addAttribute(“filePath”,filePath);
}
return “kk.jsp”;

}
收藏 打印