服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

一个文件上传javabean

 


package  com.upload;  

import  java.io.*;  
import  javax.servlet.http.httpservletrequest;  
import  javax.servlet.servletinputstream;  
import  javax.servlet.servletexception;  

public  class  upload{  
private  static  string  newline  =  "/n";  
private  string  uploaddirectory  =  ".";  
private  string  contenttype  =  "";  
private  string  characterencoding  =  "";  

private  string  getfilename(string  s){  
int  i  =  s.lastindexof("//");  
if(i  <  0  //  i  >=  s.length()  -  1){  
i  =  s.lastindexof("/");  
if(i  <  0  //  i  >=  s.length()  -  1)  
return  s;  
}  
return  s.substring(i  +  1);  
}  

public  void  setuploaddirectory(string  s){  
uploaddirectory  =  s;  
}  

public  void  setcontenttype(string  s){  
contenttype  =  s;  
int  j;  
if((j  =  contenttype.indexof("boundary="))  !=  -1){  
contenttype  =  contenttype.substring(j  +  9);  
contenttype  =  "--"  +  contenttype;  
}  
}  

public  void  setcharacterencoding(string  s){  
characterencoding  =  s;  
}  

public  void  uploadfile(  httpservletrequest  req)  throws  servletexception,  ioexception{  
setcharacterencoding(req.getcharacterencoding());  
setcontenttype(req.getcontenttype());  
uploadfile(req.getinputstream());  
}  

public  void  uploadfile(  servletinputstream  servletinputstream)  throws  servletexception,  ioexception{  

string  s5  =  null;  
string  filename  =  null;  
byte  linebyte[]  =  new  byte[4096];  
byte  outlinebyte[]  =  new  byte[4096];  
int  ai[]  =  new  int[1];  
int  ai1[]  =  new  int[1];  

string  line;  
//得到文件名  
while((line  =  readline(linebyte,  ai,  servletinputstream,  characterencoding))  !=  null){  
int  i  =  line.indexof("filename=");  
if(i  >=  0){  
line  =  line.substring(i  +  10);  
if((i  =  line.indexof("/""))  >  0)  
line  =  line.substring(0,  i);  
break;  
}  
}  

filename  =  line;  

if(filename  !=  null  &&  !filename.equals("/"")){  
filename  =  getfilename(filename);  

string  scontenttype  =  readline(linebyte,  ai,  servletinputstream,  characterencoding);  
if(scontenttype.indexof("content-type")  >=  0)  
readline(linebyte,  ai,  servletinputstream,  characterencoding);  

//file(string  parent,  string  child)  
//creates  a  new  file  instance  from  a  parent  pathname  string  
//and  a  child  pathname  string.  
file  file  =  new  file(uploaddirectory,  filename);  

//fileoutputstream(file  file)  
//creates  a  file  output  stream  to  write  to  the  file  represented  
//by  the  specified  file  object.  
fileoutputstream  fileoutputstream  =  new  fileoutputstream(file);  

while((scontenttype  =  readline(linebyte,  ai,  servletinputstream,  characterencoding))  !=  null){  
if(scontenttype.indexof(contenttype)  ==  0  &&  linebyte[0]  ==  45)  
break;  

if(s5  !=  null){  
//write(byte[]  b,  int  off,  int  len)  
//writes  len  bytes  from  the  specified  byte  array  starting  
//at  offset  off  to  this  file  output  stream.  
fileoutputstream.write(outlinebyte,  0,  ai1[0]);  
fileoutputstream.flush();  
}  
s5  =  readline(outlinebyte,  ai1,  servletinputstream,  characterencoding);  
if(s5  ==  null  //  s5.indexof(contenttype)  ==  0  &&  outlinebyte[0]  ==  45)  
break;  
fileoutputstream.write(linebyte,  0,  ai[0]);  
fileoutputstream.flush();  
}  

byte  byte0;  
if(newline.length()  ==  1)  
byte0  =  2;  
else  
byte0  =  1;  
if(s5  !=  null  &&  outlinebyte[0]  !=  45  &&  ai1[0]  >  newline.length()  *  byte0)  
fileoutputstream.write(outlinebyte,  0,  ai1[0]  -  newline.length()  *  byte0);  
if(scontenttype  !=  null  &&  linebyte[0]  !=  45  &&  ai[0]  >  newline.length()  *  byte0)  
fileoutputstream.write(linebyte,  0,  ai[0]  -  newline.length()  *  byte0);  

fileoutputstream.close();  
}  
}  

private  string  readline(byte  linebyte[],  int  ai[],  
servletinputstream  servletinputstream,  
string  characterencoding){  

try{  
//readline(byte[]  buffer,  int  offset,  int  length)  
//reads  a  line  from  the  post  data.  
ai[0]  =  servletinputstream.readline(linebyte,  0,  linebyte.length);  
if(ai[0]  ==  -1)  
return  null;  
}catch(ioexception  _ex){  
return  null;  
}  
try{  
if(characterencoding  ==  null){  
//用缺省的编码方式把给定的byte数组转换为字符串  
//string(byte[]  bytes,  int  offset,  int  length)  
return  new  string(linebyte,  0,  ai[0]);  
}else{  
//用给定的编码方式把给定的byte数组转换为字符串  
//string(byte[]  bytes,  int  offset,  int  length,  string  enc)  
return  new  string(linebyte,  0,  ai[0],  characterencoding);  
}  
}catch(exception  _ex){  
return  null;  
}  
}  
/*  
public  int  readline(byte[]  buffer,  
int  offset,  
int  length)  throws  java.io.ioexception  
从post来的数据中读一行  
参数:  
buffer  -  buffer  to  hold  the  line  data  
offset  -  offset  into  the  buffer  to  start  
length  -  maximum  number  of  bytes  to  read.  
returns:  
number  of  bytes  read  or  -1  on  the  end  of  line.  
*/  
}  


upload.jsp  
<%@page  import="com.upload.upload"%>  
<%  
string  dir  =  "/home/tonywan/upload";  

upload  upload  =  new  upload();  
upload.setuploaddirectory(dir);  
upload.uploadfile(request);  
out.print("<center><font  color=red>成功上载文件至"  +  dir  +  "</font></center>");  
%>  


upload.html  
<center>file  upload  test</center>  
<br>  
<table><tr>  
<form  method="post"  enctype="multipart/form-data"  action="upload.jsp">  
<td>  <input  type=file  size=20  name="fname">  </td>  
<td>  <input  type=submit  value=upload>  </td>  </form>  
</tr></table>  



不知道大家看地懂吗?反正使用起来很简单的,只要设置一下上传路径就可以了。

扫描关注微信公众号