这个相信大家都已经玩臭了大街了都,所以直接把代码粘过来。
/**
* 上传批文
*
upfile 是页面<input type="file" name="upfile">的name值
* */
@RequestMapping(value="/upload",method=RequestMethod.POST)
@ResponseBody
public void upload(HttpServletRequest req,@RequestParam(
"upfile")MultipartFile file)throws IOException{
//如果用的是Tomcat服务器,则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\文件夹中
String realPath = req.getSession().getServletContext().getRealPath("/WEB-INF/upload");
System.out.println( file.getName());
//这里不必处理IO流关闭的问题,因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉
FileUtils.copyInputStreamToFile(file.getInputStream(),
new File(realPath, file.getOriginalFilename()));
System.out.println(realPath);
System.out.println(file.getOriginalFilename());
//FileUtils.copyInputStreamToFile(source, destination);
}