gpt4 book ai didi

java - Spring 启动: Download zipped large files while they are being processed on the server

转载 作者:行者123 更新时间:2023-12-02 10:35:30 25 4
gpt4 key购买 nike

在我的应用程序中,我正在压缩然后下载大型文件,这些文件位于 azure 中,所以我从流中读取文件,然后一个接一个地压缩它们,这样我就可以在所有文件都下载完毕后下载zip文件已压缩,这是我的代码:

@RequestMapping(value = "{analyseId}/download", method = RequestMethod.GET, produces = "application/zip")
public ResponseEntity<Resource> download(@PathVariable List<String> paths) throws IOException {

String zipFileName = "zipFiles.zip";
File zipFile = new File(zipFileName);
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
for (String path : paths) {
InputStream fis = azureDataLakeStoreService.readFile(path);
addToZipFile(path , zos, fis);
}
zos.close();
fos.close();
BufferedInputStream zipFileInputStream = new BufferedInputStream(new FileInputStream(zipFile.getAbsolutePath()));
InputStreamResource resource = new InputStreamResource(zipFileInputStream);
zipFile.delete();

return ResponseEntity.ok()
.header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + zipFileName)
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(resource);
}

private static void addToZipFile(String path, ZipOutputStream zos, InputStream fis) throws IOException {
ZipEntry zipEntry = new ZipEntry(FilenameUtils.getName(path));
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zos.write(bytes, 0, length);
}
zos.closeEntry();
fis.close();
}

但是,在 azure 上,请求超时设置为 230 秒,并且 cannot be changed ,但是对于大文件,在服务器上加载和压缩文件需要更多时间,因此与客户端的连接将同时丢失。

所以我的问题是,既然我从流中获取数据,我们是否可以同时执行所有这些操作,意味着获取流并同时下载它,而不是等到获取整个文件,或者如果有的话其他想法可以请任何人在这里分享。

谢谢。

最佳答案

答案是不要将文件下载到服务器,然后将其发送到客户端,而是直接将其流式传输到客户端,这是代码

@RequestMapping(value = "/download", method = RequestMethod.GET)
public StreamingResponseBody download(@PathVariable String path) throws IOException {

final InputStream fecFile = azureDataLakeStoreService.readFile(path);
return (os) -> {
readAndWrite(fecFile, os);
};
}

private void readAndWrite(final InputStream is, OutputStream os)
throws IOException {
byte[] data = new byte[2048];
int read = 0;
while ((read = is.read(data)) >= 0) {
os.write(data, 0, read);
}
os.flush();
}

我还将此配置添加到 ApplicationInit 中:

@Configuration
public static class WebConfig extends WebMvcConfigurerAdapter {

@Override
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
configurer.setDefaultTimeout(-1);
configurer.setTaskExecutor(asyncTaskExecutor());
}

@Bean
public AsyncTaskExecutor asyncTaskExecutor() {
return new SimpleAsyncTaskExecutor("async");
}

}

关于java - Spring 启动: Download zipped large files while they are being processed on the server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53318057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com