欢迎您来到中国万网客户服务中心!
客服导航
如果您在使用我们的产品中遇到问题,建议您首先在“常见问题”中查询解决方法;
如果没有找到该问题的解决方法,您可以在“问题搜索”中进行搜索;
如果搜索后没有找到满意答案,您可以“在线提问”,我们会在1个工作日内给您答复。
您现在的位置: 中国万网客服中心 >> 常见问题 >> 软件常见问题 >> JSP程序模板 >> 文章正文

用JSP编写文件上传

作者:zhujl    文章来源:本站原创    点击数:    更新时间:2006-9-21

 

用JSP编写文件上传

 如过你用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<img.length;i++)
{ os.write(img[i]); }
os.close();
}
catch(IOException e)
{return(false);}

return(true);

}

返回上一页
中国万网旗下网站:中国万网 买麦网
Copyright © 2007 中国万网 版权所有
推荐使用1024×768分辨率浏览,并关闭窗口弹出阻止程序