记录在 SpringBoot上传、下载文件中遇到的情况。项目以springboot+vue开发,在vue中未设置文件上传下载的限制,所以此次记录的均是springboot侧内容。
上传
- 报错:
org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException: the request was rejected because its size (26320005) exceeds the configured maximum (10485760) - 解决:
在springboot的配置文件中添加如下两行配置
yml:
servlet:
multipart:
max-file-size: 100MB
max-request-size: 100MB
- 注意100MB的单位MB需要全部大写
下载
- 项目中的下载方式是通过流的方式获取的,在定义读取文件时使用了下面的读取大小,导致报错:
byte[] buffer = new byte[1024];
//创建缓冲输入流
BufferedInputStream bis = null;
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
bis = new BufferedInputStream(new FileInputStream(new File(realPath + "/" + name)));
int read = bis.read(buffer);
//通过while循环写入到指定了的文件夹中
while (read != -1) {
outputStream.write(buffer, 0, buffer.length);
outputStream.flush();
read = bis.read(buffer);
}
} catch {......}
Caused by: java.io.IOException: 你的主机中的软件中止了一个已建立的连接。
了解到:在读取过程中,还没有读取完毕,主机网关就因时间到了中断了读取。还没有去验证过,先记录一下。
- 解决,修改一次读取流的大小:
byte[] buffer = new byte[4096];
遇到的文件上传下载异常及处理暂时只有这个,后续在有再添加修改。
—————————————————————
很多的知识都没有掌握,见谅,如有错误请指出,以期进步,感谢!。后续有新的学习,继续补充上来。