美文网首页
springBoot 文件上传,下载(加部分踩坑记录)

springBoot 文件上传,下载(加部分踩坑记录)

作者: 南土酱 | 来源:发表于2019-10-08 17:53 被阅读0次

虽然网络上也有其他的教程或者分享了,但是还是喜欢记一下自己的。

上传文件:

public Result savefile(String filePath, MultipartFile[] files) throws IOException {

        for (MultipartFile file : files) {
            //第一步:判断文件大小
            if (file.getSize() > 1024 * 1024 * 10) {
                return new Result(CommonCode.FAIL, "文件过大,请上传10M以内的文件");
            }
        }
        File targetFile = new File(filePath);
        //第二步:判断目录是否存在  不存在:创建目录
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        //第三部:通过输出流将文件写入文件夹并关闭流
        BufferedOutputStream stream = null;
        for (MultipartFile file : files) {
            String fileName = file.getOriginalFilename();
            if (file.isEmpty()) {
                return new Result(CommonCode.FAIL, fileName + "文件为空");
            } else {
                try {
                    stream = new BufferedOutputStream(new FileOutputStream(filePath + fileName));
                    stream.write(file.getBytes());
                    stream.flush();
                } finally {
                    if (stream != null) stream.close();
                }
            }
        }

        return new Result(CommonCode.SUCCESS, "文件上传成功");
    }
(提示)filePath是文件存储的路径
MultipartFile 可以用来多文件上传
!第一步的文件大小可以任意改动。(此处有坑)

假如你报了这个错:

org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: 
The field fileName exceeds its maximum permitted size of 1048576 bytes.
(Tomcat 默认限制的文件上传的大小为 1MB ) 1MB = 1024KB * 1024B  1B = 1 bytes
所以会报错。可能有人会想:那为什么程序走不到controller层里边(debug 时候程序没走到server层
甚至是controller层)?  因为:  这是tomcat抛出的异常,不属于程序抛出的

解决方法:

application.yml:
spring:
  servlet:
    multipart:
      enabled: true
      max-file-size: 10MB
      max-request-size: 10MB
(添加如上代码即可解决。注意: 我是用yml配置的, properties后缀配置 此处不给出)

下载文件

下载文件较为简单,主要是乱码问题和下载的位置问题.
乱码此处我已经解决,位置问题自己传参数解决即可.
另外: 有人说下载的文件, 文件下载不到本地上,只下载到了服务器上。
也就是说文件下载之后的存储位置是在服务器上,不在本地上。但本人代码测试过,不会出现这个问题。

我的猜想: 文件下载的时候是通过 IO去操作的。如下图,如果用的是  response(客户端响应) 就会把文件传输到
客户端那边, 但如果另外 new OutputStream() ,就会造成下载的文件的路径是在服务器上,客户端啥都没有,
但是文件下载却成功了
image.png
public Result downLoad(HttpServletResponse response, String fileName, String path) throws IOException {
        if (fileName != null) {
            FileInputStream is = null;
            BufferedInputStream bs = null;
            OutputStream os = null;
            try {
                File file = new File(path,fileName);
                if (file.exists()) {
                    //设置Headers
                    response.setHeader("Content-Type", "application/octet-stream");
                    response.setContentType("application/octet-stream");
                    //设置下载的文件的名称-该方式已解决中文乱码问题
                    response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
                    //实现下载
                    is = new FileInputStream(file);
                    bs = new BufferedInputStream(is);
                    os = response.getOutputStream();
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len = bs.read(buffer)) != -1) {
                        os.write(buffer, 0, len);
                    }
                } else {
                    return new Result(CommonCode.FAIL, "下载的文件资源不存在");
                }
            } finally {
                if (is != null) {
                    is.close();
                }
                if (bs != null) {
                    bs.close();
                }
                if (os != null) {
                    os.flush();
                    os.close();
                }
            }
        }
        return new Result(CommonCode.SUCCESS, "进行下载");
    }

相关文章

网友评论

      本文标题:springBoot 文件上传,下载(加部分踩坑记录)

      本文链接:https://www.haomeiwen.com/subject/pcfipctx.html