java 简单文件下载

代码

 //参数是需要下载的文件路径
 @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测试
在这里插入图片描述
浏览器测试
在这里插入图片描述
这辈子坚持与不坚持都不可怕,怕的是独自走在坚持的道路上!!!

猜你喜欢

转载自blog.csdn.net/taiguolaotu/article/details/112311885
今日推荐