熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

如何用JSP編寫文件上傳

2013-11-15 12:03: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

  }


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20183.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.