[精讚] [會員登入]
338

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 boot 3 建立 Basic Authentication 參考以下網站, 建立一個http basic authentication 為例 https://www.geeksfo

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

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

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

centos 7 移机出現 dracut-initqueue timeout centos 7 移机出現 dracut-initqueue timeout 處理

windows ad 如何得知 dn 值 如何得知 windows ad 上的使用者dn 值 https://support.symantec.com/en_US

scratch3 計算得分排名 scratch3 得分排名

對照mac address 批次更改電腦名稱 讀取mac address 對照表, 還原電腦後,自動更改密碼 'show logged on user Dim

停止多執行緒 利用主程式呼叫多執行緒時, 要怎麼停止正在執行的多執行緒