文件的打包压缩与下载
2013-12-19 09:45
260 查看
java代码:action方法
public void zip(List<BbEntity list,String zipName,,String sourceName){
OutputStream o = null;
o = servletActionContext.getResponse().getOutputStream();//将输出流写入到服务器的响应输出流里。
response.setHeader("","attachement;filename='xx.zip'");//对下载的文件重命名
ZipOutputStream out =new ZipOutputStream(o);
//ZipOutputStream out =new ZipOutputStream(new File(zipName));//将输出流写入到指定的zip文件里。
for(int i=0;i<list.size();i++){
BbEntity bb = list.get(i);
out.putNextEntry(new ZipEntry(bb.getDisplayPath()+"/"+bb.getFileName));
FileInputStream in = new FileInputStream(new File(bb.getRealPath()));
int b;
while((b=in.read())!=-1){
out.write(b);
}
in.close();
}
out.close();
}
}
}
调用方式:zip(getBbList(),"d:/xx.zip","d:/xx")
页面调用方式:window.location = '${pagecontext.request.contextPath}/xx/yy!zip.action'
页面展示:弹出一个下载对话框。
将upload文件写到指定的path,文件名为filename
public static void saveFileFromInputStream(File upload,String path,String filename) throws IOException
{
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(upload));
out = new BufferedOutputStream(new FileOutputStream(path + "/"
+ filename));
byte[] buffer = new byte[BUFFER_SIZE];
int byteread = 0;
while ((byteread = in.read(buffer)) != -1) {
out.write(buffer, 0, byteread);
out.flush();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
in.close();
out.close();
}
}
public void zip(List<BbEntity list,String zipName,,String sourceName){
OutputStream o = null;
o = servletActionContext.getResponse().getOutputStream();//将输出流写入到服务器的响应输出流里。
response.setHeader("","attachement;filename='xx.zip'");//对下载的文件重命名
ZipOutputStream out =new ZipOutputStream(o);
//ZipOutputStream out =new ZipOutputStream(new File(zipName));//将输出流写入到指定的zip文件里。
for(int i=0;i<list.size();i++){
BbEntity bb = list.get(i);
out.putNextEntry(new ZipEntry(bb.getDisplayPath()+"/"+bb.getFileName));
FileInputStream in = new FileInputStream(new File(bb.getRealPath()));
int b;
while((b=in.read())!=-1){
out.write(b);
}
in.close();
}
out.close();
}
}
}
调用方式:zip(getBbList(),"d:/xx.zip","d:/xx")
页面调用方式:window.location = '${pagecontext.request.contextPath}/xx/yy!zip.action'
页面展示:弹出一个下载对话框。
将upload文件写到指定的path,文件名为filename
public static void saveFileFromInputStream(File upload,String path,String filename) throws IOException
{
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(upload));
out = new BufferedOutputStream(new FileOutputStream(path + "/"
+ filename));
byte[] buffer = new byte[BUFFER_SIZE];
int byteread = 0;
while ((byteread = in.read(buffer)) != -1) {
out.write(buffer, 0, byteread);
out.flush();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
in.close();
out.close();
}
}
相关文章推荐
- PHP中ZipArchive压缩文件并下载打包好的文件介绍
- Java打包下载ZIP压缩文件
- php文件打包 下载之使用PHP自带的ZipArchive压缩文件并下载打包好的文件
- 文件打包,下载之使用PHP自带的ZipArchive压缩文件并下载打包好的文件
- 在C#中在服务器上打包压缩文件,并下载
- 文件打包,下载之使用PHP自带的ZipArchive压缩文件并下载打包好的文件
- 文件打包,下载之使用PHP自带的ZipArchive压缩文件并下载打包好的文件
- securecrt中文件打包成压缩文件并下载命令
- 文件打包,下载之使用PHP自带的ZipArchive压缩文件并下载打包好的文件
- 多文件打包压缩、下载类--使用第三方控件ICSharpCode
- PHP网页上打包文件生成压缩文件zip,并弹出下载
- C# 压缩打包文件下载
- securecrt中文件打包成压缩文件并下载命令
- 批量下载,多文件压缩打包zip下载
- C# 压缩打包文件下载
- zip压缩打包文件下载
- 文件打包,下载之使用PHP自带的ZipArchive压缩文件并下载打包好的文件
- Thinkphp5用PHP扩展类ZipArchive批量打包压缩文件,并下载
- 将零散文件使用ICSharpCode.SharpZipLib压缩打包后一次性下载
- php文件打包 下载之使用PHP自带的ZipArchive压缩文件并下载打包好的文件