最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot各種下載文件的方式匯總

 更新時間:2022年10月14日 11:34:45   作者:yololee_  
下載功能其實就是用戶輸入指定文件路徑信息,然后把文件返回給用戶,下面這篇文章主要給大家介紹了關于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類型接收

某些情況下,在下載時需要向后端POST一些參數(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>

總結

到此這篇關于springboot各種下載文件的文章就介紹到這了,更多相關springboot下載文件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

祁连县| 苗栗县| 五峰| 清苑县| 海盐县| 大邑县| 双流县| 桃江县| 石阡县| 章丘市| 襄汾县| 岚皋县| 日喀则市| 普格县| 吴旗县| 观塘区| 花垣县| 图们市| 彭山县| 兴化市| 青河县| 阿坝县| 九台市| 西吉县| 利津县| 璧山县| 湾仔区| 吉林省| 化州市| 双鸭山市| 淳化县| 泗洪县| 长泰县| 乐山市| 邹城市| 通渭县| 汽车| 汉沽区| 东至县| 靖安县| 南康市|