📄 smartupload.java
字号:
sb.append("%" + Integer.toHexString(k).
toUpperCase());
}
}
}
return sb.toString();
}
public void fieldToFile(ResultSet rs, String columnName, String destFilePathName)
throws SQLException, SmartUploadException, IOException, ServletException
{
try
{
if(config.getServletContext().getRealPath(destFilePathName) != null)
destFilePathName = config.getServletContext().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).");
}
}
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 getFileExt(String fileName)
{
String token = new String();
String value = new String();
int pos = 0;
int i = 0;
int start = 0;
int end = 0;
if(fileName == null)
{
return null;
} else
{
start = fileName.lastIndexOf(46) + 1;
end = fileName.length();
value = fileName.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 getTypeMIME(String ContentType)
{
String value = new String();
int pos = 0;
pos = ContentType.indexOf("/");
if(pos != -1)
return ContentType.substring(1, pos);
else
return ContentType;
}
private String getSubTypeMIME(String ContentType)
{
String value = new String();
int start = 0;
int end = 0;
start = ContentType.indexOf("/") + 1;
if(start != -1)
{
end = ContentType.length();
return ContentType.substring(start, end);
} else
{
return ContentType;
}
}
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 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 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 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 void setDeniedFilesList(String deniedFilesList)
throws SQLException, IOException, ServletException
{
int pos = 1;
int cpt = 0;
String ext = "";
if(deniedFilesList != null)
{
for(int i = 0; i < deniedFilesList.length(); i++)
{
ext = ext + deniedFilesList.charAt(i);
if(deniedFilesList.charAt(i) == ',')
{
ext = ext.substring(0, ext.length() - 1);
m_deniedFilesList.put(new Integer(pos), ext);
ext = "";
pos++;
}
if(i + 1 == deniedFilesList.length() && ext != "")
{
m_deniedFilesList.put(new Integer(pos), ext);
ext = "";
pos++;
}
}
} else
{
m_deniedFilesList = null;
}
}
public void setAllowedFilesList(String allowedFilesList)
{
int pos = 1;
int cpt = 0;
String ext = "";
if(allowedFilesList != null)
{
for(int i = 0; i < allowedFilesList.length(); i++)
{
ext = ext + allowedFilesList.charAt(i);
if(allowedFilesList.charAt(i) == ',')
{
ext = ext.substring(0, ext.length() - 1);
m_allowedFilesList.put(new Integer(pos), ext);
ext = "";
pos++;
}
if(i + 1 == allowedFilesList.length() && ext != "")
{
m_allowedFilesList.put(new Integer(pos), ext);
ext = "";
pos++;
}
}
} else
{
m_allowedFilesList = null;
}
}
public void setDenyPhysicalPath(boolean deny)
{
m_denyPhysicalPath = deny;
}
public void setContentDisposition(String contentDisposition)
{
m_contentDisposition = contentDisposition;
}
public void setTotalMaxFileSize(long totalMaxFileSize)
{
m_totalMaxFileSize = totalMaxFileSize;
}
public void setMaxFileSize(long maxFileSize)
{
m_maxFileSize = maxFileSize;
}
protected String getPhysicalPath(String path)
throws IOException
{
if(path == null)
throw new IllegalArgumentException("There is no specified destination file (1140).");
if(path.length() == 0)
throw new IllegalArgumentException("There is no specified destination file (1140).");
if(config.getServletContext().getRealPath(path) == null && m_denyPhysicalPath)
throw new IllegalArgumentException("Physical path is denied (1125).");
if(config.getServletContext().getRealPath(path) == null && !m_denyPhysicalPath)
return path;
else
return config.getServletContext().getRealPath(path);
}
public void uploadInFile(String destFilePathName)
throws SmartUploadException, IOException
{
int intsize = 0;
int pos = 0;
int readBytes = 0;
if(destFilePathName == null)
throw new IllegalArgumentException("There is no specified destination file (1025).");
if(destFilePathName.length() == 0)
throw new IllegalArgumentException("There is no specified destination file (1025).");
if(config.getServletContext().getRealPath(destFilePathName) == null && m_denyPhysicalPath)
throw new SecurityException("Physical path is denied (1035).");
intsize = m_request.getContentLength();
m_binArray = new byte[intsize];
for(; pos < intsize; pos += readBytes)
try
{
readBytes = m_request.getInputStream().read(m_binArray, pos, intsize - pos);
}
catch(Exception exception) { }
if(config.getServletContext().getRealPath(destFilePathName) != null)
destFilePathName = config.getServletContext().getRealPath(destFilePathName);
try
{
File file = new File(destFilePathName);
file.createNewFile();
FileOutputStream fileOut = new FileOutputStream(file);
fileOut.write(m_binArray);
fileOut.close();
}
catch(Exception e)
{
throw new SmartUploadException("The Form cannot be saved in the specified file (1030).");
}
}
protected byte m_binArray[];
protected HttpServletRequest m_request;
protected HttpServletResponse m_response;
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 Hashtable m_deniedFilesList;
private Hashtable m_allowedFilesList;
private boolean m_denyPhysicalPath;
private String m_contentDisposition;
private Files m_files;
private Request m_formRequest;
private ServletConfig config;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -