package com.cnblogs.zxub.upload;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.MultipartStream;
import org.apache.commons.fileupload.servlet.ServletRequestContext;
/**
* @author zxub 2006-7-14 下午05:25:10 处理输入流,并提供相应信息
*/
public class HttpFileUpload extends FileUpload
{
// 有默认参数文件
public static ResourceBundle config = ResourceBundle.getBundle("config");
private static final long K = 1024;
private static final long M = 1024 * 1024;
// 默认的单个文件大小限制,0为无限制,配置文件中存放的为String类型
private long fileLimitSize = getByteSize(config.getString("fileLimitSize"));
// 默认的文件上传类型限制
private String allowFileTypes = config.getString("allowFileTypes");
// private String allowFiles = null; // 允许上传的文件类型,null表示无限制
private boolean allowField = false; // 是否允许处理非文件域,默认为否
private int reportLimitSize = 10240; // 最小报告长度,默认为10K
private StreamReport streamReport = null; // 上传进度处理对象
public HttpFileUpload()
{
super();
setSizeMax(getByteSize(config.getString("onceMaxSize")));
}
public HttpFileUpload(FileItemFactory fileItemFactory)
{
super(fileItemFactory);
setSizeMax(getByteSize(config.getString("onceMaxSize")));
}
public long getFileLimitSize()
{
return fileLimitSize;
}
public void setFileLimitSize(long fileLimitSize)
{
this.fileLimitSize = fileLimitSize;
}
public static long getByteSize(String size)
{
String unit = size.substring(size.length() - 1).toUpperCase();
String num;
if (unit.equals("K"))
{
num = size.substring(0, size.length() - 1);
return Long.parseLong(num) * HttpFileUpload.K;
}
else if (unit.equals("M"))
{
num = size.substring(0, size.length() - 1);
return Long.parseLong(num) * HttpFileUpload.M;
}
else
{
return Long.parseLong(size);
}
}
/**
* @return [String] 允许上传的文件类型
*/
public String getAllowFileTypes()
{
return allowFileTypes;
}
/**
* 设置允许上传的文件类型,默认是"",无限制
*
* @param allowFileTypes
*/
public void setAllowFileTypes(String allowFileTypes)
{
this.allowFileTypes = allowFileTypes;
}
public void addAllowFileTypes(String allowFileTypes)
{
this.allowFileTypes += ("," + allowFileTypes);
}
/**
* 设置是否允许上载除file以外的field的内容
*
* @param allow
*/
public void setAllowField(boolean allow)
{
allowField = allow;
}
/**
* @return [boolean] 是否允许上载除file以外的field
*/
public boolean isAllowField()
{
return allowField;
}
/**
* 设置最小报告长度,这个参数影响报告频度,默认是10K
*
* @param reportLimitSize
*/
public void setReportLimitSize(int reportLimitSize)
{
this.reportLimitSize = reportLimitSize;
}
public int getReportLimitSize()
{
return reportLimitSize;
}
/**
* 上传后清理工作
*/
public void dispose()
{
if (streamReport != null) streamReport.dispose();
}
/**
* 完成上传操作 根据allowField值决定是否接收除file以外的其它field,并检查上传的文件是否为允许的文件
*
* @param request
* @return
* @throws FileUploadException
* @throws IOException
*/
public List parseRequest(HttpServletRequest request)
throws FileUploadException
{
ServletRequestContext ctx = new ServletRequestContext(request);
if (ctx == null)
{
throw new NullPointerException(
"HttpFileUpload(parseRequestEx): ctx parameter");
}
ArrayList items = new ArrayList();
String contentType = ctx.getContentType();
if ((null == contentType)
|| (!contentType.toLowerCase().startsWith(MULTIPART)))
{
throw new InvalidContentTypeException(
"HttpFileUpload(parseRequestEx): the request doesn't contain a "
+ MULTIPART_FORM_DATA + " or " + MULTIPART_MIXED
+ " stream, content type header is " + contentType);
}
int requestSize = ctx.getContentLength();
if (requestSize == -1)
{
throw new UnknownSizeException("由于数据大小未知,上传被终止!");
}
long sizeMax = getSizeMax();
if (sizeMax >= 0 && requestSize > sizeMax)
{
throw new SizeLimitExceededException("由于文件总大小 [ " + requestSize
+ " ] 超出限定 [ " + sizeMax + " ],上传被终止!", requestSize,
sizeMax);
}
String charEncoding = getHeaderEncoding();
if (charEncoding == null)
{
charEncoding = ctx.getCharacterEncoding();
}
try
{
byte[] boundary = getBoundary(contentType);
if (boundary == null)
{
throw new FileUploadException(
"the request was rejected because "
+ "no multipart boundary was found");
}
streamReport = new StreamReport(request);
streamReport.init();
streamReport.setReportLimitSize(reportLimitSize);
InputStream input = ctx.getInputStream();
InputSteamEx inputEx = new InputSteamEx(input, streamReport);
MultipartStream multi = new MultipartStream(inputEx, boundary);
multi.setHeaderEncoding(charEncoding);
boolean nextPart = multi.skipPreamble();
List invalidFiles = new ArrayList();
while (nextPart)
{
Map headers = parseHeaders(multi.readHeaders());
String fieldName = getFieldName(headers);
if (fieldName != null)
{
String subContentType = getHeader(headers, CONTENT_TYPE);
if (subContentType != null
&& subContentType.toLowerCase().startsWith(
MULTIPART_MIXED))
{
// Multiple files.
// 由于一般传送文件时,设置的是multipart/form-data,所以这里不会进入
byte[] subBoundary = getBoundary(subContentType);
multi.setBoundary(subBoundary);
boolean nextSubPart = multi.skipPreamble();
while (nextSubPart)
{
headers = parseHeaders(multi.readHeaders());
if (getFileName(headers) != null)
{
FileItem item = createItem(headers, false);
OutputStream os = item.getOutputStream();
try
{
multi.readBodyData(os);
}
finally
{
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Test.zip (57个子文件)
Test
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.core.resources.prefs 128B
org.eclipse.wst.common.component 450B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 500B
org.eclipse.jdt.core.prefs 364B
src
com
cnblogs
zxub
upload
DefaultReportItem.java 4KB
StreamReport.java 2KB
ReportItemManageImpl.java 314B
StreamReportImpl.java 206B
ReportItemImpl.java 540B
HttpFileUpload.java 12KB
FileUploader.java 2KB
InputSteamEx.java 1KB
ReportItemFactory.java 938B
DefaultReportItemManage.java 2KB
config.properties 972B
.project 2KB
WebRoot
META-INF
MANIFEST.MF 36B
WEB-INF
web.xml 742B
classes
com
cnblogs
zxub
upload
StreamReportImpl.class 161B
FileUploader.class 3KB
HttpFileUpload$InvalidFileUploadException.class 1KB
ReportItemImpl.class 375B
DefaultReportItemManage.class 2KB
InputSteamEx.class 1KB
StreamReport.class 2KB
ReportItemManageImpl.class 308B
HttpFileUpload.class 9KB
DefaultReportItem.class 4KB
ReportItemFactory.class 1KB
config.properties 972B
lib
servlet-api.jar 95KB
commons-io-1.2.jar 64KB
commons-fileupload-1.2.1.jar 56KB
temp
index.html 3KB
js
UI
images
close.gif 851B
restore.gif 847B
minimize.gif 838B
panel.js 6KB
basic.js 1KB
base
thread.js 1KB
event.js 4KB
position.js 3KB
waiting.gif 819B
jquery-1.11.1.min.js 94KB
jquery-1.11.1.js 276KB
application
progressBar.js 2KB
uploadProgress.js 3KB
env.js 18KB
receive.jsp 2KB
index.jsp 4KB
progressDetail.jsp 604B
upload.jsp 759B
.mymetadata 285B
.classpath 529B
.myeclipse
共 57 条
- 1
资源评论
- qq_248983452014-12-28这个对我很有用处,谢谢
小白的家
- 粉丝: 1
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功