[精讚] [會員登入]
298

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

你可能感興趣的文章

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

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

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

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

vue.js modal 作兩個選項按鈕並導向不同頁面 vue.js modal 作兩個選項按鈕

axios vuejs application/x-www-form-urlencoded 送資料 VUE.JS 以 application/x-www-form-urlencoded 送資料

Arrays.asList 後想再add出現 UnsupportedOperationException 這個問題真是搞死我了 List<String> fruits = Arrays.asList("a

臺中市校務雲端系統與Windows AD帳號整合(1) active directory,ldaps,雲端校務系統