[精讚] [會員登入]
622

download a file from spring boot controllers

ownload a file from spring boot controllers

分享此文連結 //n.sfs.tw/11168

分享連結 download a file from spring boot controllers@igogo
(文章歡迎轉載,務必尊重版權註明連結來源)
2021-01-07 14:00:27 最後編修
2017-06-03 15:25:18 By igogo
 

 

建一個static 文件夾, 位置如圖, 把example.pdf 放進去

 

 

 

 

 @RequestMapping(path = "/download", method = RequestMethod.GET)
    public ResponseEntity<?> download(String param) throws IOException {
        File file = new File("static/報名簡章.pdf");
        if (file.exists()) {
            logger.info(file.getAbsolutePath());
        } else {
            logger.info("no");
        }

        Path path = Paths.get(file.getAbsolutePath());
        ByteArrayResource resource = new ByteArrayResource(Files.readAllBytes(path));

        HttpHeaders headers = new HttpHeaders();
        String filename = new String("報名簡章.pdf".getBytes("UTF-8"), "ISO_8859_1");
        
        headers.setContentDispositionFormData("attachment", filename);
        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(file.length())
                .contentType(MediaType.parseMediaType("application/pdf"))
                .body(resource);

    }

 

參考

https://stackoverflow.com/questions/35680932/download-a-file-from-spring-boot-rest-service/35683261#35683261

https://stackoverflow.com/questions/5673260/downloading-a-file-from-spring-controllers

關於java String 

String objects in Java don't have an encoding (*).

The only thing that has an encoding is a byte[].

https://stackoverflow.com/questions/5729806/encode-string-to-utf-8

END

你可能感興趣的文章

spring security 使用MD5 hash 認證 spring security 預設使用BCrypt , 但是舊的系統使用md5 hash, @Bean Passwor

spring boot 3 建立 Basic Authentication 參考以下網站, 建立一個http basic authentication 為例 https://www.geeksfo

download a file from spring boot controllers ownload a file from spring boot controllers

隨機好文

vue.js components 多個組件的呈現 vue.js 組件 component

00-F2 的 IPV6 反解設定 近日, 強者我的大神同事, line 傳來一句: igogo 上次你那個ipv6的設定檔 再幫我跑一次 如連結 程式都寫

臺中市雲端校務系統與Windows AD帳號整合(9) 3-2 Linux上隨系統開機啟動服務 以Centos 7 發行版為例 請注意, 此時提供服務的主机為Linux, 所以

[scratch2] 巢狀迴圈 有兩清單 一數字 一英文 想排出所以可能, 例如1a,1b,1c,2a,2b,2c...3c 利用巢狀迴圈 內圈累加的變

台中市網路應用競賽試場安排(一) 排試場就跟排班或是排課表一樣, 找不到一個都能讓所有人滿意的結果