【问题标题】:To make a download functionality to download files from the server using spring boot使用spring boot创建下载功能以从服务器下载文件
【发布时间】:2020-06-19 05:45:49
【问题描述】:

我想创建一个下载功能,它必须使用 spring boot 从服务器下载文件。 文件的大小限制应为 1gb,我应该可以选择将其下载为 zip 文件

【问题讨论】:

    标签: spring-boot download


    【解决方案1】:

    你好下载任何类型的文件都可以使用下面的方法。您必须在应用之前配置好路径。

     //descargar archivo
            @Secured({ "ROLE_ADMIN", "ROLE_USER","ROLE_SADMIN","ROLE_USERGESTSERV"})
             @RequestMapping(value="/downloadFile/{filename:.+}")
                public void getLogFile(HttpSession session,HttpServletResponse response,@PathVariable String filename) throws Exception { 
                 try {              
                        final Logger log = LoggerFactory.getLogger(getClass());
    
                        Path pathFoto = getPath(filename);
                        log.info("pathFoto: " + pathFoto);
                        Resource recurso = new UrlResource(pathFoto.toUri());     
                        File fileToDownload = new File(pathFoto.toString());
                        InputStream inputStream = new FileInputStream(fileToDownload);
    
                        response.setContentType("application/force-download");
                       response.setHeader("Content-Disposition", "attachment;recurso"); 
                    IOUtils.copy(inputStream, response.getOutputStream());
                        response.flushBuffer();
                        inputStream.close();
                    } catch (Exception exception){
                        System.out.println(exception.getMessage());
                    }
    
                }
    

    【讨论】:

      猜你喜欢
      • 2018-02-09
      • 2016-06-11
      • 2015-04-09
      • 2022-10-14
      • 1970-01-01
      • 1970-01-01
      • 2015-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多