📄 smartupload.java
字号:
package com.jspsmart.upload;
import java.io.*;
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[];
protected HttpServletRequest m_request;
protected HttpServletResponse m_response;
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;
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 void downloadField(
ResultSet rs,
String columnName,
String contentType,
String destFileName)
throws SQLException, IOException, ServletException
{
if (rs == null)
throw new IllegalArgumentException("The RecordSet cannot be null (1045).");
if (columnName == null)
throw new IllegalArgumentException("The columnName cannot be null (1050).");
if (columnName.length() == 0)
throw new IllegalArgumentException("The columnName cannot be empty (1055).");
byte b[] = rs.getBytes(columnName);
if (contentType == null)
m_response.setContentType("application/x-msdownload");
else if (contentType.length() == 0)
m_response.setContentType("application/x-msdownload");
else
m_response.setContentType(contentType);
m_response.setContentLength(b.length);
if (destFileName == null)
m_response.setHeader("Content-Disposition", "attachment;");
else if (destFileName.length() == 0)
m_response.setHeader("Content-Disposition", "attachment;");
else
m_response.setHeader(
"Content-Disposition",
"attachment; filename=".concat(String.valueOf(destFileName)));
m_response.getOutputStream().write(b, 0, b.length);
}
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
{
if (sourceFilePathName == null)
throw new IllegalArgumentException(
String.valueOf(
(new StringBuffer("File '")).append(sourceFilePathName).append(
"' not found (1040).")));
if (sourceFilePathName.equals(""))
throw new IllegalArgumentException(
String.valueOf(
(new StringBuffer("File '")).append(sourceFilePathName).append(
"' not found (1040).")));
if (!isVirtual(sourceFilePathName) && m_denyPhysicalPath)
throw new SecurityException("Physical path is denied (1035).");
if (isVirtual(sourceFilePathName))
sourceFilePathName = m_application.getRealPath(sourceFilePathName);
java.io.File file = new java.io.File(sourceFilePathName);
FileInputStream fileIn = new FileInputStream(file);
long fileLen = file.length();
int readBytes = 0;
int totalRead = 0;
byte b[] = new byte[blockSize];
if (contentType == null)
m_response.setContentType("application/x-msdownload");
else if (contentType.length() == 0)
m_response.setContentType("application/x-msdownload");
else
m_response.setContentType(contentType);
m_response.setContentLength((int) fileLen);
m_contentDisposition =
m_contentDisposition != null ? m_contentDisposition : "attachment;";
if (destFileName == null)
m_response.setHeader(
"Content-Disposition",
String.valueOf(
(new StringBuffer(String.valueOf(m_contentDisposition)))
.append(" filename=")
.append(getFileName(sourceFilePathName))));
else if (destFileName.length() == 0)
m_response.setHeader("Content-Disposition", m_contentDisposition);
else
m_response.setHeader(
"Content-Disposition",
String.valueOf(
(new StringBuffer(String.valueOf(m_contentDisposition)))
.append(" filename=")
.append(destFileName)));
while ((long) totalRead < fileLen)
{
readBytes = fileIn.read(b, 0, blockSize);
totalRead += readBytes;
m_response.getOutputStream().write(b, 0, readBytes);
}
fileIn.close();
}
public void fieldToFile(
ResultSet rs,
String columnName,
String destFilePathName)
throws SQLException, SmartUploadException, IOException, ServletException
{
try
{
if (m_application.getRealPath(destFilePathName) != null)
destFilePathName = m_application.getRealPath(destFilePathName);
InputStream is_data = rs.getBinaryStream(columnName);
FileOutputStream file = new FileOutputStream(destFilePathName);
int c;
while ((c = is_data.read()) != -1)
file.write(c);
file.close();
}
catch (Exception e)
{
throw new SmartUploadException("Unable to save file from the DataBase (1020).");
}
}
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;
}
private String getContentDisp(String dataHeader)
{
String value = new String();
int start = 0;
int end = 0;
start = dataHeader.indexOf(":") + 1;
end = dataHeader.indexOf(";");
value = dataHeader.substring(start, end);
return value;
}
private String getContentType(String dataHeader)
{
String token = new String();
String value = new String();
int start = 0;
int end = 0;
token = "Content-Type:";
start = dataHeader.indexOf(token) + token.length();
if (start != -1)
{
end = dataHeader.length();
value = dataHeader.substring(start, end);
}
return value;
}
private String getDataFieldValue(String dataHeader, String fieldName)
{
String token = new String();
String value = new String();
int pos = 0;
int i = 0;
int start = 0;
int end = 0;
token =
String.valueOf(
(new StringBuffer(String.valueOf(fieldName))).append("=").append('"'));
pos = dataHeader.indexOf(token);
if (pos > 0)
{
i = pos + token.length();
start = i;
token = "\"";
end = dataHeader.indexOf(token, i);
if (start > 0 && end > 0)
value = dataHeader.substring(start, end);
}
return value;
}
private String getDataHeader()
{
int start = m_currentIndex;
int end = 0;
int len = 0;
boolean found = false;
while (!found)
if (m_binArray[m_currentIndex] == 13 && m_binArray[m_currentIndex + 2] == 13)
{
found = true;
end = m_currentIndex - 1;
m_currentIndex = m_currentIndex + 2;
}
else
{
m_currentIndex++;
}
String dataHeader = new String(m_binArray, start, (end - start) + 1);
return dataHeader;
}
private void getDataSection()
{
boolean found = false;
String dataHeader = new String();
int searchPos = m_currentIndex;
int keyPos = 0;
int boundaryLen = m_boundary.length();
m_startData = m_currentIndex;
m_endData = 0;
do {
if (searchPos >= m_totalBytes)
break;
if (m_binArray[searchPos] == (byte) m_boundary.charAt(keyPos))
{
if (keyPos == boundaryLen - 1)
{
m_endData = ((searchPos - boundaryLen) + 1) - 3;
break;
}
searchPos++;
keyPos++;
}
else
{
searchPos++;
keyPos = 0;
}
}
while (true);
m_currentIndex = m_endData + boundaryLen + 3;
}
private String getFileExt(String fileName)
{
String value = new String();
int start = 0;
int end = 0;
if (fileName == null)
return null;
start = fileName.lastIndexOf(46) + 1;
end = fileName.length();
value = fileName.substring(start, end);
if (fileName.lastIndexOf(46) > 0)
return value;
else
return "";
}
private String getFileName(String filePathName)
{
String token = new String();
String value = new String();
int pos = 0;
int i = 0;
int start = 0;
int end = 0;
pos = filePathName.lastIndexOf(47);
if (pos != -1)
return filePathName.substring(pos + 1, filePathName.length());
pos = filePathName.lastIndexOf(92);
if (pos != -1)
return filePathName.substring(pos + 1, filePathName.length());
else
return filePathName;
}
public Files getFiles()
{
return m_files;
}
protected String getPhysicalPath(String filePathName, int option)
throws IOException
{
String path = new String();
String fileName = new String();
String fileSeparator = new String();
boolean isPhysical = false;
fileSeparator = System.getProperty("file.separator");
if (filePathName == null)
throw new IllegalArgumentException("There is no specified destination file (1140).");
if (filePathName.equals(""))
throw new IllegalArgumentException("There is no specified destination file (1140).");
if (filePathName.lastIndexOf("\\") >= 0)
{
path = filePathName.substring(0, filePathName.lastIndexOf("\\"));
fileName = filePathName.substring(filePathName.lastIndexOf("\\") + 1);
}
if (filePathName.lastIndexOf("/") >= 0)
{
path = filePathName.substring(0, filePathName.lastIndexOf("/"));
fileName = filePathName.substring(filePathName.lastIndexOf("/") + 1);
}
path = path.length() != 0 ? path : "/";
java.io.File physicalPath = new java.io.File(path);
if (physicalPath.exists())
isPhysical = true;
if (option == 0)
{
if (isVirtual(path))
{
path = m_application.getRealPath(path);
if (path.endsWith(fileSeparator))
path = path + fileName;
else
path =
String.valueOf(
(new StringBuffer(String.valueOf(path))).append(fileSeparator).append(
fileName));
return path;
}
if (isPhysical)
{
if (m_denyPhysicalPath)
throw new IllegalArgumentException("Physical path is denied (1125).");
else
return filePathName;
}
else
{
throw new IllegalArgumentException("This path does not exist (1135).");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -