压缩两个文件到指定目录下,压缩成zip文件,要求用打开,保存的模式response给客户端。 public static void createZipFile(HttpServletResponse response) throws Exception { String zipPath = "e:/zip/test.zip";
List<String> filePaths = new ArrayList<String>();
filePaths.add("e:/zip/aa.txt");
filePaths.add("e:/zip/bb.txt");
File zipFile = new File(zipPath);
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + zipFile.getName());
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
ZipOutputStream webZos = null;
ServletOutputStream out = null;
try {
out = response.getOutputStream();
webZos = new ZipOutputStream(new BufferedOutputStream(out));
for (String filePath : filePaths) {
File file = new File(filePath);
ZipEntry ze = new ZipEntry(file.getName());
zos.putNextEntry(ze);
webZos.putNextEntry(ze); InputStream is = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[1024];
for (;;) {
int len = is.read(buf);
if (len < 0) break;
webZos.write(buf, 0, len);
zos.write(buf, 0, len);
}
is.close();
}
} catch (Exception e) {
throw e;
} finally {
webZos.close();
zos.close();
} }
压缩正常,生成的test。zip也正常,可response出来的是个错误文件,不知道哪个环节出了问题,xwb。xml里用配置吗
List<String> filePaths = new ArrayList<String>();
filePaths.add("e:/zip/aa.txt");
filePaths.add("e:/zip/bb.txt");
File zipFile = new File(zipPath);
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + zipFile.getName());
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
ZipOutputStream webZos = null;
ServletOutputStream out = null;
try {
out = response.getOutputStream();
webZos = new ZipOutputStream(new BufferedOutputStream(out));
for (String filePath : filePaths) {
File file = new File(filePath);
ZipEntry ze = new ZipEntry(file.getName());
zos.putNextEntry(ze);
webZos.putNextEntry(ze); InputStream is = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[1024];
for (;;) {
int len = is.read(buf);
if (len < 0) break;
webZos.write(buf, 0, len);
zos.write(buf, 0, len);
}
is.close();
}
} catch (Exception e) {
throw e;
} finally {
webZos.close();
zos.close();
} }
压缩正常,生成的test。zip也正常,可response出来的是个错误文件,不知道哪个环节出了问题,xwb。xml里用配置吗
解决方案 »
- java如何取windows AD所有用户列到select里面?
- simplejdbctemplate处理blob字段
- Servlet.service() for servlet jsp threw exception
- hibernate 更新多条数据如何处理
- 求一些Java项目的源代码!!!
- XML优化+xml查询优化
- 【求助】关于JBOSS的问题
- java怎样实现邮件订阅功能
- java学习struts2时候在for循环里启动子线程执行耗时操作,然后就抛出索引越界了。贴出代码,求解脱啦
- 困扰我3天的问题---建立新的用户记录出错
- 想使用springIOC 的方式进行 权限的注入,求思路
- SSH中 hibernate的一个异常,好心人帮我看看是怎么回事,谢了
response.setHeader("Content-Disposition", "attachment; filename=" + zipFile.getName());
替换成
response.setHeader("Content-Disposition", "attachment; filename=" + new String(zipFile.getName().getBytes(), "ISO8859-1"));试试