`
water84222
  • 浏览: 369751 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

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

阅读更多
使用总结
/**
*要完成的功能,从页面上传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();
}
}
分享到:
评论
2 楼 water84222 2009-03-16  
jackotty 写道

你写的东西不全啊,发个完整的代码看看!

这里貼得是主要代码,这块代码是以前项目中使用的,现在也找不全了。
1 楼 jackotty 2009-03-15  
你写的东西不全啊,发个完整的代码看看!

相关推荐

Global site tag (gtag.js) - Google Analytics