【发布时间】:2020-06-19 05:45:49
【问题描述】:
我想创建一个下载功能,它必须使用 spring boot 从服务器下载文件。 文件的大小限制应为 1gb,我应该可以选择将其下载为 zip 文件
【问题讨论】:
标签: spring-boot download
我想创建一个下载功能,它必须使用 spring boot 从服务器下载文件。 文件的大小限制应为 1gb,我应该可以选择将其下载为 zip 文件
【问题讨论】:
标签: spring-boot download
你好下载任何类型的文件都可以使用下面的方法。您必须在应用之前配置好路径。
//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());
}
}
【讨论】: