当前位置: 技术问答>java相关
请问如何实现在servlet通过页面将文件传送到服务器?
来源: 互联网 发布时间:2015-11-08
本文导语: 请问如何将客户机器上的文件,通过web浏览器传送到服务器?就像在web页面的邮件服务那样,把本地机的文件传送到服务器? | 下面是一个上载的方法: public void uploadFile(javax.servlet.http.Ht...
请问如何将客户机器上的文件,通过web浏览器传送到服务器?就像在web页面的邮件服务那样,把本地机的文件传送到服务器?
|
下面是一个上载的方法:
public void uploadFile(javax.servlet.http.HttpServletRequest req) throws IOException,Exception {
try {
String contentType=req.getContentType();
int contentLength=req.getContentLength();
java.io.DataInputStream bis=new java.io.DataInputStream(req.getInputStream());
int once = 0;
int total = 0;
byte[] buffer=new byte[contentLength];
while ((total=0)) {
once = bis.read(buffer,total,contentLength);
total += once;
}
int boundaryStart=contentType.indexOf("boundary=");
boundaryStart=boundaryStart+"boundary=".length();
String boundary="--"+contentType.substring(boundaryStart);
int pos=getFormNameIndex(buffer,"filename="".getBytes(),1)+"filename="".length();
int posEnd=getFormNameIndex(buffer,""".getBytes(),pos);
String filename=new String(buffer,pos,posEnd-pos);
pos=filename.lastIndexOf(".");
String filenames=System.currentTimeMillis()/1000+filename.substring(pos);
pos=getFormNameIndex(buffer,"Content-Type: ".getBytes(),1);
pos=getFormNameIndex(buffer,"rn".getBytes(),pos+1)+4;
int endpos=getFormNameIndex(buffer,boundary.getBytes(),pos+20);
int len=endpos-pos;
java.io.DataOutputStream bos= new java.io.DataOutputStream(new java.io.FileOutputStream(new java.io.File(filepath,filenames)));
bos.write(buffer,pos,len);
bis.close();
bos.close();
}catch(Exception exc) {
throw new Exception("upload failed.");
}finally{
}
}
public void uploadFile(javax.servlet.http.HttpServletRequest req) throws IOException,Exception {
try {
String contentType=req.getContentType();
int contentLength=req.getContentLength();
java.io.DataInputStream bis=new java.io.DataInputStream(req.getInputStream());
int once = 0;
int total = 0;
byte[] buffer=new byte[contentLength];
while ((total=0)) {
once = bis.read(buffer,total,contentLength);
total += once;
}
int boundaryStart=contentType.indexOf("boundary=");
boundaryStart=boundaryStart+"boundary=".length();
String boundary="--"+contentType.substring(boundaryStart);
int pos=getFormNameIndex(buffer,"filename="".getBytes(),1)+"filename="".length();
int posEnd=getFormNameIndex(buffer,""".getBytes(),pos);
String filename=new String(buffer,pos,posEnd-pos);
pos=filename.lastIndexOf(".");
String filenames=System.currentTimeMillis()/1000+filename.substring(pos);
pos=getFormNameIndex(buffer,"Content-Type: ".getBytes(),1);
pos=getFormNameIndex(buffer,"rn".getBytes(),pos+1)+4;
int endpos=getFormNameIndex(buffer,boundary.getBytes(),pos+20);
int len=endpos-pos;
java.io.DataOutputStream bos= new java.io.DataOutputStream(new java.io.FileOutputStream(new java.io.File(filepath,filenames)));
bos.write(buffer,pos,len);
bis.close();
bos.close();
}catch(Exception exc) {
throw new Exception("upload failed.");
}finally{
}
}
|
用smartupload 那个bean.
|
http://school.itnow.com.cn/article/20011127/200111272695.shtml
|
http://www.jspsmart.com/
|
你留下email,我发给你。