/** * upload image and return the image url * * @return * @throws IOException * @throws IllegalStateException */ @RequestMapping("/upload") @ResponseBody public String uploadImage(HttpServletRequest request, HttpServletResponse response, HttpSession session, @RequestParam(value = "file", required = true) MultipartFile file) throws IllegalStateException, IOException { String path = session.getServletContext().getRealPath("/upload"); System.out.println("real path: " + path); String fileName = file.getOriginalFilename(); if (fileName.length()<1){ fileName = "image.png"; } System.out.println("file name: " + fileName); File targetFile = new File(path, fileName); if (!targetFile.exists()) { targetFile.getParentFile().mkdirs(); } file.transferTo(targetFile); String fileUrl = request.getContextPath() + "/upload/" + fileName; return fileUrl; }
接受文件上传
最新推荐文章于 2023-06-08 10:57:05 发布