/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package upload;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
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;
public class SmartUpload
{
protected byte[] m_binArray;
protected HttpServletRequest m_request;
protected HttpServletResponse m_response;
protected ServletContext m_application;
private int m_totalBytes = 0;
private int m_currentIndex = 0;
private int m_startData = 0;
private int m_endData = 0;
private String m_boundary = new String();
private long m_totalMaxFileSize = 872770077903028224L;
private long m_maxFileSize = 872770077903028224L;
private Vector m_deniedFilesList = new Vector();
private Vector m_allowedFilesList = new Vector();
private boolean m_denyPhysicalPath = false;
private boolean m_forcePhysicalPath = false;
private String m_contentDisposition = new String();
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 = new Files();
private Request m_formRequest = new Request();
/**
* @deprecated
*/
public final void init(ServletConfig config)
throws ServletException
{
this.m_application = config.getServletContext();
}
/**
* @deprecated
*/
public void service(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
this.m_request = request;
this.m_response = response;
}
public final void initialize(ServletConfig config, HttpServletRequest request, HttpServletResponse response)
throws ServletException
{
this.m_application = config.getServletContext();
this.m_request = request;
this.m_response = response;
}
public final void initialize(PageContext pageContext)
throws ServletException
{
this.m_application = pageContext.getServletContext();
this.m_request = ((HttpServletRequest)pageContext.getRequest());
this.m_response = ((HttpServletResponse)pageContext.getResponse());
}
/**
* @deprecated
*/
public final void initialize(ServletContext application, HttpSession session, HttpServletRequest request, HttpServletResponse response, JspWriter out)
throws ServletException
{
this.m_application = application;
this.m_request = request;
this.m_response = response;
}
public void upload()
throws upload.SmartUploadException, IOException, ServletException
{
int totalRead = 0;
int readBytes = 0;
long totalFileSize = 872767586821996544L;
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;
this.m_totalBytes = this.m_request.getContentLength();
this.m_binArray = new byte[this.m_totalBytes];
while (totalRead < this.m_totalBytes) {
try {
this.m_request.getInputStream();
readBytes = this.m_request.getInputStream().read(this.m_binArray, totalRead, this.m_totalBytes - totalRead);
}
catch (Exception e)
{
throw new upload.SmartUploadException("Unable to upload.");
}
totalRead += readBytes;
}
while ((!(found)) && (this.m_currentIndex < this.m_totalBytes)) {
if (this.m_binArray[this.m_currentIndex] == 13)
found = true;
else
this.m_boundary = String.valueOf(this.m_boundary).concat(String.valueOf((char)this.m_binArray[this.m_currentIndex]));
this.m_currentIndex += 1;
}
if (this.m_currentIndex == 1) {
return;
}
this.m_currentIndex += 1;
while (this.m_currentIndex < this.m_totalBytes)
{
dataHeader = getDataHeader();
this.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 (this.m_deniedFilesList.contains(fileExt) == true) {
throw new SecurityException("The extension of the file is denied to be uploaded (1015).");
}
if ((this.m_allowedFilesList.isEmpty() == false) && (this.m_allowedFilesList.contains(fileExt) == false))
{
throw new SecurityException("The extension of the file is not allowed to be uploaded (1010).");
}
if ((this.m_maxFileSize > 0) && (this.m_endData - this.m_startData + 1 > this.m_maxFileSize))
{
throw new SecurityException(String.valueOf(new StringBuffer("Size exceeded for this file : ").append(fileName).append(" (1105).")));
}
totalFileSize += this.m_endData - this.m_startData + 1;
if ((this.m_totalMaxFileSize > 0) && (totalFileSize > this.m_totalMaxFileSize))
{
throw new SecurityException("Total File Size exceeded (1110).");
}
}
if (isFile)
{
File newFile = new 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)
this.m_startData += 128;
newFile.setSize(this.m_endData - this.m_startData + 1);
newFile.setStartData(this.m_startData);
newFile.setEndData(this.m_endData);
this.m_files.addFile(newFile);
}
else
{
String value = new String(this.m_binArray, this.m_startData, this.m_endData - this.m_startData + 1);
this.m_formRequest.putParameter(fieldName, value);
}
if ((char)this.m_binArray[(this.m_currentIndex + 1)] == '-') { return;
}
this.m_currentIndex += 2;
}
}
public int save(String destPathName)
throws upload.SmartUploadException, IOException, ServletException
{
return save(destPathName, 0);
}
public int save(String destPathName, int option)
throws upload.SmartUploadException, IOException, ServletException
{
int count = 0;
if (destPathName == null) {
destPathName = this.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 < this.m_files.getCount(); ++i)
if (!(this.m_files.getFile(i).isMissing())) {
th