拷了一个upload,自己看看,download太简单了吧,就懒得写了/*
里面有不少bug,try your best
good luck Demo_upload使用说明
html 文件
upload.java 文件
RFC 文档
*/
/*
Demo_upload使用说明 Demo_upload.html
使用Form中的TYPE=file来提交文件,一个<input type=file ....>提交一个文件
,
使用多个<input type=file ....>可以提交多个文件,提交之后文件被在D:\Jav
aWebServer2.0\servlets\file
目录下以原有文件名存储。对于其他TYPE类型的INPUT在SERVLET中不做处理仅仅
将其name和value在浏览器上
显示出来。
*Form的描述METHOD必须是POST,ENCTYPE必须是"multipart/form-data"
例:<form method=post action="/servlet/Demo_upload.class" ENCTYPE="mul
tipart/form-data"> Demo_upload.class(Servlet)
放于相应Web的Servlet目录下,由Form的submit激活。
详细程序流程见RFC文档:Request For Comments: 1867 和 upload.java程序中
的注释。
*/
/*
<html>
<title>UpLoad File</title>
<body>
<form method=post action="/servlet/Demo_upload.class" ENCTYPE="multipa
rt/form-data">
<input type=textbox name="text"><br>
<P>File: <input type=file name="USERFILE01">
<br>
<P>File: <input type=file name="USERFILE02">
<br>
<input type=submit name=submit value=submit>
<input type=reset name=reset value=reset>
</body>
</html>
*/
//*
//-------
//-
//- upload.java
//-
//- Author: AR Williamson
//- Date: May 1998
//- Copyright: Maybe used for non-commercial use.
//-
//------- import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*; public class upload extends HttpServlet {
public void doPost( HttpServletRequest _req, HttpServletResponse _re
s ) throws ServletException, IOException{ parseMultiForm pMF = new parseMultiForm( _req ); String param;
while ( (param = pMF.getNextParameter()) != null ){
if ( param.equalsIgnoreCase("USERFILE") ){
FileOutputStream OutFile = new FileOutputStream( pMF.getFilena
me() );
pMF.getParameter( OutFile );
OutFile.close();
}else{
System.out.println( "Key : " + param );
System.out.println( "Value: " + pMF.getParameter() );
}
}
_res.setStatus( HttpServletResponse.SC_NO_CONTENT );
}
} class parseMultiForm extends java.lang.Object {
private ServletInputStream In;
private byte buffer[] = new byte[4096];
private String delimitor = null;
private String filename=null; public parseMultiForm( ServletRequest _req ) throws IOException{
In = _req.getInputStream();
delimitor = _req.getContentType();
if ( delimitor.indexOf("boundary=") != -1 ){
delimitor = delimitor.substring( delimitor.indexOf("boundary=")+
9, delimitor.length() );
delimitor = "--" + delimitor;
}
} private String readLine(){
try{
int noData = In.readLine( buffer, 0, buffer.length );
if ( noData != -1 )
return new String( buffer, 0, noData, "ISO-8859-1");
}catch(Exception E){}
return null;
} void test() throws IOException{
String LineIn;
while( (LineIn=readLine()) != null )
System.out.println( LineIn );
} public String getFilename(){
return filename;
} public String getParameter(){
return readLine();
} public String getNextParameter() {
try{
String LineIn=null, paramName=null; while ( (LineIn=readLine()) != null ){
if ( LineIn.indexOf( "name=" ) != -1 ){
int c1 = LineIn.indexOf( "name=" );
int c2 = LineIn.indexOf( "\"", c1+6 );
paramName = LineIn.substring( c1+6, c2 );
if ( LineIn.indexOf( "filename=") != -1 ){
c1 = LineIn.indexOf( "filename=" );
c2 = LineIn.indexOf( "\"", c1+10 );
filename = LineIn.substring( c1+10, c2 );
if ( filename.lastIndexOf( "\\" ) != -1 )
filename = filename.substring( filename.lastIndexOf( "\" )+1 ); if ( filename.length() == 0 )
filename = null;
} //- Move the pointer to the start of the data
LineIn = readLine();
if ( LineIn.indexOf( "Content-Type" ) != -1 )
LineIn = readLine(); return paramName;
}
}
}
catch( Exception E ){}
return null;
} public boolean getParameter( OutputStream _Out ){
try{
int noData;
while ( (noData=In.readLine(buffer,0,buffer.length)) != -1 ){ if ( buffer[0] == '-' ){
if ( new String( buffer, 0, noData, "ISO-8859-1").indexOf(
delimitor) == 0 )
break;
}else
_Out.write( buffer, 0, noData );
}
_Out.flush();
return true;
}
catch( Exception E ){
System.out.println( E );
} return false;
}
}
里面有不少bug,try your best
good luck Demo_upload使用说明
html 文件
upload.java 文件
RFC 文档
*/
/*
Demo_upload使用说明 Demo_upload.html
使用Form中的TYPE=file来提交文件,一个<input type=file ....>提交一个文件
,
使用多个<input type=file ....>可以提交多个文件,提交之后文件被在D:\Jav
aWebServer2.0\servlets\file
目录下以原有文件名存储。对于其他TYPE类型的INPUT在SERVLET中不做处理仅仅
将其name和value在浏览器上
显示出来。
*Form的描述METHOD必须是POST,ENCTYPE必须是"multipart/form-data"
例:<form method=post action="/servlet/Demo_upload.class" ENCTYPE="mul
tipart/form-data"> Demo_upload.class(Servlet)
放于相应Web的Servlet目录下,由Form的submit激活。
详细程序流程见RFC文档:Request For Comments: 1867 和 upload.java程序中
的注释。
*/
/*
<html>
<title>UpLoad File</title>
<body>
<form method=post action="/servlet/Demo_upload.class" ENCTYPE="multipa
rt/form-data">
<input type=textbox name="text"><br>
<P>File: <input type=file name="USERFILE01">
<br>
<P>File: <input type=file name="USERFILE02">
<br>
<input type=submit name=submit value=submit>
<input type=reset name=reset value=reset>
</body>
</html>
*/
//*
//-------
//-
//- upload.java
//-
//- Author: AR Williamson
//- Date: May 1998
//- Copyright: Maybe used for non-commercial use.
//-
//------- import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*; public class upload extends HttpServlet {
public void doPost( HttpServletRequest _req, HttpServletResponse _re
s ) throws ServletException, IOException{ parseMultiForm pMF = new parseMultiForm( _req ); String param;
while ( (param = pMF.getNextParameter()) != null ){
if ( param.equalsIgnoreCase("USERFILE") ){
FileOutputStream OutFile = new FileOutputStream( pMF.getFilena
me() );
pMF.getParameter( OutFile );
OutFile.close();
}else{
System.out.println( "Key : " + param );
System.out.println( "Value: " + pMF.getParameter() );
}
}
_res.setStatus( HttpServletResponse.SC_NO_CONTENT );
}
} class parseMultiForm extends java.lang.Object {
private ServletInputStream In;
private byte buffer[] = new byte[4096];
private String delimitor = null;
private String filename=null; public parseMultiForm( ServletRequest _req ) throws IOException{
In = _req.getInputStream();
delimitor = _req.getContentType();
if ( delimitor.indexOf("boundary=") != -1 ){
delimitor = delimitor.substring( delimitor.indexOf("boundary=")+
9, delimitor.length() );
delimitor = "--" + delimitor;
}
} private String readLine(){
try{
int noData = In.readLine( buffer, 0, buffer.length );
if ( noData != -1 )
return new String( buffer, 0, noData, "ISO-8859-1");
}catch(Exception E){}
return null;
} void test() throws IOException{
String LineIn;
while( (LineIn=readLine()) != null )
System.out.println( LineIn );
} public String getFilename(){
return filename;
} public String getParameter(){
return readLine();
} public String getNextParameter() {
try{
String LineIn=null, paramName=null; while ( (LineIn=readLine()) != null ){
if ( LineIn.indexOf( "name=" ) != -1 ){
int c1 = LineIn.indexOf( "name=" );
int c2 = LineIn.indexOf( "\"", c1+6 );
paramName = LineIn.substring( c1+6, c2 );
if ( LineIn.indexOf( "filename=") != -1 ){
c1 = LineIn.indexOf( "filename=" );
c2 = LineIn.indexOf( "\"", c1+10 );
filename = LineIn.substring( c1+10, c2 );
if ( filename.lastIndexOf( "\\" ) != -1 )
filename = filename.substring( filename.lastIndexOf( "\" )+1 ); if ( filename.length() == 0 )
filename = null;
} //- Move the pointer to the start of the data
LineIn = readLine();
if ( LineIn.indexOf( "Content-Type" ) != -1 )
LineIn = readLine(); return paramName;
}
}
}
catch( Exception E ){}
return null;
} public boolean getParameter( OutputStream _Out ){
try{
int noData;
while ( (noData=In.readLine(buffer,0,buffer.length)) != -1 ){ if ( buffer[0] == '-' ){
if ( new String( buffer, 0, noData, "ISO-8859-1").indexOf(
delimitor) == 0 )
break;
}else
_Out.write( buffer, 0, noData );
}
_Out.flush();
return true;
}
catch( Exception E ){
System.out.println( E );
} return false;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货