1.在src下创建filter包,包内Class名UploadFilter
package com.gd.filter; import org.apache.catalina.servlet4preview.http.HttpServletRequest; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; import java.io.IOException; @WebFilter(urlPatterns = "/upload/*", filterName = "uploadFilter") public class UploadFilter implements Filter { //定义文件路径常量 public static final File UPLOAD_FOLDER = new File("F:\\nginx-1.14.2\\html\\upload");//修改上传路径 @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("初始化uploadFilter"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //转换ServletRequest request类型 HttpServletRequest req = (HttpServletRequest) request; String uri = req.getRequestURI(); int index = uri.lastIndexOf("/"); String fileName = uri.substring(index + 1); File file = new File(UploadFilter.UPLOAD_FOLDER, fileName); //输入流 FileInputStream fileInputStream = new FileInputStream(file); byte[] bs = new byte[(int) file.length()]; fileInputStream.read(bs); //关闭流 fileInputStream.close(); //转换ServletResponse response类型 HttpServletResponse resp = (HttpServletResponse) response; resp.getOutputStream().write(bs); System.out.println("---请求进入--"); } @Override public void destroy() { } }
2.在控制层(controller)编写交互接口
package com.gd.controller; import com.gd.filter.UploadFilter; import org.apache.catalina.servlet4preview.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; @RestController public class UploadController { /** * 文件上传 * * @param multipartFile * @param request * @return */ @RequestMapping("/uploadFile") public String uploadFile(@RequestParam("multipartFile") MultipartFile multipartFile,HttpServletRequest request) { System.out.println(multipartFile.getOriginalFilename()); System.out.println("上传的文件夹路径:" + UploadFilter.UPLOAD_FOLDER); if (UploadFilter.UPLOAD_FOLDER.exists() == false) { UploadFilter.UPLOAD_FOLDER.mkdirs(); } //准备用来接收数据的文件 File uploadFile = new File(UploadFilter.UPLOAD_FOLDER, multipartFile.getOriginalFilename()); try { multipartFile.transferTo(uploadFile); } catch (IOException e) { e.printStackTrace(); } return "ok"; } }
3.前端页面上传入口静态页面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/uploadFile" method="post" enctype="multipart/form-data"> <input type="file" name="multipartFile"> <input type="submit" value="提交"> </form> </body> </html>
参考文章:http://blog.ncmem.com/wordpress/2023/11/23/springboot%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0%e4%bb%a3%e7%a0%81%e7%ac%94%e8%ae%b0/
欢迎入群一起讨论
标签:Springboot,UploadFilter,笔记,File,org,import,multipartFile,上传,public From: https://www.cnblogs.com/songsu/p/17852196.html