| 网站首页 | 建站学院 | 资源下载 | 建站教程 | 图片素材 | 网贝社区 | 
您现在的位置: 网贝建站 >> 建站学院 >> JSP技术 >> J2EE >> 正文 用户登录 新用户注册
专 题 栏 目
最 新 热 门
最 新 推 荐
相 关 文 章
没有相关文章
用JSP编写文件上传         ★★★★
用JSP编写文件上传
作者:佚名 文章来源:21CN 点击数: 更新时间:2004-9-30 15:39:30

/*
*@功能描述:上传文件(JavaBean)
*@author:紫竹园主
*@pubdate:2003-11
*/  
下面的例子只是一个简版  
package upload;  
import java.io.*;  
import java.util.*;  
import javax.servlet.*;  
import javax.servlet.http.*;  
import javax.servlet.jsp.PageContext;  
//确定您的环境中存在如上包
public class upload  
{  
private ServletRequest request;  
private ServletResponse response;  
private ServletConfig config;  
ServletInputStream DATA;  
int FormSize;  
File f1;  
FileOutputStream os;  
DataInputStream is;  
String filename;  
byte[] b;  
byte t;  
boolean flag=false;  
public upload()  
{ }  
//初始化
public void initialize(ServletConfig config,HttpServletRequest request,HttpServletResponse response) throws IOException  
{  
this.request=request;  
this.response=response;  
this.config=config;  
DATA = request.getInputStream();  
FormSize=request.getContentLength();  
}  
public void initialize(PageContext pageContext) throws IOException  
{  
request=pageContext.getRequest();  
response=pageContext.getResponse();  
config=pageContext.getServletConfig();  
DATA = request.getInputStream();  
FormSize=request.getContentLength();  
}  
public boolean setFilename(String s)  
{  
try  
{  
File f1=new File(s);  
os=new FileOutputStream(f1);  
}  
catch(IOException e)  
{return(false);}  
return(true);  
}  
public void getByte()  
{  
int i=0;  
try  
{  
is=new DataInputStream(DATA);  
b=new byte[FormSize];  

while (true)  
{  
try  
{  
t=is.readByte();  
b[i]=t;  
i++;  
}  
catch(EOFException e)  
{ break;}  
}  
is.close();}  
catch(IOException e)  
{}  
}  

public boolean save()  
{  
int i=0,start1=0,start2=0;  
String temp=\"\";  
if (!flag)  
{  
getByte();  
flag=true;  
}  
try  
{  
temp=new String(b,\"ISO8859_1\");  
}  
catch(UnsupportedEncodingException e)  
{return(false);}  

start1=temp.indexOf(\"image/\");  
temp=temp.substring(start1);  

start1=temp.indexOf(\"\\r\\n\\r\\n\");  

temp=temp.substring(start1+4);  
start2=temp.indexOf(\";\\r\\n\");  
if (start2!=-1)  
{  
temp=temp.substring(0,start2);  
}  
try  
{  
byte[] img=temp.getBytes(\"ISO8859_1\");  
for (i=0;i<img.length;i++)  
{ os.write(img[i]); }  
os.close();  
}  
catch(IOException e)  
{return(false);}  

return(true);  

}  
文件流形式上传,如果有不明白的发E-Mail:support@51group.com 

文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)