欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > java下载文件流,不生成中间文件。

java下载文件流,不生成中间文件。

2025/2/24 10:05:13 来源:https://blog.csdn.net/qq_38105536/article/details/144854985  浏览:    关键词:java下载文件流,不生成中间文件。

java下载文件流,不生成中间文件。

    • 代码设计:
    • 代码实现

代码设计:

从前端获取的数据经过后端加工后,生成文件流,并返回前端,(不生成中间文件,注意内存,记得关闭流)

代码实现

    @ApiOperation(value = "下载文件", notes = "")@PostMapping("/getDownLoadScriptFile")public void getDownLoadScriptFile(@RequestBody ParamsObject vo, HttpServletRequest request, HttpServletResponse response) throws Exception {SysUserEntityVo uc = (SysUserEntityVo) request.getAttribute("UC");gClientScriptService.getDownLoadScriptFile(vo, uc,response);}
  @Overridepublic void getDownLoadScriptFile(ParamsObject vo, SysUserEntityVo uc, HttpServletResponse response) throws Exception {String fileContent = vo.getFileContent();String fileName = vo.getFileName();String encodeFileName = URLEncoder.encode(fileName);ServletOutputStream out = response.getOutputStream();try {//设置允许跨域的keyresponse.setHeader("Access-Control-Expose-Headers", "Content-Disposition");//文件名有“,”等特殊字符发送到前端会报错,用""括起来解决response.addHeader("Content-Disposition", "attachment;filename=\"" + encodeFileName + "\"");//设置文件大小response.addHeader("Content-Length", "" + fileContent.getBytes().length);//设置文件名,避免问题,这个也用""括起来response.setHeader("filename,", "filename=\"" + encodeFileName + "\"");//设置文件类型response.setContentType("application/octet-stream");out.write(fileContent.getBytes(StandardCharsets.UTF_8));out.flush();} catch (Exception e) {throw e;} finally {try {out.close();} catch (Exception e) {throw e;}try {out.close();} catch (Exception e) {throw e;}}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词