SpringBoot + Swagger2 文件上传、下载、直接响应图片

时间:2025-03-17 16:50:19

概要

代码的整体逻辑是,文件上传到服务器后,我们把文件以IO的形式写到磁盘中,然后返回一个网络路径。

当我们要请求文件时,先将URL过滤,那么分两种情况,第一种情况是 我们的文件是图片类型,我们就直接响应回图片;如果是非图片类型,那么就提供下载功能

单文件上传

    /**
     * 上传接口
     * @param file
     * @return
     * @throws IllegalStateException
     * @throws IOException
     */
    @ApiOperation(value = "上传接口")
    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file)
            throws IllegalStateException, IOException {
        return (file);
    }
    @Value("${}")
    private String localPath;

    @Value("${}")
    private String netPath;

    @Override
    public String upload(MultipartFile file) throws IOException {

        if(file == null) {
            throw new BaseException(, ResultEnum.FILE_NOT_FOUND);
        }

        String originalFileName = ();
        String[] arr = ("\\.");
        String prefix = arr[ - 1];
        String fileName = () + "." + prefix;
        Boolean res = IMAGE_TYPE_LIST.contains(prefix);
        String path;

        if(res) {
            path = localPath + "\\image";
        } else {
            path = localPath + "\\file";
        }
        path += ().format(("yyyy-MM-dd"));

        File filePath = new File(path, fileName);
        // 如果文件目录不存在,创建目录
        if (!().exists()) {
            ().mkdirs();
        }
        // 写入文件
        (filePath);
        return netPath +"/"+ fileName;
    }

其中@value是SpringBoot在配置文件中查找自定义的配置,直接映射到相应的变量中,这里需要自己配置

多文件上传

    /**
     * 多文件上传
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "文件上传",notes = "文件上传")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "files",value = "多个文件,",paramType = "formData",allowMultiple=true,required = true,dataType = "file"),
            @ApiImplicitParam(name = "uId",value = "上传用户ID,",paramType = "query",required = true,dataType = "Long")
    })
    @PostMapping(value = "/uplode/files", headers = "content-type=multipart/form-data")
    public void uplodeFiles(@RequestParam(value = "files") MultipartFile[] files,Long uId) throws IOException {
        int maxCount = ;
        for (int i = 0; i < maxCount; i ++) {
            (files[i]);
        }
    }

单文件下载

    @ApiOperation(value = "下载接口")
    @ApiImplicitParam(name = "downloadFilePath", value = "文件地址", paramType = "query", required = true)
    @GetMapping("/download")
    public void download(HttpServletResponse response, @RequestParam String downloadFilePath) {
        (response, downloadFilePath);
    }   
    @Override
    public void download(HttpServletResponse response, String downloadFilePath) {
        String[] arr = ("\\.");
        String fileName = arr[ - 2] + "." + arr[ - 1];//被下载文件的名称
        File file = new File(downloadFilePath);
        if (()) {
            ("application/force-download");// 设置强制下载不打开            
            ("Content-Disposition", "attachment;fileName=" + fileName);
            byte[] buffer = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                OutputStream outputStream = ();
                int i = (buffer);
                while (i != -1) {
                    (buffer, 0, i);
                    i = (buffer);
                }
            } catch (Exception e) {
                ();
            } finally {
                if (bis != null) {
                    try {
                        ();
                    } catch (IOException e) {
                        ();
                    }
                }
                if (fis != null) {
                    try {
                        ();
                    } catch (IOException e) {
                        ();
                    }
                }
            }
        }
    }

图片文件直接响应到页面(使用过滤器)

@SpringBootApplication
@ServletComponentScan
public class CarpBusinessApplication {

    public static void main(String[] args) {
        (, args);
    }
}
/**
 * @Description: TODO
 * @Author yjw
 * @Date 2020/4/2
 **/
@WebFilter(filterName = "ImageFilter", urlPatterns = "/a/q/upload/download/*")
public class ImageFilter implements Filter {

    @Autowired
    FileService fileService;

    private static final List<String> IMAGE_TYPE_LIST = ("bmp","jpg","png",
            "tif","gif","pcx","tga","exif","fpx","svg","psd","cdr",
            "pcd","dxf","ufo","eps","ai","raw","WMF","webp","pdf");

    @Value("${}")
    private String netPath;

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        String Uri = ((HttpServletRequest) request).getRequestURI();
        String[] arr = ("/");
        String fileName = arr[ - 1];

        String[] nameArr = ("\\.");
        if(IMAGE_TYPE_LIST.contains(nameArr[ - 1])) {
            ((HttpServletResponse) response, fileName);
        } else {
            (request, response);
        }
    }
}

@WebFilter注解中不包括Component注解,所以需要在主类上加@ServletComponentScan注解。

这里过滤器中的逻辑是如果是图片那么我就把图片直接响应回去,如果是其他的文件,那么就取消过滤这个请求,让他进入相应的Controller

需要注意的是,过滤器过滤的是http://ip地址:端口/项目名 后面的东西,而不包括项目名,这里一定要注意。

    @Override
    public void downloadImage(HttpServletResponse response, String fileName) {
        File file = new File(localPath + "\\image\\" + fileName);
        if (()) {
            try {
                (new FileInputStream(file), ());

                ("image/jpg");
                return;
            } catch (IOException e) {
                throw new BaseException(, ResultEnum.IMAGE_CANNOT_FOUND);
            }
        }
    }