我试图将zip文件作为输出供用户下载。我创建了一个zipOutputStream ..并尝试将其发送到vertx http响应
这怎么下载一个损坏的zip文件。或挂起
public void download(RoutingContext routingContext) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos)) {
for (int i = 0; i < 2; i++) {
String fileName = "test" + i + ".csv";
File tempFile = createTempFile("test" + i);
tempFile.deleteOnExit();
InputStream is = new FileInputStream(tempFile);
byte[] buffer = new byte[is.available()];
is.read(buffer);
ZipEntry entry = new ZipEntry(fileName);
zos.putNextEntry(entry);
try {
zos.write(buffer, 0, buffer.length);
zos.closeEntry();
String b64String = Base64.encodeBase64String(baos.toByteArray());
routingContext.response()
.setChunked(true)
.putHeader(HttpHeaders.CONTENT_TYPE, "application/zip")
.putHeader("Content-Disposition", "attachment; filename=\"test.zip\"")
.putHeader(HttpHeaders.TRANSFER_ENCODING, "chunked")
.putHeader(HttpHeaders.CONTENT_LENGTH, Long.toString(b64String.length()));
routingContext.response().write(b64String);
routingContext.response().end();
routingContext.response().close();
} finally {
baos.close();
}
}
}
catch(Exception ex) {
}
}
答案 0 :(得分:0)
我制定了解决方案。我将vertx httpResponse包装在outputStream中,并将其推送到zipOutputStream中。它通过将chunked设置为true来实现。
答案 1 :(得分:0)
对于非常大的文件,可以使用流传输来避免将它们完全加载到RAM中:
Tesseract git repository located in C:\opencv\tesseract
Tesseract build directory C:\opencv\tesseract\build
Leptonica repository located in C:\opencv\leptonica
Leptonica build directory in C:\opencv\leptonica\build (contains LeptonicaConfig.cmake,
Leptonica library files, (libleptonica.so, libleptonica.so.1.77.0, libleptonica.so.5.3.0) are in C:\opencv\leptonica\build\src
不幸的是,AsyncInputStream(用于InputStream到ReadStream的包装)尚未成为VertX框架的一部分,因此您必须自己实现它或找到一些开源实现。