| 网站首页 | 模板 | 资料 | 源码 | 工具 | 开发 | 设计 | 安全 | 项目 | 网络 | 图片 | 系统 | 数据库 | 博客 | 会员中心 | 小说 | 
MYFTP 精品资料下载
网络学院
学习资料
源码模版
您现在的位置: 精品下载 >> 开发 >> 网站开发 >> JSP教程 >> 文章正文 用户登录 新用户注册
如何用JSP编写文件上传            【字体:
如何用JSP编写文件上传
作者:佚名    文章来源:不详    点击数:    更新时间:2007-5-9

如过你用VB编写文件上传的组件的话,那么用JAVA编写文件上传的JAVABEAN十分容易。下面的例子只是一个简版
package yuanyifileup;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.PageContext;

public class yuanyifileup
{
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 yuanyifileup()
{ }
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("

");

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

return(true);

}

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

  • 下一篇文章:
  • 最新热点 最新推荐 相关文章
    在CSS样式表里使用JavaScrip
    如何有效管理好CSS
    如何用CSS层叠样式表控制鼠标
    网页框架和网页框架集如何工
    如何让12px以下的文字正常显
    如何做好网站开发项目需求分
    如何用CSS定义表格与模拟表格
    如何用CSS制作横向菜单
    如何在网页中插入CSS
    如何在网页中动态改变样式
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)