当前位置: 技术问答>java相关
100分交换一个文件上传的.class....
来源: 互联网 发布时间:2015-05-15
本文导语: 100分交换一个文件上传的.class.... | 一个例子!! import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public class UploadFileServlet extends HttpServlet { public void doPost(HttpServ...
100分交换一个文件上传的.class....
|
一个例子!!
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("");
out.println("文件上传结果");
out.println("");
out.println("文件上传结果");
out.println("ID为"+formfields.get("FileID")+"的文件"+filename+"已经上传!"+"文件长度:"+fileSize+"字节");
out.println("");
out.println("");
}
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(maxmax)
return -1;
if(start
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("");
out.println("文件上传结果");
out.println("");
out.println("文件上传结果");
out.println("ID为"+formfields.get("FileID")+"的文件"+filename+"已经上传!"+"文件长度:"+fileSize+"字节");
out.println("");
out.println("");
}
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(maxmax)
return -1;
if(start