✨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/ 相关文章 智能推荐
1
✨Excel 样式复制工具类
技术分享 记录 Java 中复制 Excel 单元格样式的工具类实现,用于上传、读取和生成 Excel 文件时保留样式。
2
✨上移下移,升级降级树形节点操作工具类
技术分享 记录 Java 树形节点排序工具类实现,支持同层级上移下移、升级降级等节点调整操作。
3
✨Java 获取以太网IP
技术分享 记录 Java 遍历网卡并获取以太网 IP 的实现方式,适用于多网卡环境下筛选指定网络地址。
4
✨IDEA 常用快捷键
技术分享 整理 IntelliJ IDEA 日常开发常用快捷键,覆盖搜索、编辑、导航、重构、运行调试、Git 与窗口操作。
5
✨基于Redis + WebSocket的批量文件处理系统
技术分享 记录基于 Redis 缓存、WebSocket 实时通信和多线程处理的批量文件处理系统设计与实现思路。
随机文章 随机推荐