上传文件的小程序 站点:爱心种子小博士 关键字:上传文件的小程序
|
上传文件的小程序 一、上传表单:
<%@ page contentType="text/html;charset=gb2312" %>
<html><head><title>上传文件</title></head>
<body bgcolor="#ffffff" text="#000000">
<div align="center">
<p><b><font color="#ff6600">上传文件</font></b></p>
<FORM METHOD="POST" ACTION="tt.jsp" ENCTYPE="multipart/form-data">
<INPUT TYPE="FILE" NAME="FILE1" SIZE="50"><BR>
<INPUT TYPE="SUBMIT" VALUE="Upload">
</FORM>
二、处理上传的jsp <%@ page import="ex.*" %>
<%
try{
Upload load=new Upload();
load.setDir("d:\\tt\\");
load.doUpload(request,response);
}
catch( Exception e ){
return;
}
%>
OK!
三、上传程序 package ex;
import java.io.*;
import java.util.*;
import java.text.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* @author 2002/7/10 zty
*
*<p>
*<p> 【使用例】
*<p> try {
*<p> Upload load=new Upload();
*<p> load.setDir("i:\\ztytest\\");
*<p> load.doUpload(req,res);
*<p> }
*<p> catch( Exception e ) {
*<p> return;
*<p> }
*/
public class Upload extends HttpServlet {
public String strDir;
public Upload()
{
}
public void doUpload(HttpServletRequest req, HttpServletResponse res)
throws IOException
{
String boundary=req.getHeader("Content-Type");
int pos=boundary.indexOf(=);
boundary=boundary.substring(pos+1);
boundary="--"+ boundary;
ServletInputStream in=req.getInputStream() ;
byte[] bytes=new byte[512];
int state=0;
ByteArrayOutputStream buffer=new ByteArrayOutputStream();
String name=null;
String value=null;
String filename=null;
String contentType=null;
int i=in.readLine(bytes,0,512);
while (-1!=i)
{
String st=new String(bytes,0,i);
if (st.startsWith(boundary))
{
state=0;
System.out.println(filename);
if (null!=name)
{
if(buffer.size()>2)
{
FileOutputStream outStream = new FileOutputStream(strDir+filename);
outStream.write(buffer.toByteArray(), 0, buffer.toByteArray().length);
outStream.close();
}
name=null;
value=null;
filename=null;
contentType=null;
buffer=new ByteArrayOutputStream();
}
}//end of st.startsWith(boundary)
else if (st.startsWith("Content-Disposition: form-data") && state==0)
{
StringTokenizer tokenizer=new StringTokenizer(st,";=\"");
while(tokenizer.hasMoreTokens())
{
String token=tokenizer.nextToken();
if(token.trim().startsWith("name"))
{
name=tokenizer.nextToken();
state=2;
}
else if(token.trim().startsWith("filename"))
{
filename=tokenizer.nextToken();
StringTokenizer ftokenizer=new StringTokenizer(filename,"\\");
filename=ftokenizer.nextToken();
while(ftokenizer.hasMoreTokens())
filename=ftokenizer.nextToken();
state=1;
break;
}
}
}//end state=0
else if (st.startsWith("Content-Type") && state==1)
{
pos=st.indexOf(":");
contentType=st.substring(pos+2,st.length()-2);
}//end state=1
else if (state==1)
state=3;
else if (st.equals("\r\n")&&state==2)
state=4;
else if (state==3)
buffer.write(bytes,0,i);
else if (state==4)
value=value==null?st:value+st;
i=in.readLine(bytes,0,512);
}//end while
}
public void setDir(String strSavePath)
{
strDir =strSavePath;
}
} |
|
|
|