我创建了一个休息端点,在其中提供文件名,服务从S3获取该文件并将其下载给用户。 几乎适用于所有文件。
当我们尝试下载<1kb的文件时,它不会下载,并且chrome显示错误:“失败-没有文件”
在后端服务器或客户端都没有错误。 客户端是用Angular编写的。
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Path("/downloadCsvFile")
public void downloadCsvFile(@Context HttpServletRequest request, @Context HttpServletResponse response,
@QueryParam("fileName") final String fileName) {
response.setContentType("application/csv");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName);
response.setHeader("Transfer-Encoding", "Chunked");
response.setHeader("Content-Description", "File Transfer");
try {
final InputStream inputStream = new BufferedInputStream(AwsS3Utils.getObject().getFileStram(fileName));
// Copy bytes from source to destination(outputstream in this example), closes
// both streams.
FileCopyUtils.copy(inputStream, response.getOutputStream());
LOG.info("Downloading CSV file");
} catch (final Exception e) {
LOG.error("Exception occured while downloading csv file for admin report", e);
}
}
答案 0 :(得分:0)
如果客户端不知道该扩展名,则会显示"Failed - no file"
错误。
请检查您的扩展名。 '.'
之后,客户端应该知道它,例如.txt,.csv等
答案 1 :(得分:0)
解决了。
问题出在FileCopyUtils.copy方法上。 跳过了彼此复制流的部分。
新代码:
final InputStream inputStream = new BufferedInputStream(AwsS3Utils.getObject().getFileStram(fileName));
final ResponseBuilder response = Response.ok(inputStream);
response.header("Content-Disposition", "attachment;filename=" + fileName); response.header("Content-Type", "application/csv");
return response.build();