如题,搜了一下,基本都是smartupload组件,
能不能自己写一个啊,上传的原理是什么?
哪位能指教,有代码最好了.
能不能自己写一个啊,上传的原理是什么?
哪位能指教,有代码最好了.
解决方案 »
- Mule ESB使用了中文,则返回乱码,怎么解决?
- 想请朋友们帮我看下我的这个jsp哪里不对
- 这个网页怎么做?
- mysql 语法 的一些问题 请教大家 大家帮帮忙!!
- 如何让INPUT 表单也能实现类似SELECT的效果?
- 怎么样更改double类型的表现形式?
- 急急急求 一个全国省市区的数据库下载---http://d.download.csdn.net/down/341245/zlwork_84,因为分不够了,不能下载了
- JFreeChart高手:如何准确控制坐标轴上显示的数字/刻度?
- 有个容易的小问题,请教,100分(在线等候!!)
- java程序生成zip文件并下载,在弱网络情况下,下载不完全,解压不成功,提示压缩文件已经损坏
- 如何实现动态数据库二级菜单效果
- 如何将时间转换成汉字格式
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.io.File;import javax.mail.internet.ContentType;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;// Referenced classes of package com.jspsmart.upload:
// Files, Request, SmartUploadException, Filepublic class SmartUpload
{
String preName="";
public void setpreName(String newpreName){
preName=newpreName;
} 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();
} /**
* @deprecated Method init is deprecated
*/ public final void init(ServletConfig config)
throws ServletException
{
m_application = config.getServletContext();
} /**
* @deprecated Method service is deprecated
*/ public void service(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
m_request = request;
m_response = response;
} public final void initialize(ServletConfig config, HttpServletRequest request, HttpServletResponse response)
throws ServletException
{
m_application = config.getServletContext();
m_request = request;
m_response = response;
} public final void initialize(PageContext pageContext)
throws ServletException
{
m_application = pageContext.getServletContext();
m_request = (HttpServletRequest)pageContext.getRequest();
m_response = (HttpServletResponse)pageContext.getResponse();
} /**
* @deprecated Method initialize is deprecated
*/ public final void initialize(ServletContext application, HttpSession session, HttpServletRequest request, HttpServletResponse response, JspWriter out)
throws ServletException
{
m_application = application;
m_request = request;
m_response = response;
}
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();
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 = preName+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.cargo.jsp.application.upload.File newFile = new com.cargo.jsp.application.upload.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);
newFile.setEndData(m_endData);
m_files.addFile(newFile);
} else
{
String value = new String(m_binArray, m_startData, (m_endData - m_startData) + 1);
m_formRequest.putParameter(fieldName, value);
}
if((char)m_binArray[m_currentIndex + 1] == '-')
break;
m_currentIndex = m_currentIndex + 2;
} while(true);
}
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();
m_binArray = new byte[m_totalBytes];
// if (m_binArray.length > 10000) {
// throw new SmartUploadException("The File is too big.");
// }
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 = preName+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.cargo.jsp.application.upload.File newFile = new com.cargo.jsp.application.upload.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);
newFile.setEndData(m_endData);
m_files.addFile(newFile);
} else
{
String value = new String(m_binArray, m_startData, (m_endData - m_startData) + 1);
m_formRequest.putParameter(fieldName, value);
}
if((char)m_binArray[m_currentIndex + 1] == '-')
break;
m_currentIndex = m_currentIndex + 2;
} while(true);
} public int save(String destPathName)
throws SmartUploadException, IOException, ServletException
{
return save(destPathName, 0);
} public int save(String destPathName, int option)
throws SmartUploadException, IOException, ServletException
{
int count = 0;
if(destPathName == null)
destPathName = m_application.getRealPath("/");
if(destPathName.indexOf("/") != -1)
{
if(destPathName.charAt(destPathName.length() - 1) != '/')
destPathName = String.valueOf(destPathName).concat("/");
} else
if(destPathName.charAt(destPathName.length() - 1) != '\\')
destPathName = String.valueOf(destPathName).concat("\\");
for(int i = 0; i < m_files.getCount(); i++)
if(!m_files.getFile(i).isMissing())
{
m_files.getFile(i).saveAs(destPathName + m_files.getFile(i).getFileName(), option);
count++;
} return count;
} public int getSize()
{
return m_totalBytes;
} public byte getBinaryData(int index)
{
byte retval;
try
{
retval = m_binArray[index];
}
catch(Exception e)
{
throw new ArrayIndexOutOfBoundsException("Index out of range (1005).");
}
return retval;
} public Files getFiles()
{
return m_files;
} public Request getRequest()
{
return m_formRequest;
} public void downloadFile(String sourceFilePathName)
throws SmartUploadException, IOException, ServletException
{
downloadFile(sourceFilePathName, null, null);
} public void downloadFile(String sourceFilePathName, String contentType)
throws SmartUploadException, IOException, ServletException
{
downloadFile(sourceFilePathName, contentType, null);
} public void downloadFile(String sourceFilePathName, String contentType, String destFileName)
throws SmartUploadException, IOException, ServletException
{
downloadFile(sourceFilePathName, contentType, destFileName, 65000);
} public void downloadFile(String sourceFilePathName, String contentType, String destFileName, int blockSize)
throws SmartUploadException, IOException, ServletException
{