SpringMvc 文件上传

public 
void importExcel(MultipartFile file, HttpServletRequest request) throws FileNotFoundException, IOException {

//tomcat下路径
String path = request.getSession().getServletContext().getRealPath("upload");
//上传文件名称
String fileName = file.getOriginalFilename();

System.out.println("****************开始***************");
File targetFile = new File(path, fileName);
if(!targetFile.exists()){
targetFile.mkdirs();
}

//保存
try {
file.transferTo(targetFile);
System.out.println("****************上传结束***************");
} catch (Exception e) {
e.printStackTrace();
}

原文地址:https://www.cnblogs.com/xiexy/p/3878478.html