1.导包
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
2.在页面添加上传文件按钮
<input type="file" name="Pic">
3.编写控制类方法
public ModelAndView upload(@RequestParam("Pic") MultipartFile Pic) throws IOException {
UUID rid=UUID.randomUUID();
String filename = Pic.getOriginalFilename();
String suffix=filename.substring(filename.lastIndexOf('.'));
String imgName=uid+suffix;
File file=new File(UPLOAD_PATH+"/"+imgName);
Pic.transferTo(file);
ModelAndView mv=new ModelAndView();
mv.setViewName("upload");
mv.addObject("upload_file_path",file.getName());
return mv;
}