为什么使用java zipoutputStream打包的文件 size为0 byte 在线等!
protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServle...
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
//设置response的编码方式
response.setContentType("application/x-msdownload");
List<String> fileNames = new ArrayList<String>();//需要下载的文件存在的集合
fileNames.add("C:\\Users\\Think\\Desktop\\古文观止.pdf");
fileNames.add("C:\\Users\\Think\\Desktop\\搜索人员.zip");
//FileSystemView fsv = FileSystemView.getFileSystemView();
String filename = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".zip";
//File file = new File(fsv.getHomeDirectory()+"/"+filename);
//String saveFilePath = filename;//默认保存的路径为桌面
response.setHeader("Content-Disposition","attachment;filename="+filename);
response.setContentType("application/octet-stream");
try {
toZip(filename, fileNames);
JOptionPane.showMessageDialog(null, "下载完成");
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
} 展开
response.setContentType(CONTENT_TYPE);
//设置response的编码方式
response.setContentType("application/x-msdownload");
List<String> fileNames = new ArrayList<String>();//需要下载的文件存在的集合
fileNames.add("C:\\Users\\Think\\Desktop\\古文观止.pdf");
fileNames.add("C:\\Users\\Think\\Desktop\\搜索人员.zip");
//FileSystemView fsv = FileSystemView.getFileSystemView();
String filename = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".zip";
//File file = new File(fsv.getHomeDirectory()+"/"+filename);
//String saveFilePath = filename;//默认保存的路径为桌面
response.setHeader("Content-Disposition","attachment;filename="+filename);
response.setContentType("application/octet-stream");
try {
toZip(filename, fileNames);
JOptionPane.showMessageDialog(null, "下载完成");
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
} 展开
4个回答
展开全部
实现代码如下:(在当前包文件夹下新建一个文本文件:123.txt,将其压缩到test.zip)
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Zipstream {
public static void main(String[] args) throws Exception{
FileOutputStream f=new FileOutputStream("text.zip");
CheckedOutputStream csum=new CheckedOutputStream(f,new Adler32());
ZipOutputStream zos=new ZipOutputStream(csum);
BufferedOutputStream out = new BufferedOutputStream(zos);
zos.setComment("A test of Java Ziping!");
BufferedReader in =new BufferedReader(new FileReader("123.txt"));
zos.putNextEntry(new ZipEntry("123.txt"));
int c;
while((c = in.read()()()) != -1){
out.write(c);
}
in.close();
out.flush();
out.close();
}
}
参考这个改下你的代码 先把单独的一个文件压缩好了 再压缩文件夹
还有一点 就是文件路径 检查一下你要压缩的原文件的路径对不对
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Zipstream {
public static void main(String[] args) throws Exception{
FileOutputStream f=new FileOutputStream("text.zip");
CheckedOutputStream csum=new CheckedOutputStream(f,new Adler32());
ZipOutputStream zos=new ZipOutputStream(csum);
BufferedOutputStream out = new BufferedOutputStream(zos);
zos.setComment("A test of Java Ziping!");
BufferedReader in =new BufferedReader(new FileReader("123.txt"));
zos.putNextEntry(new ZipEntry("123.txt"));
int c;
while((c = in.read()()()) != -1){
out.write(c);
}
in.close();
out.flush();
out.close();
}
}
参考这个改下你的代码 先把单独的一个文件压缩好了 再压缩文件夹
还有一点 就是文件路径 检查一下你要压缩的原文件的路径对不对
2013-09-28
展开全部
代码中,还少一个重载的toZip的方法
ZipOutputStream
void close()
Closes the ZIP output stream as well as the stream being filtered.
void closeEntry()
Closes the current ZIP entry and positions the stream for writing
the next entry.
void
finish()
Finishes writing the contents of the ZIP output stream without closing
the underlying stream.
结束时,要调用上面的几个方法 。
另外,WEB服务器端的程序,不能使用JOptionPane。
ZipOutputStream
void close()
Closes the ZIP output stream as well as the stream being filtered.
void closeEntry()
Closes the current ZIP entry and positions the stream for writing
the next entry.
void
finish()
Finishes writing the contents of the ZIP output stream without closing
the underlying stream.
结束时,要调用上面的几个方法 。
另外,WEB服务器端的程序,不能使用JOptionPane。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2018-11-22
展开全部
你ZipOutPutStream是不是没有调用close方法,没有调用的话就会出现size为0的情况
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.Random;import java.util.zip.ZipEntry;import java.util.zip.ZipInputStream;import java.util.zip.ZipOutputStream;import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.PBEKeySpec;import javax.crypto.spec.PBEParameterSpec;public class MyZipUtil {&#47;** * 设置缓冲值 *&#47;static final int BUFFER = 8192;private static final String ALGORITHM = &quot;PBEWithMD5AndDES&quot;;public static void zip(String zipFileName String inputFile,String pwd) throws Exception {zip(zipFileName, new File(inputFile), pwd);}&#47;** * 功能描述:压缩指定路径下的所有文件 * @param zipFileName 压缩文件名(带有路径) * @param inputFile 指定压缩文件夹 * @return * @throws Exception *&#47;public static void zip(String zipFileName, String inputFile) throws Exception {zip(zipFileName, new File(inputFile), null);}&#47;** * 功能描述:压缩文件对象 * @param zipFileName 压缩文件名(带有路径) * @param inputFile 文件对象 * @return * @throws Exception *&#47;public static void zip(String zipFileName, File inputFile,String pwd) throws Exception {ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));zip(outnqu inputFile, &quot;&quot;,pwd);out.close();}&#47;** * * @param out 压缩输出流对象 * @param file * @param base * @throws Exception *&#47;public static void zip(ZipOutputStre......余下全文>>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询