@Controller public class MultiController { // 处理器方法 @RequestMapping(value = "/two.do") public String doFirst(HttpSession session, @RequestParam MultipartFile[] uploadFile) throws Exception { for (MultipartFile item : uploadFile) { // 1.获取文件名称 String filename = item.getOriginalFilename(); // 6.必须选择上传文件 if (item.getSize() > 0) { // 5.限制文件类型 if (filename.endsWith("jpg") || filename.endsWith("png") || filename.endsWith("txt")) { // 2.获取保存的前路径 String rootPath = session.getServletContext().getRealPath( "upload"); // 3.拼接路径 File realPath = new File(rootPath, filename); // 4.保存 item.transferTo(realPath); } else { return "/error.jsp"; } } else { return "/error.jsp"; } } return "/two.jsp"; } }
时间: 2024-10-07 05:31:39