从web url
获取数据到inputStream对象inputStream = AWSFileUtil.getInputStream(
AWSConnectionUtil.getS3Object(null),
"cdn.generalsentiment.com", filePath);
如果它们是多个文件,那么我想压缩它们并将文件类型作为“zip”发送到执行下载的struts.xml。
实际上是将输入流转换为byteArrayInputStream
ByteArrayInputStream byteArrayInputStream = new
ByteArrayInputStream(inputStream.toString().getBytes());
while (byteArrayInputStream.read(inputStream.toString().getBytes()) > 0) {
zipOutputStream.write(inputStream.toString().getBytes());
}
然后
zipOutputStream.close();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
fileInputStream = new FileInputStream(file);
while (fileInputStream.read(buffer) > 0) {
byteArrayOutputStream.write(buffer);
}
byteArrayOutputStream.close();
inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
reportName = "GS_MediaValue_Reports.zip";
fileType = "zip";
}
return fileType;
但是提取时下载的zip会提供损坏的文件。 请建议我解决这个问题。
答案 0 :(得分:0)
简短的回答是ZipOutputStream
的工作原理并非如此。由于它旨在存储多个文件,以及它们的文件名,目录结构等,因此您需要明确地告知流。
此外,将流转换为字符串通常是一个坏主意,加上它很慢,尤其是当你在循环中进行时。
所以你的解决方案就像:
ZipEntry entry = new ZipEntry( fileName ); // You have to give each entry a different filename
zipOutputStream.putNextEntry( entry );
byte buffer[] = new byte[ 1024 ]; // 1024 is the buffer size here, but it could be anything really
int count;
while( (count = inputStream.read( buffer, 0, 1024 ) ) != -1 ) {
zipOutputStream.write( buffer, 0, count );
}