前端代码

后端以流的形式返回

单个文件下载

    @RequestMapping(value = "download", method = RequestMethod.POST)@ApiOperation(value = "单个文件下载")public HttpServletResponse download(@RequestBody FileDto fileDto, HttpServletResponse response) {InputStream fis = null;OutputStream toClient = null;try {// path是指欲下载的文件的路径。String dirPath = cloud_root + fileDto.getPath();// 取得文件名。String filename = fileDto.getFileName();File file = new File(dirPath + "/" + filename);fis = new BufferedInputStream(new FileInputStream(file));byte[] buffer = new byte[fis.available()];fis.read(buffer);fis.close();// 清空responseresponse.reset();// 解决跨域问题,这句话是关键,对任意的域都可以,如果需要安全,可以设置成安前的域名response.addHeader("Access-Control-Allow-Origin", "*");response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");// 设置response的Headerresponse.setHeader("Content-Disposition", "attachment;filename="+ new String(filename.getBytes(),"iso-8859-1"));response.addHeader("Content-Length", "" + file.length());toClient = new BufferedOutputStream(response.getOutputStream());response.setContentType("application/octet-stream");toClient.write(buffer);toClient.flush();toClient.close();} catch (IOException ex) {ex.printStackTrace();}finally {try { // close input streamif (fis != null) {fis.close();}} catch(Exception ex) {}try { // close output streamif (toClient != null) {toClient.close();}} catch(Exception ex) {}}return response;}

批量文件下载(分目录)

@RequestMapping(value = "downloadZip", method = RequestMethod.POST)@ApiOperation(value = "批量文件下载")public void downloadZip(@RequestBody List<FileDto> fileDtos, HttpServletResponse response){String fileName = "download.zip";InputStream is = null;ZipOutputStream os = null;try {response.reset();response.addHeader("Access-Control-Allow-Origin", "*");response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");response.setContentType("application/octet-stream");response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));//设置下载的文件名称os = new ZipOutputStream(response.getOutputStream());byte[] buffer = new byte[1024*10];for (FileDto fileDto : fileDtos) {String downloadPath = cloud_root + fileDto.getPath();File file = new File(downloadPath + "/" +fileDto.getFileName());if (!file.exists() || !file.isFile()) {continue;}is = new FileInputStream(file);os.putNextEntry(new ZipEntry(fileDto.getPath() + "/" + file.getName()));int num = is.read(buffer);while (num > -1) {os.write(buffer, 0, num);num = is.read(buffer);}os.closeEntry();is.close();}os.close();}catch (Exception ex) {ex.printStackTrace();}finally {try { // close input streamif (is != null) {is.close();}}catch(Exception ex) {}try { // close output streamif (os != null) {os.close();}}catch(Exception ex) {}}}

批量下载这里有坑,另外一个项目批量下载的时候,没有目录层级,下载文件名相同的时候会报异常。

java.util.zip.ZipException: duplicate entry: ZP_3100192130_21585780_20191017114406.jpgat java.util.zip.ZipOutputStream.putNextEntry(ZipOutputStream.java:163)

vue axios 实现 文件流下载(前后端分离跨域问题的解决)相关推荐

  1. 前后端分离跨域问题解决方案

    问题 因为最近在学习vue和springboot.用到了前后端分离.前端webpack打包运行的时候会启动nodejs的服务器占用8080端口,后端springboot自带tomcat启动占用1111 ...

  2. 【python学习笔记】关于python Flask前后端分离跨域问题

    关于python Flask前后端分离跨域问题 前后端分离过程中,前后端对接测试难免遇到跨域问题.因为是个新司机,所以在我经过一天的测试,才找到解决办法=-= 第一种方法 from functools ...

  3. Springboot整合Shiro前后端分离跨域问题

    Springboot整合Shiro前后端分离跨域问题 前言:SpringBoot整合shiro进行前后端分离开发时(前端是Vue),项目做了跨域配置,但还是前端请求会出现cros err–显示的跨域问 ...

  4. pc网站调用微服务器,【微服务】前后端分离-跨域问题和解决方案

    跨域问题存在的原因 跨域问题的根本原因:因为浏览器收到同源策略的限制,当前域名的js只能读取同域下的窗口属性.什么叫做同源策略?就是不同的域名, 不同端口, 不同的协议不允许共享资源的,保障浏览器安全 ...

  5. nginx处理前后端分离跨域问题

    在微服务中,通常会使用前后端分离的方式进行开发和部署.由于前后端分开部署,属于不同的"资源",因此前端调用后端API时可能会出现跨域问题,Cross-Origin Resource ...

  6. cors 前后端分离跨域问题_前后端分离之CORS跨域访问踩坑总结

    前言 前后端分离的开发模式越来越流行,目前绝大多数的公司与项目都采取这种方式来开发,它的好处是前端可以只专注于页面实现,而后端则主要负责接口开发,前后端分工明确,彼此职责分离,不再高度耦合,但是由于这 ...

  7. 前后端分离跨域问题Access to XMLHttpRequest at ‘http://localhos...has been blocked by CORS policy: No ‘Access-

    完整报错如下: Access to XMLHttpRequest at 'http://localhost:8081/login' from origin 'http://localhost:8084 ...

  8. springboot+Vue项目-微博留言(前后端分离,跨域)

    所用技术 数据库:mysql 后台框架:springboot,mybatis plus 前台框架:Vue 实体类:lombok 异步:axios 一丶微博留言后端 小贴士:约定>配置>编码 ...

  9. nginx配置反向代理解决前后端分离跨域问题

    2019独角兽企业重金招聘Python工程师标准>>> 摘自<AngularJS深度剖析与最佳实践>P132 nginx配置文件如下: server {listen 80 ...

最新文章

  1. win2003辅助域服务器相关几个错误日志的解决办法
  2. 1000亿个整数,请找出其中最大的100个
  3. 配置JDK时发生'javac'不是内部或外部命令的现象与解决过程
  4. 用反向传导分子模型去计算基团的定位效应
  5. springMVC笔记day01
  6. (chap2 简单的Http协议) HTTP性能优化之管线化pipelining
  7. 批次管理相关事务代码
  8. 插入函数c语言,线性表的插入函数
  9. Windows使用筛选器来处理异常
  10. ASP.NET MVC下的四种验证编程方式[续篇]
  11. 怎样王远端服务器上传文件,传王电子传真使用指南-Freefax传真服务器,传王A6,免费传真...
  12. 多个DataTable的合并成一个新表
  13. Python进程间通信之管道Pipe
  14. 软件技术方案模板_携手跨越,法本信息数字化解决方案赋能企业,共建数字未来...
  15. java OA开源办公系统源码下载
  16. Xshell4、Xftp4注册码
  17. 网络安全工程师面试题合集(不全,暂不整理了)
  18. python之列表详解
  19. 【收集】Oracle官网账号
  20. 月薪40K+银行测试经理,自动化测试实践经验分享

热门文章

  1. 【文学文娱】《人间七月是晴天》
  2. 扎心话题 | 设计院背后的潜规则你知道吗?
  3. spotlight重启
  4. python网络爬虫权威指南 豆瓣_《Python网络爬虫权威指南第2版》相关学习资料和经验...
  5. Activiti的基本介绍
  6. 【activiti】activiti环境配置
  7. 推荐系统[一]:超详细知识介绍,一份完整的入门指南,解答推荐系统相关算法流程、衡量指标和应用,以及如何使用jieba分词库进行相似推荐,业界广告推荐技术最新进展
  8. AI+影像赛道开启,美图在人工智能领域如何「名利双收」?
  9. 华北科技学院 嵌入式实训知识点
  10. flutter PageView 禁止左右滚动