package com.soft4j.httpupload4j;
import java.io.*;
import java.net.URLEncoder;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
public class SmartUpload
{
protected byte m_binArray[];
//HttpServletRequest对象。
protected HttpServletRequest m_request;
//HttpServletResponse对象。
protected HttpServletResponse m_response;
//servlet上下文。
protected ServletContext m_application;
private int m_totalBytes;
private int m_currentIndex;
private int m_startData;
private int m_endData;
private String m_boundary;
private long m_totalMaxFileSize;
private long m_maxFileSize;
private Vector m_deniedFilesList;
private Vector m_allowedFilesList;
private boolean m_denyPhysicalPath;
private boolean m_forcePhysicalPath;
private String m_contentDisposition;
public static final int SAVE_AUTO = 0;
public static final int SAVE_VIRTUAL = 1;
public static final int SAVE_PHYSICAL = 2;
private Files m_files;
private Request m_formRequest;
/**
* 目的:解决上传中文文件名的文件名乱码问题。
*/
private String chartSet;
public SmartUpload()
{
m_totalBytes = 0;
m_currentIndex = 0;
m_startData = 0;
m_endData = 0;
m_boundary = new String();
m_totalMaxFileSize = 0L;
m_maxFileSize = 0L;
m_deniedFilesList = new Vector();
m_allowedFilesList = new Vector();
m_denyPhysicalPath = false;
m_forcePhysicalPath = false;
m_contentDisposition = new String();
m_files = new Files();
m_formRequest = new Request();
}
public final void init(ServletConfig config)
throws ServletException
{
//初始化上下文。
m_application = config.getServletContext();
}
public void service(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
//HttpServletRequest对象。
m_request = request;
//HttpServletResponse对象。
m_response = response;
}
//初始化系统参数。
public final void initialize(ServletConfig config, HttpServletRequest request, HttpServletResponse response)
throws ServletException
{
//初始化上下文。
m_application = config.getServletContext();
//HttpServletRequest对象。
m_request = request;
//HttpServletResponse对象。
m_response = response;
setChartSet(m_response.getCharacterEncoding());
}
//初始化系统参数。
public final void initialize(PageContext pageContext)
throws ServletException
{
//初始化上下文。
m_application = pageContext.getServletContext();
//HttpServletRequest对象。
m_request = (HttpServletRequest)pageContext.getRequest();
//HttpServletResponse对象。
m_response = (HttpServletResponse)pageContext.getResponse();
//设置编码UTF-8。
setChartSet(m_response.getCharacterEncoding());
}
//初始化系统参数。
public final void initialize(ServletContext application, HttpSession session, HttpServletRequest request, HttpServletResponse response, JspWriter out)
throws ServletException
{
//初始化上下文。
m_application = application;
//HttpServletRequest对象。
m_request = request;
//HttpServletResponse对象。
m_response = response;
setChartSet(m_response.getCharacterEncoding());
}
public void upload()
throws SmartUploadException, IOException, ServletException
{
int totalRead = 0;
int readBytes = 0;
long totalFileSize = 0L;
boolean found = false;
String dataHeader = new String();
String fieldName = new String();
String fileName = new String();
String fileExt = new String();
String filePathName = new String();
String contentType = new String();
String contentDisp = new String();
String typeMIME = new String();
String subTypeMIME = new String();
boolean isFile = false;
m_totalBytes = m_request.getContentLength();//10733
m_binArray = new byte[m_totalBytes];
for(; totalRead < m_totalBytes; totalRead += readBytes)
try
{
m_request.getInputStream();
readBytes = m_request.getInputStream().read(m_binArray, totalRead, m_totalBytes - totalRead);
}
catch(Exception e)
{
throw new SmartUploadException("Unable to upload.");
}
for(; !found && m_currentIndex < m_totalBytes; m_currentIndex++)
if(m_binArray[m_currentIndex] == 13)
found = true;
else
m_boundary = m_boundary + (char)m_binArray[m_currentIndex];
if(m_currentIndex == 1)
return;
m_currentIndex++;
do
{
if(m_currentIndex >= m_totalBytes)
break;
dataHeader = getDataHeader();
m_currentIndex = m_currentIndex + 2;
isFile = dataHeader.indexOf("filename") > 0;
fieldName = getDataFieldValue(dataHeader, "name");
if(isFile)
{
filePathName = getDataFieldValue(dataHeader, "filename");
fileName = getFileName(filePathName);
fileExt = getFileExt(fileName);
contentType = getContentType(dataHeader);
contentDisp = getContentDisp(dataHeader);
typeMIME = getTypeMIME(contentType);
subTypeMIME = getSubTypeMIME(contentType);
}
getDataSection();
if(isFile && fileName.length() > 0)
{
if(m_deniedFilesList.contains(fileExt))
throw new SecurityException("The extension of the file is denied to be uploaded (1015).");
if(!m_allowedFilesList.isEmpty() && !m_allowedFilesList.contains(fileExt))
throw new SecurityException("The extension of the file is not allowed to be uploaded (1010).");
if(m_maxFileSize > (long)0 && (long)((m_endData - m_startData) + 1) > m_maxFileSize)
throw new SecurityException(String.valueOf((new StringBuffer("Size exceeded for this file : ")).append(fileName).append(" (1105).")));
totalFileSize += (m_endData - m_startData) + 1;
if(m_totalMaxFileSize > (long)0 && totalFileSize > m_totalMaxFileSize)
throw new SecurityException("Total File Size exceeded (1110).");
}
if(isFile)
{
com.soft4j.httpupload4j.File newFile = new com.soft4j.httpupload4j.File();
newFile.setParent(this);
newFile.setFieldName(fieldName);
newFile.setFileName(fileName);
newFile.setFileExt(fileExt);
newFile.setFilePathName(filePathName);
newFile.setIsMissing(filePathName.length() == 0);
newFile.setContentType(contentType);
newFile.setContentDisp(contentDisp);
newFile.setTypeMIME(typeMIME);
newFile.setSubTypeMIME(subTypeMIME);
if(contentType.indexOf("application/x-macbinary") > 0)
m_startData = m_startData + 128;
newFile.setSize((m_endData - m_startData) + 1);
newFile.setStartData(m_startData);
没有合适的资源?快使用搜索试试~ 我知道了~
swfupload(new)swfupload(new)swfupload(new)

共38个文件
java:7个
class:7个
js:5个


温馨提示
swfupload(new)swfupload(new)swfupload(new)swfupload(new)swfupload(new)swfupload(new)swfupload(new)swfupload(new)swfupload(new)swfupload(new)swfupload(new)
资源推荐
资源详情
资源评论











收起资源包目录































































共 38 条
- 1
资源评论

- shaoslu2015-07-01挺好用的,谢谢
- huanghe8886662014-04-14不错的东西,挺有用的

fanxing168
- 粉丝: 104
- 资源: 11
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


会员权益专享
安全验证
文档复制为VIP权益,开通VIP直接复制
