@RequestMapping("/upload")
public String upload(HttpServletRequest request) throws IllegalStateException, IOException{
//基于http的文件上传:解析浏览器的请求的协议
//1.创建一个解析器
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
//2.判断请求当中是否包含文件,看是否属于文件的表单
boolean flag = multipartResolver.isMultipart(request);
if(flag){
//3.request对象强转
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
//4.通过上面已转的对象获取文件
Iterator<String> fileNames = multipartRequest.getFileNames();
//5.while循环拿到每一个文件
while(fileNames.hasNext()){
//6.根据拿到的文件名获取文件
MultipartFile multipartFile = multipartRequest.getFile((String)fileNames.next());
//7.判断文件名是否为空
String fileName = multipartFile.getOriginalFilename();
if(fileName != null){
//8.构造上传路径
String path = "E:/upload/" + fileName;
File file = new File(path);
//9.转移文件
multipartFile.transferTo(file);
}
}
}
request.setAttribute("msg", "上传成功");
return "forward:/file/jump";
}
@RequestMapping("/jump")
public String jump(HttpServletRequest request,Model model){
System.out.println("jump page");
//model.addAttribute("msg","上传成功");
return "/success";
}