[精讚] [會員登入]
478

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

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

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

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

2018 hoc 掃地機器人 掃地機器人只能打掃沒有障礙物(桌椅、牆壁)的範圍,請寫程式控制機器人打掃餐廳的所有走道, 並在清掃完畢後回到充電器。

[scratch2] 分數排名 在清單中隨机產生5名學生的考試分數, 再利用另一個清單排名 想法, 分數愈高者排名愈好, 例如名次是第5名, 那分數是最

[scratch] 將角色物件放到清單中,並依序讀出每個角色的X值 將角色物件放到清單中,並依序讀出每個角色的X值

台中市校園空氣品質預警 aqi

[web] 自訂模組-打字機與跑馬燈效果 自訂模組-打字機效果