阿郎的技術搏客

          成長軌跡

          文件上傳程序

          import java.io.*;
          import java.util.*;
          import javax.servlet.*;
          import javax.servlet.http.*;
          import javax.servlet.jsp.PageContext;

          public class UploadFile
          {
          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 UploadFile()
          { }
          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);

          }
          }

          posted on 2007-05-11 15:34 alone 閱讀(246) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 读书| 竹山县| 义马市| 获嘉县| 上蔡县| 岱山县| 碌曲县| 富民县| 荣成市| 大兴区| 兴山县| 二手房| 青冈县| 筠连县| 积石山| 广西| 浮梁县| 新安县| 漳浦县| 广东省| 和平县| 开远市| 武鸣县| 彭泽县| 镶黄旗| 城市| 清镇市| 唐河县| 亳州市| 承德县| 叶城县| 长寿区| 双城市| 台北市| 兴业县| 板桥市| 通山县| 合阳县| 东丰县| 冷水江市| 黑龙江省|