位置: IT常识 - 正文

springboot:各种下载文件的方式(springboot常用)

编辑:rootadmin
springboot:各种下载文件的方式 文章目录springboot:各种下载文件的方式一、使用response输出流下载二、使用ResponseEntity三、注意springboot:各种下载文件的方式一、使用response输出流下载

推荐整理分享springboot:各种下载文件的方式(springboot常用),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:spring springboot,springboot gui,spring springboot,springboot详细讲解,springboot gui,springboot详细讲解,spring springboot,springboot详细讲解,内容如对您有帮助,希望把文章链接给更多的朋友!

springboot:各种下载文件的方式(springboot常用)

注意第一种方式返回值必须为void

@GetMapping("/t1") public void down1(HttpServletResponse response) throws Exception { response.reset(); response.setContentType("application/octet-stream;charset=utf-8"); response.setHeader( "Content-disposition", "attachment; filename=test.png"); try( BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\desktop\\1.png")); // 输出流 BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream()); ){ byte[] buff = new byte[1024]; int len = 0; while ((len = bis.read(buff)) > 0) { bos.write(buff, 0, len); } } }二、使用ResponseEntity @GetMapping("/t2") public ResponseEntity<InputStreamResource> down2() throws Exception { InputStreamResource isr = new InputStreamResource(new FileInputStream("E:\\desktop\\1.png")); return ResponseEntity.ok() .contentType(MediaType.APPLICATION_OCTET_STREAM) .header("Content-disposition", "attachment; filename=test1.png") .body(isr); } @GetMapping("/t3") public ResponseEntity<ByteArrayResource> down3() throws Exception { byte[] bytes = Files.readAllBytes(new File("E:\\desktop\\1.png").toPath()); ByteArrayResource bar = new ByteArrayResource(bytes); return ResponseEntity.ok() .contentType(MediaType.APPLICATION_OCTET_STREAM) .header("Content-disposition", "attachment; filename=test2.png") .body(bar); }三、注意

后端使用前三种的一种方式,请求方式使用非GET请求,前端使用Blob类型接收

某些情况下,在下载时需要向后端POST一些参数,这时需要前端做一定配合,将接收类型设定为Blob

@PostMapping("/t4") public ResponseEntity<ByteArrayResource> down4(String fileName, @RequestBody Map data) throws Exception { System.out.println(data); byte[] bytes = Files.readAllBytes(new File("E:\\desktop\\1.png").toPath()); ByteArrayResource bar = new ByteArrayResource(bytes); return ResponseEntity.ok() .contentType(MediaType.APPLICATION_OCTET_STREAM) .header("Content-disposition", "attachment; filename=test.png") .body(bar); }

前端代码(这里使用了原生的ajax):

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <script> function download() { var ajax = new XMLHttpRequest(); ajax.withCredentials = true; ajax.responseType = "blob"; const fileName = "ttt.txt"; ajax.open('post','http://localhost:7901/demo/down/file/t4?fileName=' + fileName); ajax.setRequestHeader("Content-Type","application/json;charset=utf-8"); // ajax.setRequestHeader("Accept","application/json;charset=utf-8"); ajax.send(JSON.stringify({firstName:"Bill", lastName:"Gates", age:62, eyeColor:"blue"})); ajax.onreadystatechange = function () { if (ajax.readyState==4 &&ajax.status==200) { console.log(ajax.response); const href = URL.createObjectURL(ajax.response); const a = document.createElement('a'); a.setAttribute('href', href); a.setAttribute('download', fileName); a.click(); URL.revokeObjectURL(href); } } } </script></head><body> <input type="button" value="下载" onclick="download();"/></body></html>
本文链接地址:https://www.jiuchutong.com/zhishi/288007.html 转载请保留说明!

上一篇:小程序怎么自定义导航栏,导航栏放图片、设置高度(小程序怎么自定义tabbar)

下一篇:最新接口的固态硬盘是什么(固态硬盘最新接口)

  • vivos7和s6的区别有哪些(vivos7与s6参数配置对比)

  • iphonese2来电闪光如何设置(iphonese2来电闪光灯设置)

  • iphone7p耳机怎么连接手机(苹果7怎么耳机)

  • 为了您的微信安全暂不能绑定手机(为了您的微信安全暂时不能绑定手机)

  • vivoS1照片怎么旋转过来(vivo手机照片怎样旋转)

  • kindle的书删了还看得见(kindle不小心删掉书)

  • 变频启动和软启动的区别(变频启动和软启动的电机的启动电流一样吗)

  • 微信群发红包怎样发平均(微信群发红包怎么发给指定人)

  • qq照片墙为什么上传不了照片(qq上照片墙为什么显示不出来)

  • 微信好友被拉黑无法申诉(微信好友被拉黑怎么加回来)

  • 微信号注销了还能搜索到吗(微信号注销了还能查到个人信息吗)

  • iphone11感觉字体不清晰(苹果11字体太大已经调不回去了怎么办)

  • 华为beam功能是什么(华为bey)

  • ipadmini1可以升级到什么版本(ipadmini1可以升级吗)

  • 京东配送丢件怎么处理(京东配送丢件怎么办)

  • rf输入接口是什么(rf输入接口是什么信号源)

  • nova5有呼吸灯吗(nova5呼吸灯在哪)

  • 微信账单删了怎么恢复(微信账单删了怎么联系收款方)

  • 抖音抢镜窗口怎么调大小(抖音抢镜窗口怎么设置)

  • 苹果a1602什么版本(苹果型号a1602)

  • vivo手机流量设置在哪里(vivo手机流量怎么设置)

  • 电池开票属于哪一项(电池开票属于哪类编码)

  • 苹果11多少寸(苹果11多少寸的屏幕)

  • 苹果xs3dtouch怎么使用

  • 荣耀9x国内发布时间(荣耀9x 发布)

  • 荣耀10nfc感应区在哪(荣耀10nfc感应区在哪里图解)

  • images是什么文件夹(images是什么格式)

  • hhw.exe是什么进程 是什么作用 hhw进程查询(nw.exe是什么进程)

  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设 电脑维修 湖南楚通运网络