羅比特

          學習筆記

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            2 隨筆 :: 3 文章 :: 0 評論 :: 0 Trackbacks
            在Web應用中,實現文件上傳,可以用這個組件:Commons FileUpload
            先到Apache官網下載這個組件,并將Commons-Fileupload.jar 和 Commons-io.jar加到用戶庫中,并添加到本應用中。
           
            在Servlet 添加一個Init()方法,用來接收一些初始參數,代碼如下:
           
          public ServletContext sc;
          public String savePath;
          public void init(ServletConfig config)
          {
            config.getInitParameter(
          "savePath");
            sc
          =config.getServletContext();
          }
             在doPost()方法中,添加代碼:
          DiskFileItemFactory factoty=new DiskFileItemFactory();
          ServletFileUpload upload
          =new ServletFileUpload(factory);
          以下需要try
          -catch一下
          List Items
          =upload.parseRequest(request);
          Iterator it
          =items.iterator();
          while(it.hasNext())
          {
            FileItem item
          =(FileItem)it.next();
            
          if(item.isFormField())
              {  
                item.getFiledName();
                item.getString(
          "UTF-8")
              }
            
          else
              {
                 
          if(item.getName()!=null&&item.getName().equals(""))
                   {
                     File temp
          =new File(item.getName());
                     File file
          =new File(sc.getRealPath("/")+savePath,temp.getName());
                     item.write(file);
                   }
                  
              }
          }
          posted on 2008-07-20 04:30 羅比特 閱讀(116) 評論(0)  編輯  收藏 所屬分類: JSP

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


          網站導航:
           
          主站蜘蛛池模板: 盘山县| 耿马| 黑河市| 桑日县| 石阡县| 奇台县| 同仁县| 武邑县| 甘泉县| 鹤庆县| 通榆县| 周口市| 淳安县| 中牟县| 孝昌县| 青川县| 鹤山市| 天长市| 新巴尔虎右旗| 青铜峡市| 孟连| 莱阳市| 万荣县| 长白| 远安县| 克东县| 双柏县| 双城市| 巴中市| 香港 | 县级市| 涡阳县| 锡林郭勒盟| 林甸县| 乐至县| 金沙县| 昌黎县| 石楼县| 全椒县| 兰考县| 修武县|