springboot各種下載文件的方式匯總
一、使用response輸出流下載
注意第一種方式返回值必須為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類型接收
某些情況下,在下載時(shí)需要向后端POST一些參數(shù),這時(shí)需要前端做一定配合,將接收類型設(shè)定為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>
總結(jié)
到此這篇關(guān)于springboot各種下載文件的文章就介紹到這了,更多相關(guān)springboot下載文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)多人多牌數(shù)比較游戲
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)多人多牌數(shù)比較游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01java 將字符串、list 寫入到文件,并讀取內(nèi)容的案例
這篇文章主要介紹了java 將字符串、list 寫入到文件,并讀取內(nèi)容的案例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09Java使用集合實(shí)現(xiàn)斗地主分牌完整代碼
在斗地主游戲中,通常是將一副牌平均分成3份,每份17張牌,并留3張底牌,我們可以使用集合來實(shí)現(xiàn)這一功能,這篇文章主要給大家介紹了關(guān)于Java使用集合實(shí)現(xiàn)斗地主分牌的相關(guān)資料,需要的朋友可以參考下2024-05-05Java中stream.map和stream.forEach的區(qū)別
本文主要介紹了Java中stream.map和stream.forEach的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08