高分索取upload和download的源代码

olvest4000 2001-07-30 03:51:26
最近开发网站时出现了,不能实现upload和download功能,有谁帮帮忙啊,我一定重谢啊,谢谢先!
qq:4579751
email:olvest198@elong.com
...全文
71 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
olvest4000 2001-07-31
  • 打赏
  • 举报
回复
你的是不是有servlet写的代码啊???
olvest4000 2001-07-31
  • 打赏
  • 举报
回复
我的是所有的文件都要能实现upload和download!
softech 2001-07-30
  • 打赏
  • 举报
回复
拷了一个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;
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