springboot 文件的上传下载

SpringMVC的文件上传是通过MultipartResolver(Multipart解析器)处理,MultipartResolver只是一个接口,有两个实现类。
1.CommonsMultipartResolver :依赖Apache FileUpload项目解析Multipart,可以在Spring的各个版本使用,需要依赖第三方jar包。
2.StandardServletMultipartResolver: 是Spring3.1之后的产物,依赖于Servlet3.0或更高版本的实现,不需要第三方jar包。

Springboot实现文件上传

Springboot默认可以使用文件上传,使用transferTo方法保存文件。

示例:FileCtrl.java

服务器根目录创建文件夹放置文件

@RestController
@Slf4j
public class FileCtrl {

    @PostMapping("/singleFile")
    @ResponseBody
    public String singleFile(@RequestParam("file") MultipartFile file) {
        //判断非空
        if (file.isEmpty()) {
            return "上传的文件不能为空";
        }
        try {
            // 测试MultipartFile接口的各个方法
            log.info("[文件类型ContentType] - [{}]", file.getContentType());
            log.info("[文件组件名称Name] - [{}]", file.getName());
            log.info("[文件原名称OriginalFileName] - [{}]", file.getOriginalFilename());
            log.info("[文件大小] - [{}]", file.getSize());
            //文件路径
            String path = System.getProperty("user.dir") + "/upload/";
            log.info(this.getClass().getName() + "图片路径:" + path);
            File f = new File(path);
            //如果不存在该路径就创建
            if (!f.exists()) {
                f.mkdir();
            }
            File dir = new File(path + file.getOriginalFilename());
            // 文件写入
            file.transferTo(dir);
            return "上传单个文件成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "上传单个文件失败";
        }
    }

    @PostMapping("/multipleFiles")
    @ResponseBody
    public String multipleFiles(@RequestParam("file") MultipartFile[] files) {
        if (null == files && files.length == 0) {
            return null;
        }
        String filePath = System.getProperty("user.dir") + "/uploads/";
        log.info(this.getClass().getName() + "图片路径:" + filePath);
        File f = new File(filePath);
        //如果不存在该路径就创建
        if (!f.exists()) {
            f.mkdir();
        }
        for (MultipartFile mf : files) {
            //文件名称
            String filename = mf.getOriginalFilename();
            if (mf.isEmpty()) {
                return "文件名称:" + filename + "上传失败,原因是文件为空!";
            }
            File dir = new File(filePath + filename);
            try {
                //写入文件
                mf.transferTo(dir);
                log.info("文件名称:" + filename + "上传成功");
            } catch (IOException e) {
                log.error(e.toString(), e);
                return "文件名称:" + filename + "上传失败";
            }
        }
        return "多文件上传成功";
    }
}
html测试

singleFile.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p>单文件上传</p>
<form method="POST" enctype="multipart/form-data" action="http://localhost:8080/singleFile" >
    文件:<input type="file" name="file"/>
    <input type="submit"/>
</form>
<hr/>
</body>
</html>

multipleFiles.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<p>多文件上传</p>
<form method="POST" enctype="multipart/form-data" action="http://localhost:8080/multipleFiles">
    <p>文件1:<input type="file" name="file"/></p>
    <p>文件2:<input type="file" name="file"/></p>
    <p><input type="submit" value="上传"/></p>
</form>
</body>
</html>
配置文件application.properties

默认每个文件的配置最大为1Mb,单次请求的文件的总数不能大于10Mb。
修改默认配置大小

#springboot 2.x
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=100MB

在这里插入图片描述
如图,根据提示可知除设置文件大小外还有四个属性可设置如下

#是否支持 multipart 上传文件,默认true,false无法上传
spring.servlet.multipart.enabled=true
#文件大小阈值,大于这个值将写入磁盘,否则在内存中,默认0,一般不修改
spring.servlet.multipart.file-size-threshold=0
#临时文件目录
spring.servlet.multipart.location=
# 判断是否要延迟解析文件,懒加载,一般不修改
spring.servlet.multipart.resolve-lazily=false
Springboot实现文件下载
单文件下载

