• Springboot文件上传代码笔记


    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>
  • 相关阅读:
    Why does my Authorize Attribute not work?
    百度网页搜索部来自Console的招聘信息
    Javascript 日期时间超强正则表达式
    解决SQL Server [Suspect]
    我回来了
    不忘初心
    WEB系统技术开发方向
    使用postman模拟登录请求
    什么是蓝绿部署?
    element-ui Cascader 级联选择器示例
  • 原文地址:https://www.cnblogs.com/StanleyBlogs/p/10482830.html
Copyright © 2020-2023  润新知