springmvc.xml中添加文件上传的配置commons-io commons-io2.4 commons-fileupload commons-fileupload1.4 commons-io commons-io commons-codec commons-codec1.13
上传文件的网页
<%@ page contentType="text/html;charset=UTF-8" language="java" %>处理文件上传的控制器
@Controller @RequestMapping("/file") public class FileUploadController { @PostMapping("/upload") public ModelAndView fileUpload(@RequestParam("file") MultipartFile multipartFile, ModelAndView mav) { String originalFilename = multipartFile.getOriginalFilename(); //获取原文件名 String newFilename = UUID.randomUUID() + "_" + originalFilename; //使用UUID避免重名 File filePath = new File("D:/" + newFilename); try { multipartFile.transferTo(filePath); } catch (IllegalStateException|IOException e) { e.printStackTrace(); mav.addObject("msg", "上传失败"); mav.setViewName("show"); return mav; } mav.addObject("msg", "上传成功"); mav.setViewName("show"); return mav; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)