boa 上传文件到服务器,springMVC 上传文件到服务器

public String uploading(HttpServletRequest request, @RequestParam("file1") MultipartFile file,Model model) throws IllegalStateException, IOException {

//         ProducerTest producerTest = new ProducerTest();

//如果文件不为空,写入上传路径

if(!file.isEmpty()) {

//上传文件路径

String path = request.getServletContext().getRealPath("/files/");

//上传文件名

String filename = file.getOriginalFilename();

File filepath = new File(path,filename);

//判断路径是否存在,如果不存在就创建一个

if (!filepath.getParentFile().exists()) {

filepath.getParentFile().mkdirs();

}

//将上传文件保存到一个目标文件当中

file.transferTo(new File(path + File.separator + filename));

}

}

spring-mvc.xml 配置

//加上maven 坐标

commons-fileupload

commons-fileupload

1.2.1