java simple file download

Code

 //参数是需要下载的文件路径
 @GetMapping("/download")
    public ResponseEntity<byte[]> documentList(@RequestParam String documentAccessory) throws IOException {
        byte[] data = Files.readAllBytes(Paths.get(documentAccessory));
        String fillName = documentAccessory.substring(documentAccessory.lastIndexOf("/") + 1);
        HttpHeaders headers = new HttpHeaders();

        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentLength(data.length);
        headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode(fillName, "UTF-8"));
        return new ResponseEntity<>(data, headers, HttpStatus.OK);
    }

Postman test,
Insert picture description here
browser test,
Insert picture description here
persistence or non-persistence in this life is not terrible, I am afraid that I will be on the road of persistence alone! ! !

Guess you like

Origin blog.csdn.net/taiguolaotu/article/details/112311885