FileUpload文件上传组件与java.util.zip解压缩类

[b][size=medium]使用总结
/**
*要完成的功能,从页面上传zip文件及若干参数,将zip文件解压缩放到指定位置
*使用到的组件有文件长传组件FileUpload,java类,java.util.zip;
/
<form action="fileUploadFinish.jsp" name="fileform" method="post" enctype="multipart/form-data">

接受页面的处理
DiskFileItemFactory dff = new DiskFileItemFactory();
dff.setSizeThreshold(Integer.parseInt(ZipUploadConfig.getProperty("SIZETHRESHOLD")));
ServletFileUpload sfu = new ServletFileUpload(dff);
sfu.setFileSizeMax(Integer.parseInt(ZipUploadConfig.getProperty("FILESIZEMAX")));
sfu.setSizeMax(Integer.parseInt(ZipUploadConfig.getProperty("SIZEMAX")));

HashMap param = new HashMap();
List list = sfu.parseRequest(request);//包括了,form表单中的字段和上传文件

if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
FileItem fi = (FileItem) list.get(i);
if (fi.isFormField()) { //将参数中的form表单中的字段提取出来
String name = fi.getFieldName();
String value = fi.getString();
param.put(name, value);
}
}
}

String overload_flag = ""+param.get("overload_flag");//将form表单中的字段取出
String stylesheet = ""+param.get("stylesheet");
String template = ""+param.get("template");


if (list != null && list.size() > 0) {
for (int j = 0; j < list.size(); j++) {
FileItem fi = (FileItem) list.get(j);
if (!fi.isFormField() && fi.getName().length() > 0) { //将上传的文件取出
String zipFileName = fi.getName().substring(fi.getName().lastIndexOf("\\") + 1);//去掉盘符路径,只留下文件名
//contentInfo.setContenttype(zipFileName.substring(0, zipFileName.lastIndexOf(".")));
BufferedInputStream in = new BufferedInputStream(fi.getInputStream());
FileUploader zip = new FileUploader(db, in, contentInfo,servletContext);
Map result = zip.unzip();
in.close();
}
}
}

FileUploader.java

public FileUploader(DB db, InputStream inStream,ContentInfoBean contentInfo,ServletContext servletcontext) {
super();
this.servletcontext = servletcontext;
this.inStream = inStream;
this.db = db;
this.contentInfo = contentInfo;
}

public HashMap unzip() {
HashMap result = new HashMap();
//
// Expand ZIP file
//
ZipInputStream in = new ZipInputStream(this.inStream);
ZipEntry z;
Date now = new Date();
String tmpPath = UploadFileConfig.getUploadTmpPath() + File.separator + now.getTime();//UploadFileConfig.getUploadTmpPath()<--->E:\\project\\V682\\upload\\tmp
File file = new File(tmpPath);
if (!file.exists()) {
try {
file.mkdirs(); //创建临时文件夹
} catch (Exception e) {
e.printStackTrace();
}
}
try {
while ((z = in.getNextEntry()) != null) {
String name = z.getName();
if (z.isDirectory()) {
name = name.substring(0, name.length() - 1);
File f = new File(tmpPath + File.separator + name);
try {
f.mkdirs();
} catch (Exception e) {
e.printStackTrace();
}
} else {
if (name.indexOf("/") > 0) {
File folder = new File(tmpPath + File.separator + name.substring(0, name.lastIndexOf("/")));
if (!folder.exists()) {
try {
folder.mkdirs();
} catch (Exception e) {
e.printStackTrace();
}
}
}
File f = new File(tmpPath + File.separator + name);
f.createNewFile();
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f));
int size = 0;
byte[] b = new byte[4096];
while ((size = in.read(b)) != -1) {
out.write(b, 0, size);
}
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}[/size][/b]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值