📄 fileupload.java
字号:
//***********************************************
//Copyright 2002 by BHH丂All Rights Reverved.
//
//僔僗僥儉柤 :BestProject
//僒僽僔僗僥儉柤 :忈奞娗棟
//
//===============================================
//version 曄峏擔 曄峏幰 曄峏撪梕
//-----------------------------------------------
//01-00 2003.1.21 丂棝埞愇 峏怴嶌惉
package bestproject.upload;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import bestproject.common.*;
public class FileUpload {
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;
///////////////////////////
private String pFileName;
private String pFileExt;
private String pFilePath;
///////////////////////////
/**
*弶婜壔
*@since 01-00
*/
public FileUpload() {
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();
pFileName = new String();
pFileExt = new String();
pFilePath = new String();
}
/**
*request,response弶婜壔
*@param request HttpServletRequest
*@param response HttpServletResponse
*@throws IOException
*@throws ServletException
*@since 01-00
*/
public void service(HttpServletRequest request, HttpServletResponse response) throws
IOException, ServletException {
m_request = request;
m_response = response;
}
/**
*Request傪庢傞
*@return Request
*@since 01-00
*/
public Request getRequest() {
return m_formRequest;
}
/**
*multipart form 僨乕僞傪庢傞
*@since 01-00
*@throws IOException
*@throws ServletException
*/
public void upload() throws 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 {
readBytes = m_request.getInputStream().read(m_binArray, totalRead, m_totalBytes - totalRead);
}
catch (IOException e) {
throw e;
}
}
for (; !found && m_currentIndex < m_totalBytes; m_currentIndex++) {
if (m_binArray[m_currentIndex] == 13) {
String bbb = "";
bbb = bbb + (char) m_binArray[m_currentIndex];
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);
////////////////////////////////
setpFileName(fileName);
setpFileExt(fileExt);
setpFilePath(filePathName);
////////////////////////////////
contentType = getContentType(dataHeader);
contentDisp = getContentDisp(dataHeader);
typeMIME = getTypeMIME(contentType);
subTypeMIME = getSubTypeMIME(contentType);
}
getDataSection();
if (isFile && fileName.length() > 0) {
if (m_deniedFilesList.contains(fileExt)) { //嬛巭upload偺僼傽僀儖椶宆
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) {
bestproject.upload.File newFile = new bestproject.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);
}
/**
*uploaded僼傽僀儖傪曐懚偡傞
*@return int
*@param destPathName filepath
*@since 01-00
*@throws BP0600_CommonEx
*/
public int save(String destPathName) throws BP0600_CommonEx {
int i = 0;
try {
i = save(destPathName, 0);
}
catch (BP0600_CommonEx e) {
throw e;
}
return i;
}
/**
*uploaded僼傽僀儖傪曐懚偡傞
*@return int
*@param destPathName filepath
*@param option int
*@since 01-00
*@throws BP0600_CommonEx
*/
public int save(String destPathName, int option) throws BP0600_CommonEx {
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()) {
try {
m_files.getFile(i).saveAs(destPathName +
m_files.getFile(i).getFileName(), option);
}
catch (BP0600_CommonEx e) {
throw (e);
}
count++;
}
}
return count;
}
/**
*form 僨乕僞傪庢傞
*@return int
*@param dataHeader String
*@param fieldName 僼傽僀儖柤
*@since 01-00
*/
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;
}
/**
*僼傽僀儖Ext柤傪庢傞
*@return String
*@param fileName String
*@since 01-00
*/
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 "";
}
}
/**
*ContentType傪庢傞
*@return String
*@param dataHeader String
*@since 01-00
*/
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;
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -