Java 例外:流在下载多个附件时关闭
我有一个要求,一个用户可以上传一个文件,然后如果其他用户看到他可以下载一个文件。新的要求表明,现在一个用户可以上传多个附件,任何看到它的用户也可以下载多个附件 所以我拿了一个列表,其中添加了附件并将其定向到下载控制器,我更改了前面的行并保留了一个for循环,但在下载过程中只下载了第一个附件,然后关闭了异常流。下面是控制器的代码。请让我知道如何克服这个问题Java 例外:流在下载多个附件时关闭,java,spring-boot,Java,Spring Boot,我有一个要求,一个用户可以上传一个文件,然后如果其他用户看到他可以下载一个文件。新的要求表明,现在一个用户可以上传多个附件,任何看到它的用户也可以下载多个附件 所以我拿了一个列表,其中添加了附件并将其定向到下载控制器,我更改了前面的行并保留了一个for循环,但在下载过程中只下载了第一个附件,然后关闭了异常流。下面是控制器的代码。请让我知道如何克服这个问题 @ApiOperation(value = "Download content") @RequestMapping(value = "/api
@ApiOperation(value = "Download content")
@RequestMapping(value = "/api/content/{id}/download/", method = RequestMethod.GET)
public ResponseEntity<String> downloadContent(HttpServletResponse response, @PathVariable("id") final Long id)
throws IOException, APIException {
Content content = null;
try {
content = this.contentService.get(this.contentUtils.getContentObject(id));
} catch (ServiceException e) {
throw new APIException("Access denied");
}
if (null == content) {
throw new APIException("Invalid content id");
}
List<Document> documentList = this.contentService.getDocumentByContent(content);
if (documentList != null && !documentList.isEmpty()) {
//Document document = documentList.get(0); //If multiple files supported?, then need to be handled here
for (Document document : documentList) {
File file = new File(document.getLocalFilePath());
if (file.exists()) {
response.setHeader("Content-Disposition", "attachment;filename=\"" + file.getName() + "\"");
try (InputStream inputStream = new FileInputStream(file); ServletOutputStream sos = response.getOutputStream();) {
IOUtils.copy(inputStream, sos);
} catch (final IOException e) {
LOGGER.error("File not found during content download" + id, e);
throw new APIException("Error during content download:" + id);
}
} else {
try {
s3FileUtil.download(document.getS3Url(), document.getLocalFilePath());
} catch (S3UtilException e) {
throw new APIException("Document not found");
}
}
}
} else {
//404
return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<String>(HttpStatus.OK);
}
@ApiOperation(value=“下载内容”)
@RequestMapping(value=“/api/content/{id}/download/”,method=RequestMethod.GET)
公共响应下载内容(HttpServletResponse,@PathVariable(“id”)最终长id)
抛出IOException,APIException{
Content=null;
试一试{
content=this.contentService.get(this.contentUtils.getContentObject(id));
}捕获(服务异常e){
抛出新异常(“访问被拒绝”);
}
if(null==内容){
抛出新的APIException(“无效内容id”);
}
List documentList=this.contentService.getDocumentByContent(内容);
if(documentList!=null&&!documentList.isEmpty()){
//Document Document=documentList.get(0);//如果支持多个文件,则需要在此处处理
用于(文档:文档列表){
File File=新文件(document.getLocalFilePath());
if(file.exists()){
response.setHeader(“内容处置”、“附件;文件名=\”+文件.getName()+”\”);
try(InputStream InputStream=newfileinputstream(file);ServletOutputStream sos=response.getOutputStream();){
IOUtils.copy(输入流,sos);
}捕获(最终IOE例外){
LOGGER.error(“内容下载期间未找到文件”+id,e);
抛出新的APIException(“内容下载时出错:+id”);
}
}否则{
试一试{
下载(document.getS3Url(),document.getLocalFilePath());
}捕获(S3E){
抛出新的APIException(“未找到文档”);
}
}
}
}否则{
//404
返回新的ResponseEntity(未找到HttpStatus.NOT_);
}
返回新的响应状态(HttpStatus.OK);
}
实际上,您无法一次下载所有文件。因为,一旦打开流并将文件内容写入流,就必须关闭流。
在
for
循环中添加文件时,必须附加文件内容而不是文件,这不是预期的行为。
当您想一次下载多个文件时,必须zip 下载文件 检查此链接: