- 页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="/fileUpload" method="post" enctype="multipart/form-data"> <label>上传图片</label> <input type="file" name="file"/> <input type="submit" value="上传"/> </form> <p>图片:</p> <img src="${filename }"/> </body> </html>
- controller
package com.rainy.controller; import org.apache.catalina.servlet4preview.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import java.io.*; import java.util.UUID; /** * 文件上传 */ @Controller public class FileController { @GetMapping(value = "/file") public String file() { return "file"; } @PostMapping(value = "/fileUpload") public String fileUpload(@RequestParam(value = "file") MultipartFile file, Model model, HttpServletRequest request) { if (file.isEmpty()) { System.out.println("文件为空空"); } String fileName = file.getOriginalFilename(); // 文件名 String suffixName = fileName.substring(fileName.lastIndexOf(".")); // 后缀名 String filePath = "D://temp-rainy//"; // 上传后的路径 fileName = UUID.randomUUID() + suffixName; // 新文件名 File dest = new File(filePath + fileName); if (!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); } try { file.transferTo(dest); } catch (IOException e) { e.printStackTrace(); } String filename = "/temp-rainy/" + fileName; model.addAttribute("filename", filename); return "file"; } }
- 配置资源映射路径
/** * 资源映射路径 */ @Configuration public class MyWebAppConfigurer implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/temp-rainy/**").addResourceLocations("file:D:/temp-rainy/"); } }
- 替他参考
import java.io.File; import java.io.FileOutputStream; /** * @Author: Mz * @Data: 2019/1/9 16:51 * * 文件工具类 */ public class FileUtil { public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception { File targetFile = new File(filePath); if(!targetFile.exists()){ targetFile.mkdirs(); } FileOutputStream out = new FileOutputStream(filePath+fileName); out.write(file); out.flush(); out.close(); } }
@Resource HttpServletRequest request; //处理文件上传 @RequestMapping(value="/testuploadimg", method = RequestMethod.POST) public @ResponseBody String uploadImg(@RequestParam("file") MultipartFile file) { String fileName = file.getOriginalFilename(); //设置文件上传路径 String filePath = request.getSession().getServletContext().getRealPath("imgupload/"); try { FileUtil.uploadFile(file.getBytes(), filePath, fileName); return "上传成功"; } catch (Exception e) { return "上传失败"; } } /* * 文件上传到SpringBoot自带的Tomcat服务器中 * 文件可以在 file:///C:/Users/user/AppData/Local/Temp/tomcat-docbase.4531370466993049046.8080(jar包每启动一次产生一个文件夹)/imgeas/weixinimg/201904031556/1554278165070784.jpg 看到 */