谁有用Servlet或JavaBean实现文件上传的源码?

rainbow2k 2002-05-09 05:57:06
多谢了。
...全文
6 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
lianyunzxp 2002-05-09
Faint.........
怎么乱七八糟的呀
呵呵,自己整理一下了,不好意思
回复
lianyunzxp 2002-05-09
搜索一下,挺多的
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class UploadFileServlet extends HttpServlet
{
public void doPost(HttpServletRequest req,
HttpServletResponse res)
throws IOException, ServletException{
final int NONE=0;
final int DATAHEADER=1;
final int FILEDATA=2;
final int FIELDDATA=3;
int totalbytes=req.getContentLength();
byte[] b=new byte[totalbytes];
String contentType=req.getContentType();
String fieldname="";
String fieldvalue="";
String filename="";
String boundary="";
String lastboundary="";
int fileSize=0;
Hashtable formfields=new Hashtable();
int pos =contentType.indexOf("boundary=");
String fileID;
if(pos!=-1){
pos+="boundary=".length();
boundary="--"+contentType.substring(pos);
lastboundary=boundary+"--";
}
int state=NONE;
DataInputStream in=new DataInputStream(req.getInputStream());
in.readFully(b);
in.close();
String reqcontent=new String(b);
BufferedReader reqbuf=new BufferedReader(new StringReader(reqcontent));
boolean flag=true;
int i=0;
while(flag==true){
String s=reqbuf.readLine();
if(s==lastboundary||s==null)break;
switch(state){
case NONE:
if(s.startsWith(boundary)){
state=DATAHEADER;
i+=1;
}
case DATAHEADER:
pos=s.indexOf("filename=");
if(pos==-1){
pos=s.indexOf("name=");
pos+="name=".length()+1;
s=s.substring(pos);
int l=s.length();
s=s.substring(0,l-1);
fieldname=s;
state=FIELDDATA;
}
else{
String temp=s;
pos=s.indexOf("filename=");
pos+="filename=".length()+1;
s=s.substring(pos);
int l=s.length();
s=s.substring(0,l-1);
pos=s.lastIndexOf("\\");
s=s.substring(pos+1);
filename=s;
pos=byteIndexOf(b,temp,0);
b=subBytes(b,pos+temp.getBytes().length+2,b.length);
s=reqbuf.readLine();
File f=new File(filename);
DataOutputStream fileout=new DataOutputStream(new FileOutputStream(f));
b=subBytes(b,s.getBytes().length+4,b.length);
pos=byteIndexOf(b,boundary,0);
b=subBytes(b,0,pos-1);
fileout.write(b,0,b.length-1);
fileSize=b.length-1;
state=FILEDATA;
}
break;
case FIELDDATA:
s=reqbuf.readLine();
fieldvalue=s;
formfields.put(fieldname,fieldvalue);
state=NONE;
break;
case FILEDATA:
while((!s.startsWith(boundary))&&(!s.startsWith(lastboundary))){
s=reqbuf.readLine();
if(s.startsWith(boundary)){
state=DATAHEADER;
}
else break;
break;
}
}
}
res.setContentType("text/html;charset=gb2312");
PrintWriter out=res.getWriter();
out.println("<html>");
out.println("<head><title>文件上传结果</title></head>");
out.println("<body>");
out.println("<h1>文件上传结果</h1><hr>");
out.println("ID为"+formfields.get("FileID")+"的文件"+filename+"已经上传!"+"文件长度:"+fileSize+"字节");
out.println("</body>");
out.println("</html>");
}
private static int byteIndexOf(byte[] b,String s,int start)
{
return byteIndexOf(b,s.getBytes(),start);
}
private static int byteIndexOf(byte[] b,byte[] s,int start)
{
int i;
if(s.length==0)
{
return 0;
}
int max=b.length-s.length;
if(max<0)
return -1;
if(start>max)
return -1;
if(start<0)
start=0;
search:
for(i=start;i<=max;i++)
{
if(b[i]==s[0])
{
int k=1;
while(k<s.length)
{
if(b[k+i]!=s[k])
{
continue search;
}
k++;
}
return i;
}
}
return -1;
}
private static byte[] subBytes(byte[] b,int from ,int end)
{
byte[]result=new byte[end-from];
System.arraycopy(b,from,result,0,end-from);
return result;
}
private static String subBytesString(byte[] b,int from, int end)
{
return new String(subBytes(b,from,end));
}
}
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告