📄 file.java
字号:
package mypkg.jspsmart.upload;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
/**
* An abstract representation of an uploaded file. This is a dependant object
* wich can't be directly instantiate.
*
* @author jimshen
*
*/
public class File {
private SmartUpload parent;
private int startData;
private int endData;
private int size;
private String fieldName;
private String fileName;
private String fileExt;
private String filePathName;
private String contentType;
private String contentDisp;
private String typeMime;
private String subTypeMime;
private boolean isMissing;
public static final int SAVEAS_AUTO = 0;
public static final int SAVEAS_VIRTUAL = 1;
public static final int SAVEAS_PHYSICAL = 2;
File() {
startData = 0;
endData = 0;
size = 0;
fieldName = "";
fileName = "";
fileExt = "";
filePathName = "";
contentType = "";
contentDisp = "";
typeMime = "";
subTypeMime = "";
isMissing = true;
}
/**
* Save the file to the specified destination.
*
* @param dstFilePathName
* the destination file name
* @throws IOException
* @throws SmartUploadException
*/
public void saveAs(String dstFilePathName) throws IOException,
SmartUploadException {
saveAs(dstFilePathName, 0);
}
/**
* Save the file to the specified destination.
*
* @param dstFilePathName
* The destination file name.
* @param optionSaveAs
* SAVEAS_AUTO : The method save in a virtual if it exists else
* consider the path as a physical if the porperty
* denyPhysicalPath is false. SAVEAS_VIRTUAL: The method save the
* file only if the path is a virtual path. SAVEAS_PHYSICAL: The
* method save the file only if the path is a physical path.
* @throws IOException
* @throws SmartUploadException
*/
public void saveAs(String dstFilePathName, int optionSaveAs)
throws IOException, SmartUploadException {
// Method invokes dubious new String() constructor; just use ""
// Creating a new java.lang.String object using the no-argument
// constructor wastes memory because the object so created will be
// functionally indistinguishable from the empty string constant
// "".\u00A0 Java guarantees that identical string constants will be
// represented by the same String object.\u00A0 Therefore, you should
// just use the empty string constant directly.
// String s1 = new String();
String s1 = "";
s1 = parent.getPhysicalPath(dstFilePathName, optionSaveAs);
if (s1 == null) {
throw new IllegalArgumentException(
"There is no specified destination file (1140).");
}
try {
java.io.File file = new java.io.File(s1);
FileOutputStream fileoutputstream = new FileOutputStream(file);
fileoutputstream.write(parent.binArray, startData, size);
fileoutputstream.close();
} catch (IOException ioexception) {
throw new SmartUploadException("File can't be saved (1120).");
}
}
/**
* Save the file in a field of a database.
*
* @param rs
* the resulset to update
* @param columnName
* the destination column
* @throws ServletException
* @throws IOException
* @throws SmartUploadException
* @throws SQLException
*/
public void fileToField(ResultSet rs, String columnName)
throws ServletException, IOException, SmartUploadException,
SQLException {
long l = 0L;
int i = 0x10000;
int j = 0;
int k = startData;
if (rs == null) {
throw new IllegalArgumentException(
"The RecordSet cannot be null (1145).");
}
if (columnName == null) {
throw new IllegalArgumentException(
"The columnName cannot be null (1150).");
}
if (columnName.length() == 0) {
throw new IllegalArgumentException(
"The columnName cannot be empty (1155).");
}
l = BigInteger.valueOf(size).divide(BigInteger.valueOf(i)).longValue();
j = BigInteger.valueOf(size).mod(BigInteger.valueOf(i)).intValue();
try {
for (int i1 = 1; (long) i1 < l; i1++) {
rs.updateBinaryStream(columnName, new ByteArrayInputStream(
parent.binArray, k, i), i);
k = k != 0 ? k : 1;
k = i1 * i + startData;
}
if (j > 0) {
rs.updateBinaryStream(columnName, new ByteArrayInputStream(
parent.binArray, k, j), j);
}
} catch (SQLException sqlexception) {
byte abyte0[] = new byte[size];
System.arraycopy(parent.binArray, startData, abyte0, 0, size);
rs.updateBytes(columnName, abyte0);
} catch (Exception exception) {
throw new SmartUploadException(
"Unable to save file in the DataBase (1130).");
}
}
/**
* Test if a file is present.
*
* @return true if the file is missing
*/
public boolean isMissing() {
return isMissing;
}
/**
* Returns the field's name of the HTML form.
*
* @return the field name.
*/
public String getFieldName() {
return fieldName;
}
/**
* Returns the file's name.
*
* @return the file name.
*/
public String getFileName() {
return fileName;
}
/**
* Returns the full path of the file.
*
* @return the full file path name.
*/
public String getFilePathName() {
return filePathName;
}
/**
* Returns the file's extension.
*
* @return the extension of the file.
*/
public String getFileExt() {
return fileExt;
}
/**
* Returns the content type.
*
* @return the content type of the file.
*/
public String getContentType() {
return contentType;
}
/**
* Returns the content disposition.
*
* @return the content disposition.
*/
public String getContentDisp() {
return contentDisp;
}
/**
* Returns the full content.
*
* @return the content
*/
public String getContentString() {
String s = new String(parent.binArray, startData, size);
return s;
}
/**
* Returns the MIME Type.
*
* @return String the MIME Type
* @throws IOException
*/
public String getTypeMIME() throws IOException {
return typeMime;
}
/**
* Returns the sub type mime.
*
* @return the sub MIME Type
*/
public String getSubTypeMIME() {
return subTypeMime;
}
/**
* Returns the file's size.
*
* @return the size of the file.
*/
public int getSize() {
return size;
}
protected int getStartData() {
return startData;
}
protected int getEndData() {
return endData;
}
protected void setParent(SmartUpload smartupload) {
parent = smartupload;
}
protected void setStartData(int i) {
startData = i;
}
protected void setEndData(int i) {
endData = i;
}
protected void setSize(int i) {
size = i;
}
protected void setIsMissing(boolean flag) {
isMissing = flag;
}
protected void setFieldName(String s) {
fieldName = s;
}
protected void setFileName(String s) {
fileName = s;
}
protected void setFilePathName(String s) {
filePathName = s;
}
protected void setFileExt(String s) {
fileExt = s;
}
protected void setContentType(String s) {
contentType = s;
}
protected void setContentDisp(String s) {
contentDisp = s;
}
protected void setTypeMIME(String s) {
typeMime = s;
}
protected void setSubTypeMIME(String s) {
subTypeMime = s;
}
/**
* Returns the byte corresponding to the table index containing the
* transmitted data
*
* @param index
* index of the Table
* @return the byte corresponding to the table index
*/
public byte getBinaryData(int index) {
if (startData + index > endData) {
throw new ArrayIndexOutOfBoundsException(
"Index Out of range (1115).");
}
if (startData + index <= endData) {
return parent.binArray[startData + index];
} else {
return 0;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -