?? smartupload.java
字號:
} 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 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{
destFileName=new String(destFileName.getBytes("GB2312"),"ISO8859-1");
m_response.setHeader("Content-Disposition",
"attachment; filename=".concat(String.valueOf(
destFileName)));
}
}
m_response.getOutputStream().write(b,0,b.length);
}
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).");
}
}
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 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 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{
String ext="";
if(deniedFilesList!=null){
ext="";
for(int i=0;i<deniedFilesList.length();i++){
if(deniedFilesList.charAt(i)==','){
if(!m_deniedFilesList.contains(ext)){
m_deniedFilesList.addElement(ext);
}
ext="";
} else{
ext=ext+deniedFilesList.charAt(i);
}
}
if(ext!=""){
m_deniedFilesList.addElement(ext);
}
} else{
m_deniedFilesList=null;
}
}
public void setAllowedFilesList(String allowedFilesList){
String ext="";
if(allowedFilesList!=null){
ext="";
for(int i=0;i<allowedFilesList.length();i++){
if(allowedFilesList.charAt(i)==','){
if(!m_allowedFilesList.contains(ext)){
m_allowedFilesList.addElement(ext);
}
ext="";
} else{
ext=ext+allowedFilesList.charAt(i);
}
}
if(ext!=""){
m_allowedFilesList.addElement(ext);
}
} else{
m_allowedFilesList=null;
}
}
public void setDenyPhysicalPath(boolean deny){
m_denyPhysicalPath=deny;
}
public void setForcePhysicalPath(boolean force){
m_forcePhysicalPath=force;
}
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 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:"/";
File physicalPath=new 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).");
}
}
if(option==1){
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){
throw new IllegalArgumentException("The path is not a virtual path.");
} else{
throw new IllegalArgumentException("This path does not exist (1135).");
}
}
if(option==2){
if(isPhysical){
if(m_denyPhysicalPath){
throw new IllegalArgumentException("Physical path is denied (1125).");
} else{
return filePathName;
}
}
if(isVirtual(path)){
throw new IllegalArgumentException("The path is not a physical path.");
} else{
throw new IllegalArgumentException("This path does not exist (1135).");
}
} else{
return null;
}
}
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(!isVirtual(destFilePathName)&&m_denyPhysicalPath){
throw new SmartUploadException("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 e){
throw new SmartUploadException("Unable to upload.");
}
}
if(isVirtual(destFilePathName)){
destFilePathName=m_application.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).");
}
}
private boolean isVirtual(String pathName){
if(m_application.getRealPath(pathName)!=null){
File virtualFile=new File(m_application.getRealPath(pathName));
return virtualFile.exists();
} else{
return false;
}
}
/**
* 將文件名中的漢字轉(zhuǎn)為UTF8編碼的串,以便下載時(shí)能正確顯示另存的文件名.
* 縱橫軟件制作中心雨亦奇2003.08.01
* @param s 原文件名
* @return 重新編碼后的文件名
*/
public String toUtf8String(String s){
StringBuffer sb=new StringBuffer();
for(int i=0;i<s.length();i++){
char c=s.charAt(i);
if(c>=0&&c<=255){
sb.append(c);
} else{
byte[] b;
try{
b=Character.toString(c).getBytes("utf-8");
} catch(Exception ex){
System.out.println(ex);
b=new byte[0];
}
for(int j=0;j<b.length;j++){
int k=b[j];
if(k<0)k+=256;
sb.append("%"+Integer.toHexString(k).
toUpperCase());
}
}
}
return sb.toString();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -