修改下 我要把流直接返回 @GetMapping("/downloadBilling") public static

动态 未结 0 13
邮箱用户_8st46
邮箱用户_8st46 LV1 前天 17:20 发表
摘要:
修改下 我要把流直接返回 @GetMapping("/downloadBilling") public static boolean downloadFile(@RequestParam("url") String downloadUrl){ BufferedInputStream inputStream = null; FileOutputStream outputStream = null; boolean tag = true; try { URL url = new URL(downloadUrl); //这里没有使用 封装后的ResponseEntity 就是也是因为这里不适合一次性的拿到结果,放不下content,会造成内存溢出 HttpURLConnection connection =(HttpURLConnection) url.openConnection(); //使用bufferedInputStream 缓存流的方式来获取下载文件,不然大文件会出现内存溢出的情况 inputStream = new BufferedInputStream(connection.getInputStream()); // File file = new File(path); // if (file.exists()) { // file.delete(); // } // outputStream = new FileOutputStream(file); //这里也很关键每次读取的大小为5M 不一次性读取完 byte[] buffer = new byte[1024 * 1024 * 5];// 5MB int len = 0; while ((len = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } connection.disconnect(); }catch (Exception e){ tag=false; log.error("====download File failed====",e); }finally { try { if (outputStream != null) { outputStream.close(); } if (inputStream != null) { inputStream.close(); } } catch (IOException ioe) { // ignore } } return tag; }
我将帮你修改代码,直接将输入流返回给客户端,而不是保存到本地文件。以下是修改后的代码: ```java @GetMapping("/downloadBilling") public ResponseEntity downloadFile(@RequestParam("url") String downloadUrl) { try { URL url = new URL(downloadUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 获取输入流但不立即读取 InputStream inputStream = new BufferedInputStream(connection.getInputStream()); // 使用StreamingResponseBody实现流式传输 StreamingResponseBody responseBody = outputStream -> { try (inputStream) { byte[] buffer = new byte[1024 * 1024 * 5]; // 5MB缓冲区 int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.flush(); } finally { connection.disconnect(); } }; // 设置响应头 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); // 可以添加其他需要的响应头,如Content-Disposition return new ResponseEntity<>(responseBody, headers, HttpStatus.OK); } catch (Exception e) { log.error("====download File failed====", e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); } } ``` 主要修改点: 1. 改为返回`ResponseEntity`,支持流式传输 2. 使用`StreamingResponseBody`接口实现流式写入输出流 3. 移除了本地文件保存的逻辑 4. 添加了适当的响应头 5. 使用try-with-resources确保资源自动关闭 6. 添加了错误处理,返回500状态码 这种方法可以避免内存溢出问题,因为数据是流式传输的,不会一次性加载到内存中。 如果需要设置下载文件名,可以添加`Content-Disposition`响应头: ```java headers.setContentDisposition(ContentDisposition.attachment().filename("filename.ext").build()); ```
收藏(0)  分享
相关标签: 智能问答
问题没解决?让AI助手帮你作答 AI助手
0个回复
  • 消灭零回复