我试图从URL读取多个文件(可以是任何格式,即pdf,txt,tiff等)并使用它们进行压缩ZipOutputStream.我的代码看起来像这样:
// using in-memory file read
// then zipping all these files in-memory
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
.....
URL url = new URL(downloadUrl); // can be multiple URLs
ByteArrayOutputStream bais = new ByteArrayOutputStream();
InputStream is = url.openStream();
byte[] byteChunk = new byte[4096];
int n;
while ( (n = is.read(byteChunk)) > 0 )
{
bais.write(byteChunk, 0, n);
}
byte[] fileBytes = bais.toByteArray();
ZipEntry entry = new ZipEntry(fileName);
entry.setSize(fileBytes.length);
zos.putNextEntry(entry);
zos.write(fileBytes);
zos.closeEntry();
// close the url input stream
is.close();
// close the zip output stream
zos.close();
// read the byte array from ByteArrayOutputStream
byte[] zipFileBytes = baos.toByteArray();
String fileContent = new String(zipFileBytes);
Run Code Online (Sandbox Code Playgroud)
然后我将此内容"fileContent"传递给我的perl前端应用程序.
我使用perl代码下载此压缩文件:
WPHTTPResponse::setHeader( 'Content-disposition', 'attachment; filename="test.zip"' );
WPHTTPResponse::setHeader( 'Content-type', 'application/zip');
print $result; // string coming from java application
Run Code Online (Sandbox Code Playgroud)
但它提供的zip文件已损坏.我认为数据转换出了问题.
我很感激任何帮助.
| 归档时间: |
|
| 查看次数: |
276 次 |
| 最近记录: |