jsp

位置:IT落伍者 >> jsp >> 浏览文章

如何用JSP编写文件上传


发布日期:2020年12月29日
 
如何用JSP编写文件上传

如果你曾用VB编写文件上传的组件的话那么用JAVA编写文件上传的JAVABEAN十分容易

下面的例子只是一个简版

package yuanyifileup;

import javaio*;

import javautil*;

import javaxservlet*;

import javaxservlet*;

import javaxservletjspPageContext;

public class yuanyifileup

{

private ServletRequest request;

private ServletResponse response;

private ServletConfig config;

ServletInputStream DATA;

int FormSize;

File f;

FileOutputStream os;

DataInputStream is;

String filename;

byte[] b;

byte t;

boolean flag=false;

public yuanyifileup()

{ }

public void initialize(ServletConfig configHttpServletRequest requestHttpServletResponse response) throws IOException

{

thisrequest=request;

thisresponse=response;

thisconfig=config;

DATA = requestgetInputStream();

FormSize=requestgetContentLength();

}

public void initialize(PageContext pageContext) throws IOException

{

request=pageContextgetRequest();

response=pageContextgetResponse();

config=pageContextgetServletConfig();

DATA = requestgetInputStream();

FormSize=requestgetContentLength();

}

public boolean setFilename(String s)

{

try

{

File f=new File(s);

os=new FileOutputStream(f);

}

catch(IOException e)

{return(false);}

return(true);

}

public void getByte()

{

int i=;

try

{

is=new DataInputStream(DATA);

b=new byte[FormSize];

while (true)

{

try

{

t=isreadByte();

b[i]=t;

i++;

}

catch(EOFException e)

{ break;}

}

isclose();}

catch(IOException e)

{}

}

public boolean save()

{

int i=start=start=;

String temp="";

if (!flag)

{

getByte();

flag=true;

}

try

{

temp=new String(b"ISO_");

}

catch(UnsupportedEncodingException e)

{return(false);}

start=tempindexOf("image/");

temp=tempsubstring(start);

start=tempindexOf("rnrn");

temp=tempsubstring(start+);

start=tempindexOf(";rn");

if (start!=)

{

temp=tempsubstring(start);

}

try

{

byte[] img=tempgetBytes("ISO_");

for (i=;i<imglength;i++)

{ oswrite(img[i]); }

osclose();

}

catch(IOException e)

{return(false);}

return(true);

}

如果有不明白的发EMail:yymailbox@netBye

}

               

上一篇:Tomcat JSP经典配置实例

下一篇:JSP技巧教你如何成为一个成功的Jsp程序员