我有一个zip文件作为字节数组(byte []),我可以使用
将其写入文件系统 FileOutputStream fos = new FileOutputStream("C:\\test1.zip");
fos.write(decodedBytes); // decodedBytes is the zip file as a byte array
fos.close();
我想将字节数组直接下载,而不是将其写入文件并将其读取为下载文件, 我试过这个,
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=\"File.zip\"");
ServletOutputStream outStream = response.getOutputStream();
outStream.write(decodedBytes); // decodedBytes is the zip file as a byte array
这不起作用,我得到空文件。如何将字节数组作为下载?
更新 我添加了finally子句并关闭了ServletOutputStream并且它工作正常。
}catch (Exception e) {
Log.error(this, e);
} finally {
try{
if (outStream != null) {
outStream.close();
}
} catch (IOException e) {
Log.error(this, "Download: Error during closing resources");
}
}
Pankaj解决方案也有效。
答案 0 :(得分:3)
尝试以下:
ServletOutputStream outStream = response.getOutputStream();
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename="DATA.ZIP"");
outStream.write(decodedBytes);
outStream.flush();