各位大虾,我现在是要把一些文本文件打包压缩后下载,测试后发现似乎文件个数多了以后会出现 closed connection的例外,自己定位在zf.closeEntry()上,
万望指教! 下面是完整的代码:
public static void creatZipFile(
HttpServletResponse response,
HttpServletRequest request,
List dataList,
boolean fileType,
String zipFileName)
throws FileUpDownException {
ServletOutputStream outputStream = null;
ZipOutputStream zf = null;
try {
outputStream = response.getOutputStream();
zf = new ZipOutputStream(outputStream);
zipFileName = new String(zipFileName.getBytes(), "ISO-8859-1");
response.setContentType("application/zip"); response.setHeader("Pragma", "private");
response.setHeader("Cache-Control", "max-age=0");
response.setHeader("Content-Disposition","attachment;filename=" + zipFileName); zf.setLevel(Deflater.BEST_COMPRESSION);
for (int i = 0; i < dataList.size(); i++) {
Map fileList = (Map)dataList.get(i);
String fileName = (String)fileList.get("FileName");
ZipEntry entry = new ZipEntry(fileName);
entry.setTime(System.currentTimeMillis());
zf.putNextEntry(entry);
if (fileList.get("TempletFilePath") == null) {
List colList = null;
if (fileList.get("ColumList") != null) {
colList = (List)fileList.get("ColumList");
}
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
List fileDataList = (List)fileList.get("FileDataList");
outputBasicData(
colList,
fileDataList,
new PrintStream(buffer));
zf.write(buffer.toByteArray());
zf.closeEntry();
} else {
String templetFilePath =
(String)fileList.get("TempletFilePath");
File file = new File(templetFilePath + "\\" + fileName);
if (file.exists()) {
FileInputStream fileIn = new FileInputStream(file);
byte[] buffer = new byte[4096];
while (fileIn.read(buffer) != -1) {
zf.write(buffer);
}
zf.closeEntry();
fileIn.close();
}
} }
zf.flush();
zf.close();
} catch (IOException e) {
throw new FileUpDownException(e);
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
if (zf != null) {
zf.close();
}
} catch (IOException e1) {
// do nothing
}
} }
万望指教! 下面是完整的代码:
public static void creatZipFile(
HttpServletResponse response,
HttpServletRequest request,
List dataList,
boolean fileType,
String zipFileName)
throws FileUpDownException {
ServletOutputStream outputStream = null;
ZipOutputStream zf = null;
try {
outputStream = response.getOutputStream();
zf = new ZipOutputStream(outputStream);
zipFileName = new String(zipFileName.getBytes(), "ISO-8859-1");
response.setContentType("application/zip"); response.setHeader("Pragma", "private");
response.setHeader("Cache-Control", "max-age=0");
response.setHeader("Content-Disposition","attachment;filename=" + zipFileName); zf.setLevel(Deflater.BEST_COMPRESSION);
for (int i = 0; i < dataList.size(); i++) {
Map fileList = (Map)dataList.get(i);
String fileName = (String)fileList.get("FileName");
ZipEntry entry = new ZipEntry(fileName);
entry.setTime(System.currentTimeMillis());
zf.putNextEntry(entry);
if (fileList.get("TempletFilePath") == null) {
List colList = null;
if (fileList.get("ColumList") != null) {
colList = (List)fileList.get("ColumList");
}
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
List fileDataList = (List)fileList.get("FileDataList");
outputBasicData(
colList,
fileDataList,
new PrintStream(buffer));
zf.write(buffer.toByteArray());
zf.closeEntry();
} else {
String templetFilePath =
(String)fileList.get("TempletFilePath");
File file = new File(templetFilePath + "\\" + fileName);
if (file.exists()) {
FileInputStream fileIn = new FileInputStream(file);
byte[] buffer = new byte[4096];
while (fileIn.read(buffer) != -1) {
zf.write(buffer);
}
zf.closeEntry();
fileIn.close();
}
} }
zf.flush();
zf.close();
} catch (IOException e) {
throw new FileUpDownException(e);
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
if (zf != null) {
zf.close();
}
} catch (IOException e1) {
// do nothing
}
} }
解决方案 »
- [业内传闻]java太简单,学java全是垃圾,天天用别人的API,自己就是个民工.
- 仿造WindowAdapter写了一个程序,为啥不成功呢
- String str = textfield1.getText(); 这里str值不为空啊?textfield框内可是空的
- repaint和paintComponent的问题,急~~~
- 如何把String类型转换成int类型?
- 各位大侠们,帮我们OK一下两个问题,只要进来分数都是100分
- {共享}我有 Thinking in Java 中 英 版的电子书 要的报名
- 关于unicode的问题,请大侠指教
- 一个关于组合框的奇怪问题,希望论坛的各位朋友指点!!
- 明天我去应聘----大家帮我出出注意
- 关于String问题。
- 请问如何在java中执行sql存储过程,或者是sql脚本文件?
List fileDataList = (List)fileList.get("FileDataList");
outputBasicData(colList,fileDataList,new PrintStream(buffer));
zf.write(buffer.toByteArray());
zf.closeEntry();
这些代码在for循环里面,为什么前几次zf.closeEntry()都OK,后来就出exception了呢
执行两次
在if里面就不要写这句了