[摘要]只需要include 然后是用就行了,其他还有取文件名,取文件大小,取文件类型,上传改名,取mulitForm中其他字段的值的功能 :) 这是小弟用Java做的第一个可以用的东西 :P 大家帮忙看看~~<%!// 取得Multi Form中字段的值 in(FieldName, Servlet...
只需要include 然后是用就行了,其他还有取文件名,取文件大小,取文件类型,上传改名,取mulitForm中其他字段的值的功能 :)
这是小弟用Java做的第一个可以用的东西 :P 大家帮忙看看~~
<%!// 取得Multi Form中字段的值 in(FieldName, ServletInputStream) out(String FieldValue)
public String getMultiFields(String FieldName,ServletInputStream sis) throws java.io.IOException{
byte[] b = new byte[4096];
String s = "";
String FieldValue="";
int a=0;
while((a = sis.readLine(b,0,b.length)) != -1){
s = new String(b,0,a);
if(s.indexOf("Content-Disposition: form-data; name=\""+FieldName+"\"")!=-1){
a = sis.readLine(b,0,b.length);
s = new String(b,0,a);
whiles:
while(1==1){
a = sis.readLine(b,0,b.length);
s = new String(b,0,a);
if(s.indexOf("-----------------------------")!=-1){
break whiles;
}else{
FieldValue=FieldValue+s;
}
}
}
}
sis.close();
return FieldValue;
}
%>
<%!// 取得上传文件的文件名 in(FieldName, ServletInputStream) out(String FileName)
public String getFileName(String FieldName,ServletInputStream sis) throws java.io.IOException{
String filenames="";
String s="";
byte[] b = new byte[4096];
int tempA;
int tempB;
int a=0;
while((a = sis.readLine(b,0,b.length)) != -1){
s=new String(b,0,a);
if(s.indexOf("Content-Disposition: form-data; name=\""+FieldName+"\"")!=-1){
if(s.indexOf("filename=") != -1){
tempA=s.indexOf("filename=\"");
tempA+=10;
tempB=s.indexOf("\"",tempA);
filenames=s.substring(tempA,tempB);
tempA=filenames.lastIndexOf("\\");
filenames=filenames.substring(tempA+1,filenames.length()).trim();
}
}
}
sis.close();
return filenames;
}
%>
<%!// 取得上传文件的文件类型 in(FieldName, ServletInputStream) out(String FileType)
public String getFileType(String FieldName,ServletInputStream sis) throws java.io.IOException{
String filetype="";
String s="";
byte[] b = new byte[4096];
int tempA;
int tempB;
int a=0;
while((a = sis.readLine(b,0,b.length)) != -1){
s=new String(b,0,a);
if(s.indexOf("Content-Disposition: form-data; name=\""+FieldName+"\"")!=-1){
if(s.indexOf("filename=") != -1){
a = sis.readLine(b,0,b.length);
s=new String(b,0,a);
tempA=s.indexOf("Content-Type: ");
tempA+=14;
filetype=s.substring(tempA,s.length()).trim();
}
}
}
sis.close();
return filetype;
}
%>
<%!// 取得文件大小 in(FieldName, ServletInputStream) out(int FileSize)
public int getFileSize(String FieldName,ServletInputStream sis) throws java.io.IOException{
int filesize=0;
String s="";
byte[] b = new byte[4096];
int tempA;
int tempB;
int a=0;
while((a = sis.readLine(b,0,b.length)) != -1){
s=new String(b,0,a);
if(s.indexOf("Content-Disposition: form-data; name=\""+FieldName+"\"")!=-1){
if(s.indexOf("filename=") != -1){
a = sis.readLine(b,0,b.length);
a = sis.readLine(b,0,b.length);
countsize:
while(1==1){
a = sis.readLine(b,0,b.length);
s = new String(b,0,a);
if(s.indexOf("-----------------------------")!=-1){
break countsize;
}else{
filesize=filesize+a;
}
}
}
}
}
sis.close();
return filesize;
}
%>
<%!// 上传文件 in(保存的绝对路径, 改名文件前缀名 *为空不改名, FieldName, ServletInputStream) out(String 上传后的Filename)
public String SaveFile(String savepath,String filename,String FieldName,ServletInputStream sis) throws java.io.IOException{
String filenames="";
String s="";
byte[] b = new byte[4096];
int tempA;
int tempB;
int a=0;
while((a = sis.readLine(b,0,b.length)) != -1){
s=new String(b,0,a);
if(s.indexOf("Content-Disposition: form-data; name=\""+FieldName+"\"")!=-1){
if(s.indexOf("filename=") != -1){
tempA=s.indexOf("filename=\"");
tempA+=10;
tempB=s.indexOf("\"",tempA);
filenames=s.substring(tempA,tempB);
tempA=filenames.lastIndexOf(".");
if(filename=="" filename==null){
tempB=filenames.lastIndexOf("\\");
filenames=filenames.substring(tempB+1,tempA).trim()+filenames.substring(tempA,filenames.length()).trim();
}else{
filenames=filename+filenames.substring(tempA,filenames.length()).trim();
}
a = sis.readLine(b,0,b.length);
a = sis.readLine(b,0,b.length);
FileOutputStream fos = new FileOutputStream(savepath+filenames);
savedata:
while(1==1){
a = sis.readLine(b,0,b.length);
s = new String(b,0,a);
if(s.indexOf("-----------------------------")!=-1){
break savedata;
}else{
fos.write(b,0,a);
}
}
fos.close();
}
}
}
sis.close();
return filenames;
}
%>
……