传入文件名,下载服务器upload目录下文件

 @GetMapping(value = "/downSingleFile")
    public String download(HttpServletResponse response, String fileName) throws UnsupportedEncodingException {
//        String filePathName = "11你好.docx";
        String filePathName = fileName;
        File file = new File("upload/" + filePathName);
        if (!file.exists()) {
            return "文件不存在";
        }
        //使用URLEncoder解决中文变__问题
        //response.setHeader("Content-Disposition", "attachment;fileName=" + filePathName);
        response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(filePathName, "utf-8"));
        try {
            InputStream inStream = new FileInputStream(file);
            OutputStream os = response.getOutputStream();
            byte[] buff = new byte[1024];
             int i = 0;
            //read方法返回读取到字节数,=0说明到达文件结尾,=-1说明发生错误
            while ((i = inStream.read(buff)) != -1) {
                //write()方法3个参数,可自定义读取字节数0-i;
                os.write(buff, 0, i);
            }
            os.flush();
            os.close();
            inStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "下载成功";
    }

多文件下载

多个文件打包下载
解决方法:将需要文件复制到临时文件夹,打包zip下载,删除临时文件夹

找的一个工具类,可直接使用
ZipUtils.java

package com.example.demo;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * file-upload
 * 2019/11/19 16:45
 *
 * @author
 * @description 多文件压缩zip
 **/
@Slf4j
public class ZipUtils {

    private static final int BUFFER_SIZE = 2 * 1024;

    /**
     * 压缩成ZIP 方法1
     *
     * @param srcDir           压缩文件夹路径
     * @param out              压缩文件输出流
     * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
     *                         <p>
     *                         false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
     * @throws RuntimeException 压缩失败会抛出运行时异常
     */

    public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure) throws RuntimeException {

        long start = System.currentTimeMillis();
        ZipOutputStream zos = null;
        try {
            zos = new ZipOutputStream(out);
            File sourceFile = new File(srcDir);
            compress(sourceFile, zos, sourceFile.getName(), KeepDirStructure);
            long end = System.currentTimeMillis();
            log.info("压缩完成,耗时:" + (end - start) + " ms");
        } catch (Exception e) {
            throw new RuntimeException("zip error from ZipUtils", e);
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 压缩成ZIP 方法2
     *
     * @param srcFiles 需要压缩的文件列表
     * @param out      压缩文件输出流
     * @throws RuntimeException 压缩失败会抛出运行时异常
     */
    public static void toZip(List<File> srcFiles, OutputStream out) throws RuntimeException {
        long start = System.currentTimeMillis();
        ZipOutputStream zos = null;
        try {
            zos = new ZipOutputStream(out);
            for (File srcFile : srcFiles) {
                byte[] buf = new byte[BUFFER_SIZE];
                zos.putNextEntry(new ZipEntry(srcFile.getName()));
                int len;
                FileInputStream in = new FileInputStream(srcFile);
                while ((len = in.read(buf)) != -1) {
                    zos.write(buf, 0, len);
                }
                zos.closeEntry();
                in.close();
            }
            long end = System.currentTimeMillis();
            System.out.println("压缩完成,耗时:" + (end - start) + " ms");
        } catch (Exception e) {
            throw new RuntimeException("zip error from ZipUtils", e);
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 递归压缩方法
     *
     * @param sourceFile       源文件
     * @param zos              zip输出流
     * @param name             压缩后的名称
     * @param KeepDirStructure 是否保留原来的目录结构, true:保留目录结构;
     *                         false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
     * @throws Exception
     */

    private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure)
            throws Exception {

        byte[] buf = new byte[BUFFER_SIZE];
        if (sourceFile.isFile()) {
            // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
            zos.putNextEntry(new ZipEntry(name));
            // copy文件到zip输出流中
            int len;
            FileInputStream in = new FileInputStream(sourceFile);
            while ((len = in.read(buf)) != -1) {
                zos.write(buf, 0, len);
            }
            // Complete the entry
            zos.closeEntry();
            in.close();
        } else {
            File[] listFiles = sourceFile.listFiles();
            if (listFiles == null || listFiles.length == 0) {
                // 需要保留原来的文件结构时,需要对空文件夹进行处理
                if (KeepDirStructure) {
                    // 空文件夹的处理
                    zos.putNextEntry(new ZipEntry(name + "/"));
                    // 没有文件,不需要文件的copy
                    zos.closeEntry();
                }
            } else {
                for (File file : listFiles) {
                    // 判断是否需要保留原来的文件结构
                    if (KeepDirStructure) {
                        // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
                        // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
                        compress(file, zos, name + "/" + file.getName(), KeepDirStructure);
                    } else {
                        compress(file, zos, file.getName(), KeepDirStructure);
                    }
                }
            }
        }
    }


    /**
     * 拷贝文件夹
     *
     * @param oldPath
     * @param newPath
     */
    public static void copyDir(String oldPath, String newPath) throws IOException {
        File file = new File(oldPath);
        //文件名称列表
        String[] filePath = file.list();

        if (!(new File(newPath)).exists()) {
            (new File(newPath)).mkdir();
        }

        for (int i = 0; i < filePath.length; i++) {
            if ((new File(oldPath + File.separator + filePath[i])).isDirectory()) {
                copyDir(oldPath + File.separator + filePath[i], newPath + File.separator + filePath[i]);
            }

            if (new File(oldPath + File.separator + filePath[i]).isFile()) {
                copyFile(oldPath + File.separator + filePath[i], newPath + File.separator + filePath[i]);
            }

        }
    }

    /**
     * 拷贝文件
     *
     * @param oldPath
     * @param newPath
     */
    public static void copyFile(String oldPath, String newPath) throws IOException {
        File oldFile = new File(oldPath);
        File file = new File(newPath);
        FileInputStream in = new FileInputStream(oldFile);
        FileOutputStream out = new FileOutputStream(file);
        ;
        //2M
        byte[] buffer = new byte[2097152];

        while ((in.read(buffer)) != -1) {
            out.write(buffer);
        }
        in.close();
        out.close();
    }

}

ctrl层调用工具类下载

 @GetMapping("/downMultipleFiles")
    public String multipleFiles(HttpServletResponse response, String[] fileName) throws IOException {
        //1.创建临时文件夹
        String downPath = System.getProperty("user.dir") + "/downzip/";
        File f = new File(downPath);
        //如果不存在该路径就创建
        if (!f.exists()) {
            f.mkdir();
        }
        //2.将需要下载文件复制到临时文件夹
//        List<File> fileList = new ArrayList<>();
        for (int i = 0; i < fileName.length; i++) {
            String uploadPath = System.getProperty("user.dir") + "/upload/" + fileName[i];
//            File file = new File(uploadPath);
//            fileList.add(file);
            ZipUtils.copyFile(uploadPath, downPath + "/" + fileName[i]);
        }
        //3.设置response,设置压缩包名称
        response.setContentType("application/zip");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        String filename = format + "down.zip";
        response.setHeader("Content-Disposition", "attachment; filename=" + filename);
        //4.调用工具类,下载zip
        ZipUtils.toZip(downPath, response.getOutputStream(), true);
//        ZipUtils.toZip(fileList,response.getOutputStream());
        //5.删除临时文件夹
        File[] listFiles = f.listFiles();
        for (int i = 0; i < listFiles.length; i++) {
            listFiles[i].delete();
            log.info("正在删除第" + i + "个文件");
        }
        f.delete();
        return "多文件下载成功";
    }
参考:

Spring boot中下载文件名称中文变___________下划线了,请问如何处理?
springboot多文件下载

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值