1. @GetMapping("/download")
    2. public void downloadFile(String fileName) throws IOException {
    3. BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
    4. response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
    5. File file = new File(
    6. System.getProperty(Constant.USER_DIR) + Constant.SPLIT
    7. + Constant.UPLOAD_DIR + Constant.SPLIT + fileName);
    8. InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
    9. try (FileInputStream in = new FileInputStream(file);) {
    10. int len = 0;
    11. //创建数据缓冲区
    12. byte[] buffer = new byte[1024];
    13. //通过response对象获取outputStream流
    14. //将FileInputStream流写入到buffer缓冲区
    15. while ((len = in.read(buffer)) > 0) {
    16. //使用OutputStream将缓冲区的数据输出到浏览器
    17. out.write(buffer, 0, len);
    18. }
    19. } catch (Exception e) {
    20. System.out.println(e);
    21. }
    22. out.flush();
    23. }