java项目中实现定时刷新,如何在Java中定期刷新ZipOutputStream

I am trying to archive list of files in zip format and then downloading it for the user on the fly...

I am facing out of memory issue when downloading a zip of 1gb size

Please help me how i can resolve this without increasing jvm heap size. i would like to flush the stream periodically..

I AM TRYING TO FLUSH PERIODICALLY BUT THIS IS NOT WORKING FOR ME.

Please find my code attached below:

try{

ServletOutputStream out = response.getOutputStream();

ZipOutputStream zip = new ZipOutputStream(out);

response.setContentType("application/octet-stream");

response.addHeader("Content-Disposition",

"attachment; filename=\"ResultFiles.zip\"");

//adding multiple files to zip

ZipUtility.addFileToZip("c:\\a", "print1.txt", zip);

ZipUtility.addFileToZip("c:\\a", "print2.txt", zip);

ZipUtility.addFileToZip("c:\\a", "print3.txt", zip);

ZipUtility.addFileToZip("c:\\a", "print4.txt", zip);

zip.flush();

zip.close();

out.close();

} catch (ZipException ex) {

System.out.println("zip exception");

} catch (Exception ex) {

System.out.println("exception");

ex.printStackTrace();

}

public class ZipUtility {

static public void addFileToZip(String path, String srcFile,

ZipOutputStream zip) throws Exception {

File file = new File(path + "\\" + srcFile);

boolean exists = file.exists();

if (exists) {

long fileSize = file.length();

int buffersize = (int) fileSize;

byte[] buf = new byte[buffersize];

int len;

FileInputStream fin = new FileInputStream(path + "\\" + srcFile);

zip.putNextEntry(new ZipEntry(srcFile));

int bytesread = 0, bytesBuffered = 0;

while ((bytesread = fin.read(buf)) > -1) {

zip.write(buf, 0, bytesread);

bytesBuffered += bytesread;

if (bytesBuffered > 1024 * 1024) { //flush after 1mb

bytesBuffered = 0;

zip.flush();

}

}

zip.closeEntry();

zip.flush();

fin.close();

}

}

}

}

解决方案

You want to use chunked encoding to send a file that large otherwise the servlet container will try and figure out the size of the data you are trying to send before sending it so it can set the Content-Length header. Since you are compressing files you don't know the size of the data you're sending. Chunked-Encoding allows you to send pieces of the response in smaller chunks. Don't set the content length of the stream. You might try using curl or something to see the HTTP headers in the response your getting from the server. If it isn't chunked then you'll want to figure that out. You'll want to research how to force the servlet container to send chunked encoding. You might have to add this to the response header to make the servlet container send it chunked.

response.setHeader("Transfer-Encoding", "chunked");

The other option would be to compress the file into a temporary file with File.createTemp(), and then send the contents of that. If you compress to a temp file first then you can know how big the file is and set the content length for the servlet.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值