📄 smartupload.java
字号:
response.getOutputStream().write(abyte0, 0, j);
}
fileinputstream.close();
}
/**
* downloadField Downloads a field from a database's table
*
* @param rs
* the resulstet
* @param columnName
* the column name
* @param contentType
* the content type
* @param destFileName
* the suggested file name
* @throws ServletException
* @throws IOException
* @throws SQLException
*/
public void downloadField(ResultSet rs, String columnName,
String contentType, String destFileName) throws ServletException,
IOException, SQLException {
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 abyte0[] = rs.getBytes(columnName);
if (contentType == null) {
response.setContentType("application/x-msdownload");
} else {
if (contentType.length() == 0) {
response.setContentType("application/x-msdownload");
} else {
response.setContentType(contentType);
}
}
response.setContentLength(abyte0.length);
if (destFileName == null) {
response.setHeader("Content-Disposition", "attachment;");
} else {
if (destFileName.length() == 0) {
response.setHeader("Content-Disposition", "attachment;");
} else {
response.setHeader("Content-Disposition",
"attachment; filename=" + destFileName);
}
}
response.getOutputStream().write(abyte0, 0, abyte0.length);
}
/**
* Save a field from a database's table to a file
*
* @param rs
* the resulstet
* @param columnName
* the column name
* @param destFileName
* the suggested file name
* @throws ServletException
* @throws IOException
* @throws SmartUploadException
* @throws SQLException
*/
public void fieldToFile(ResultSet rs, String columnName, String destFileName)
throws ServletException, IOException, SmartUploadException,
SQLException {
try {
if (application.getRealPath(destFileName) != null) {
destFileName = application.getRealPath(destFileName);
}
InputStream inputstream = rs.getBinaryStream(columnName);
FileOutputStream fileoutputstream = new FileOutputStream(
destFileName);
int i;
while ((i = inputstream.read()) != -1) {
fileoutputstream.write(i);
}
fileoutputstream.close();
} catch (Exception exception) {
throw new SmartUploadException(
"Unable to save file from the DataBase (1020).");
}
}
private String getDataFieldValue(String s, String s1) {
String s2 = "";
String s3 = "";
int i = 0;
s2 = s1 + "=" + '"';
i = s.indexOf(s2);
if (i > 0) {
int j = i + s2.length();
int k = j;
s2 = "\"";
int l = s.indexOf(s2, j);
if (k > 0 && l > 0) {
s3 = s.substring(k, l);
}
}
return s3;
}
private String getFileExt(String s) {
String s1; // = new String();
int i = 0;
int j = 0;
if (s == null) {
return null;
}
i = s.lastIndexOf('.') + 1;
j = s.length();
s1 = s.substring(i, j);
if (s.lastIndexOf('.') > 0) {
return s1;
} else {
return "";
}
}
private String getContentType(String s) {
String s1 = ""; // = new String();
String s2 = ""; // = new String();
int i = 0;
// boolean flag = false;
s1 = "Content-Type:";
i = s.indexOf(s1) + s1.length();
if (i != -1) {
int j = s.length();
s2 = s.substring(i, j);
}
return s2;
}
private String getTypeMIME(String s) {
// String s1 = new String();
int i = 0;
i = s.indexOf("/");
if (i != -1) {
return s.substring(1, i);
} else {
return s;
}
}
private String getSubTypeMIME(String s) {
// String s1 = new String();
// boolean flag = false;
int i = 0;
i = s.indexOf("/") + 1;
if (i != -1) {
int j = s.length();
return s.substring(i, j);
} else {
return s;
}
}
private String getContentDisp(String s) {
// String s1 = new String();
String s1 = "";
int i = 0;
int j = 0;
i = s.indexOf(":") + 1;
j = s.indexOf(";");
s1 = s.substring(i, j);
return s1;
}
private void getDataSection() {
// boolean flag = false;
// String s = "";
// String s = new String();
int i = currentIndex;
int j = 0;
int k = boundary.length();
startData = currentIndex;
endData = 0;
while (i < totalBytes) {
if (binArray[i] == (byte) boundary.charAt(j)) {
if (j == k - 1) {
endData = ((i - k) + 1) - 3;
break;
}
i++;
j++;
} else {
i++;
j = 0;
}
}
currentIndex = endData + k + 3;
}
private String getDataHeader() {
// boolean flag = false;
int i = currentIndex;
int j = 0;
for (boolean flag1 = false; !flag1;) {
if (binArray[currentIndex] == 13
&& binArray[currentIndex + 2] == 13) {
flag1 = true;
j = currentIndex - 1;
currentIndex = currentIndex + 2;
} else {
currentIndex++;
}
}
String s = new String(binArray, i, (j - i) + 1);
return s;
}
private String getFileName(String s) {
// String s1 = ""; // = new String();
// String s2 = ""; // = new String();
// boolean flag = false;
// boolean flag1 = false;
// boolean flag2 = false;
int i = 0;
i = s.lastIndexOf('/');
if (i != -1) {
return s.substring(i + 1, s.length());
}
i = s.lastIndexOf('\\');
if (i != -1) {
return s.substring(i + 1, s.length());
} else {
return s;
}
}
/**
* Set the list of denied files. Contains the list of file's extensions
* which are denied to be uploaded. If this list is empty then no files are
* denied. All the files with an extension in the DeniedFileslist are denied
* even if they are in the AllowedFilesList.
*
* In order to deny the files without an extension the list must contain two
* commas ",,". By defect this property is empty.
*
* @param deniedFilesList
* The list of denied files
* @throws ServletException
* @throws IOException
* @throws SQLException
*/
public void setDeniedFilesList(String deniedFilesList)
throws ServletException, IOException, SQLException {
if (deniedFilesList != null) {
String s2 = "";
for (int i = 0; i < deniedFilesList.length(); i++) {
if (deniedFilesList.charAt(i) == ',') {
if (!this.deniedFilesList.contains(s2)) {
this.deniedFilesList.addElement(s2);
}
s2 = "";
} else {
s2 = s2 + deniedFilesList.charAt(i);
}
}
if (!s2.equals("")) {
this.deniedFilesList.addElement(s2);
}
} else {
this.deniedFilesList = null;
}
}
/**
* Set the list of allowed files. Contains the list of file's extensions
* which are allowed to be uploaded. If this list is empty then all files
* are allowed. If the list is not empty then only the files with a
* specified extension are allowed. In order to allow the files without
* extension list must contain two commas ",,". By defect this property is
* empty.
*
* @param allowedFilesList
* the list of allowed files
*/
public void setAllowedFilesList(String allowedFilesList) {
if (allowedFilesList != null) {
String s2 = "";
for (int i = 0; i < allowedFilesList.length(); i++) {
if (allowedFilesList.charAt(i) == ',') {
if (!this.allowedFilesList.contains(s2)) {
this.allowedFilesList.addElement(s2);
}
s2 = "";
} else {
s2 = s2 + allowedFilesList.charAt(i);
}
}
if (!s2.equals("")) {
this.allowedFilesList.addElement(s2);
}
} else {
this.allowedFilesList = null;
}
}
/**
* Sets if the component deny physical path. Prevents to access a path other
* that a virtual path of the Web Server. By defect this property is false.
*
* @param deny
* is a boolean
*/
public void setDenyPhysicalPath(boolean deny) {
denyPhysicalPath = deny;
}
/**
* Sets if the component force physical path. By default, the component
* consider a path as a virtual path. To oblige the component to consider
* all path as physical path, set this property to true.
*
* @param force
* The boolean value
*/
public void setForcePhysicalPath(boolean force) {
}
/**
* Sets the Content Disposition. Contains the string of the Content
* Disposition Hearders for the MIME Type.
*
* @param contentDisposition
* The content disposition
*/
public void setContentDisposition(String contentDisposition) {
this.contentDisposition = contentDisposition;
}
/**
* Sets the total maximum file size. Is the maximum allowed size of all
* files to be uploaded. If this value is null then there is no limit for
* the total file's size uploaded. By defect this property is null.
*
* @param totalMaxFileSize
* the total max file size
*/
public void setTotalMaxFileSize(long totalMaxFileSize) {
this.totalMaxFileSize = totalMaxFileSize;
}
/**
* Sets the maximum file size. Is the maximum allowed size of one file to be
* uploaded. If this value is null then there is no limit for the file's
* size uploaded. By defect this property is null.
*
* @param maxFileSize
* the max file size
*/
public void setMaxFileSize(long maxFileSize) {
this.maxFileSize = maxFileSize;
}
protected String getPhysicalPath(String s, int i) throws IOException {
String s1 = "";
String s2 = "";
String s3 = "";
boolean flag = false;
s3 = System.getProperty("file.separator");
if (s == null) {
throw new IllegalArgumentException(
"There is no specified destination file (1140).");
}
if (s.equals("")) {
throw new IllegalArgumentException(
"There is no specified destination file (1140).");
}
if (s.lastIndexOf("\\") >= 0) {
s1 = s.substring(0, s.lastIndexOf("\\"));
s2 = s.substring(s.lastIndexOf("\\") + 1);
}
if (s.lastIndexOf("/") >= 0) {
s1 = s.substring(0, s.lastIndexOf("/"));
s2 = s.substring(s.lastIndexOf("/") + 1);
}
s1 = s1.length() != 0 ? s1 : "/";
java.io.File file = new java.io.File(s1);
if (file.exists()) {
flag = true;
}
if (i == 0) {
if (isVirtual(s1)) {
s1 = application.getRealPath(s1);
if (s1.endsWith(s3)) {
s1 = s1 + s2;
} else {
s1 = s1 + s3 + s2;
}
return s1;
}
if (flag) {
if (denyPhysicalPath) {
throw new IllegalArgumentException(
"Physical path is denied (1125).");
} else {
return s;
}
} else {
throw new IllegalArgumentException(
"This path does not exist (1135).");
}
}
if (i == 1) {
if (isVirtual(s1)) {
s1 = application.getRealPath(s1);
if (s1.endsWith(s3)) {
s1 = s1 + s2;
} else {
s1 = s1 + s3 + s2;
}
return s1;
}
if (flag) {
throw new IllegalArgumentException(
"The path is not a virtual path.");
} else {
throw new IllegalArgumentException(
"This path does not exist (1135).");
}
}
if (i == 2) {
if (flag) {
if (denyPhysicalPath) {
throw new IllegalArgumentException(
"Physical path is denied (1125).");
} else {
return s;
}
}
if (isVirtual(s1)) {
throw new IllegalArgumentException(
"The path is not a physical path.");
} else {
throw new IllegalArgumentException(
"This path does not exist (1135).");
}
} else {
return null;
}
}
/**
* The uploadInFile method creates a new file with all data of the POST
* form. This method can't be called if the upload() method is already
* called.
*
* @param destFilePathName
* The path of the file
* @throws IOException
* @throws SmartUploadException
*/
public void uploadInFile(String destFilePathName) throws IOException,
SmartUploadException {
int i = 0;
int j = 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 (!isVirtual(destFilePathName) && denyPhysicalPath) {
throw new SecurityException("Physical path is denied (1035).");
}
i = request.getContentLength();
binArray = new byte[i];
int k;
for (; j < i; j += k) {
try {
k = request.getInputStream().read(binArray, j, i - j);
} catch (Exception exception) {
throw new SmartUploadException("Unable to upload.");
}
}
if (isVirtual(destFilePathName)) {
destFilePathName = application.getRealPath(destFilePathName);
}
try {
java.io.File file = new java.io.File(destFilePathName);
FileOutputStream fileoutputstream = new FileOutputStream(file);
fileoutputstream.write(binArray);
fileoutputstream.close();
} catch (Exception exception1) {
throw new SmartUploadException(
"The Form cannot be saved in the specified file (1030).");
}
}
private boolean isVirtual(String s) {
if (application.getRealPath(s) != null) {
java.io.File file = new java.io.File(application.getRealPath(s));
return file.exists();
} else {
return false;
}
}
public static String encodeString(String s) throws UnsupportedEncodingException {
String str = URLEncoder.encode(s, "UTF-8");
str = str.replace("+", "%20");
if (str.length() > 150) {
str = new String(s.getBytes("GB2312"), "ISO8859-1");
str = str.replace(" ", "%20");
}
return str;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -