`
- 浏览:
369751 次
- 性别:
- 来自:
大连
-
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();
}
}
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
NULL 博文链接:https://412887952-qq-com.iteye.com/blog/1366123
文件上传组件commons-fileupload用户指南文件上传组件commons-fileupload用户指南
FileUpload 文件上传组件的ASP.NET实例,基于主副窗口的上传程序,利用线程提升上传... ASP.NET FileUpload上传图片并解决上传限制 解决FileUpload控件上传文件大小限制 ASP.NET中实现FileUpload多文件上传
Common-fileupload组件实现文件上传.pdfCommon-fileupload组件实现文件上传.pdfCommon-fileupload组件实现文件上传.pdfCommon-fileupload组件实现文件上传.pdfCommon-fileupload组件实现文件上传.pdfCommon-...
commons-fileupload-1.0上传组件使用实例.以及使用组件的乱码问题。
上传组件Commons fileUpload.jar剖析 兔子Java 系列之2 - Commons fileUpload.doc
commons-fileupload-1.1.1.jar fileupload.jar上传文件 j2ee上传文件 j2ee上传文件jar包
commons-fileupload-1.2.2-bin.zip一个文件上传的第三方插件
包中描述了多种上传文件的方式,以及使用js控件进行上传。里面含有上传文件 commons-fileupload-1.1.jar jquery.ajaxfileupload.js 以及demo。
Upload.java package com.zj.sample; import java.io.File; import java.io.IOException; import java.util.Iterator; import java.util.List; import javax.servlet.ServletException; import javax.servlet....
我自己写的一个fileUpload组件的上传例子
Apache FileUpload文件上传组件API解析
本工程用于借助Servlet3.0实现文件上传 本工程编码方式:UTF-8 注意: 1、在上传文件前请在计算机C盘新建一个名为“upload”的文件夹,否则会出现如下异常:java.io.IOException: The temporary upload location [C...
fileuploadhtml5 是一个用 html5 asp.net mvc 4 开发的文件上传组件,支持断点续传。 demo: http://fileuploadh5.sitecloud.cytanium.com/
支持多文件上传,打破网页一次可以只选一个文件的传统管理。实现一次多选多个文件上传。
主要为大家详细介绍了使用fileupload实现文件上传功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
apach提供的文件上传工具类,需要结合commons-io包进行使用
bootstrap-fileupload被誉为最好的文件上传组件。功能非常细腻周全,极微细的参数都可以得到设置,你值得拥有。
java实现的文件上传下载FileUpLoad,内含SQL文件,使用Oracle数据库,struts2组件包装,包装流加快下载,可上传可下载,适合初学者学习使用!