羅比特

          學習筆記

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            2 隨筆 :: 3 文章 :: 0 評論 :: 0 Trackbacks
            在Web應用中,實現(xiàn)文件上傳,可以用這個組件:Commons FileUpload
            先到Apache官網(wǎng)下載這個組件,并將Commons-Fileupload.jar 和 Commons-io.jar加到用戶庫中,并添加到本應用中。
           
            在Servlet 添加一個Init()方法,用來接收一些初始參數(shù),代碼如下:
           
          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

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


          網(wǎng)站導航:
          相關(guān)文章:
           
          主站蜘蛛池模板: 澳门| 松溪县| 台东市| 阳东县| 宽城| 临西县| 聂荣县| 江陵县| 阿拉善盟| 噶尔县| 淳安县| 桂林市| 新野县| 成安县| 南丰县| 绥宁县| 航空| 正蓝旗| 绍兴县| 吉安县| 韩城市| 顺义区| 东光县| 广宗县| 勃利县| 蒙城县| 奉化市| 武清区| 凤山市| 沾益县| 高唐县| 桦南县| 保定市| 库车县| 深圳市| 丰台区| 湘潭市| 门源| 原阳县| 台东县| 通城县|