✨Response 响应流遇到的坑

717 字
4 分钟
✨Response 响应流遇到的坑

前端获取Blob大小为0代码

private void downloadFileByPath(String path, String fileName, HttpServletResponse response) {
try {
// 设置内容格式
response.setContentType("image/png");
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(sysFileDTO.get().getFileName(), "UTF-8"));
response.setHeader("Content-Length", sysFileDTO.get().getFileSize() + "");
// 内容安全策略
response.setHeader("Content-Security-Policy", "script-src 'self' 'unsafe-inline' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data:;connect-src 'self' <http://127.0.0.1:8080> data:;font-src 'self';object-src 'self';");
} catch (UnsupportedEncodingException e) {
log.error("文件编码异常 : {}", e.getMessage());
}
// 将文件流写入输出流
File file = new File(path);
if (file.exists()) {
InputStream is = null;
try {
is = new FileInputStream(file);
IOUtils.copy(is, response.getOutputStream());
response.getOutputStream().flush();
} catch (FileNotFoundException e) {
log.error("文件未找到异常 : {}", path);
throw new RuntimeException("文件未找到");
} catch (IOException e) {
log.error("文件读写异常 : {}", path);
throw new RuntimeException("文件读写异常");
} finally {
try {
is.close();
} catch (IOException e) {
throw new RuntimeException("文件关闭异常");
}
}
} else {
log.error("文件未找到异常 : {}", path);
throw new RuntimeException("文件未找到");
}
}

前端获取Blob大小正常代码

private void downloadFileByPath(String path, String fileName, HttpServletResponse response) {
try {
// 设置内容格式
response.setContentType("image/png");
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(sysFileDTO.get().getFileName(), "UTF-8"));
response.setHeader("Content-Length", sysFileDTO.get().getFileSize() + "");
// 内容安全策略
response.setHeader("Content-Security-Policy", "script-src 'self' 'unsafe-inline' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data:;connect-src 'self' <http://127.0.0.1:8080> data:;font-src 'self';object-src 'self';");
} catch (UnsupportedEncodingException e) {
log.error("文件编码异常 : {}", e.getMessage());
}
// 将文件流写入输出流
File file = new File(path);
if (file.exists()) {
InputStream is = null;
try {
is = new FileInputStream(file);
// 必须使用赋值的方式
ServletOutputStream outputStream = response.getOutputStream();
IOUtils.copy(is, outputStream);
response.getOutputStream().flush();
} catch (FileNotFoundException e) {
log.error("文件未找到异常 : {}", path);
throw new RuntimeException("文件未找到");
} catch (IOException e) {
log.error("文件读写异常 : {}", path);
throw new RuntimeException("文件读写异常");
} finally {
try {
is.close();
} catch (IOException e) {
throw new RuntimeException("文件关闭异常");
}
}
} else {
log.error("文件未找到异常 : {}", path);
throw new RuntimeException("文件未找到");
}
}

也可以使用这种方式

private void downloadFileByPath(String path, String fileName, HttpServletResponse response) {
try {
// 设置内容格式
response.setContentType("image/png");
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(sysFileDTO.get().getFileName(), "UTF-8"));
response.setHeader("Content-Length", sysFileDTO.get().getFileSize() + "");
// 内容安全策略
response.setHeader("Content-Security-Policy", "script-src 'self' 'unsafe-inline' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data:;connect-src 'self' <http://127.0.0.1:8080> data:;font-src 'self';object-src 'self';");
} catch (UnsupportedEncodingException e) {
log.error("文件编码异常 : {}", e.getMessage());
}
// 将文件流写入输出流
File file = new File(path);
if (file.exists()) {
InputStream is = null;
ServletOutputStream outputStream = null;
try {
is = new FileInputStream(file);
// 必须使用赋值的方式
out = response.getOutputStream();
// 创建缓冲区
byte buffer[] = new byte[4096];
int len = 0;
// 循环将输入流中的内容读取到缓冲区当中
while ((len = in.read(buffer)) > 0) {
// 输出缓冲区的内容到浏览器,实现文件下载
out.write(buffer, 0, len);
}
} catch (FileNotFoundException e) {
log.error("文件未找到异常 : {}", path);
throw new RuntimeException("文件未找到");
} catch (IOException e) {
log.error("文件读写异常 : {}", path);
throw new RuntimeException("文件读写异常");
} finally {
try {
is.close();
out.close();
} catch (IOException e) {
throw new RuntimeException("文件关闭异常");
}
}
} else {
log.error("文件未找到异常 : {}", path);
throw new RuntimeException("文件未找到");
}
}

具体因为 ServletOutputStream outputStream = response.getOutputStream(); 就正常的原因还是未知,没有太多的时间排查。

其中还遇到使用日志 log.error(“大小:{}”, is.readAllBytes().length); 输出长度也会影响前端Blob的大小为0,同时图片响应也是异常的。

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

✨Response 响应流遇到的坑
https://dear7575.cn/posts/response-error/
作者
北港不夏
发布于
2024-04-26
许可协议
CC BY-NC-SA 4.0

评论区

Profile Image of the Author
北港不夏
Hello, I'm 北港不夏.
公告
欢迎来到我的博客!这是一则示例公告。
音乐
封面

音乐

暂未播放

0:00 0:00
暂无歌词
分类
标签
站点统计
文章
36
分类
3
标签
49
总字数
22,285
运行时长
0
最后活动
0 天前

文章目录