隨筆 - 0, 文章 - 75, 評論 - 0, 引用 - 0
          數(shù)據(jù)加載中……

          SmartUpload組件控制文件上傳

          <form action="upload/doUpload.jsp" method="post" enctype="multipart/form-data">
             <input type="file" name="myfile"/><br />
             <input type="file" name="myfile2" /><br />
             描述:<input type="text" name="desc" /><br />
             <input type="submit"/>
             </form>

          form表單屬性必須定義method="post" enctype="multipart/form-data"

           

          JSP:

          <%@page import="com.jspsmart.upload.SmartUpload"%>
          <%@page import="com.jspsmart.upload.File"%>

           <%
               SmartUpload su = new SmartUpload();
               //初始化
               su.initialize(pageContext); //內(nèi)置對象pageContext作為參數(shù)
               String allowed="jpg,bmp,txt";
               su.setAllowedFilesList(allowed); //設(shè)置允許上傳的擴(kuò)展名
               String unallowed="bat";
               su.setDeniedFilesList(unallowed); //不允許上傳

               su.setMaxFileSize(1024*1024*2);  //最大上傳2M
               try{
                su.upload(); //上傳到內(nèi)存

                for (int i = 0; i < su.getFiles().getCount(); i++) { //多個(gè)上傳
                 File file = su.getFiles().getFile(i); //取得單個(gè)文件上傳信息
                 if(file.isMissing()) //判斷是否為空上傳項(xiàng)
                  continue;

                String desc = su.getRequest().getParameter("desc"); //在upload()方法之后才可使用,獲取表單信息
                out.print("上傳描述:"+desc);
                File file = su.getFiles().getFile(i); //取得單個(gè)文件上傳信息
                String filePath = "file/";
                filePath += file.getFileName(); //設(shè)置文件在服務(wù)器保存位置
                file.saveAs(filePath,SmartUpload.SAVE_VIRTUAL); //文件另存到tomcat部署的項(xiàng)目文件夾中,不是當(dāng)前項(xiàng)目物理位置
                //如果保存絕對路徑,
                //file.saveAs(filePath,SmartUpload.SAVE_PHYSICAL);
                out.print(filePath);

                }
               }catch(Exception e){
                out.write(e.toString());
                e.printStackTrace();
               }
               %>


          Servlet:實(shí)現(xiàn)上傳

          <form action="servlet/DoUpload" method="post" enctype="multipart/form-data">

          --------------------------

          public void doGet(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            SmartUpload su = new SmartUpload();
            su.initialize(this.getServletConfig(), request, response); //初始化
            String allowed = "jpg,bmp,ico,png";
            su.setAllowedFilesList(allowed);
            String unallowed = "bat,jsp,aspx,asp,txt";
            try {
             su.setDeniedFilesList(unallowed);
            } catch (SQLException e) {
             e.printStackTrace();
            }
            su.setMaxFileSize(1024*1024*2);
            try {
             su.upload();
             for(int i =0; i<su.getFiles().getCount(); i++){
              File file = su.getFiles().getFile(i);
              if(file.isMissing())
               continue;
              String desc = su.getRequest().getParameter("desc");
              out.print(desc);
              String fileName = "d:/file/"; //路徑必須存在,否則異常
              fileName += file.getFileName();
              out.print(fileName);
              file.saveAs(fileName,SmartUpload.SAVE_PHYSICAL);
             }
            } catch (SmartUploadException e) {
             out.print(e.toString());
             e.printStackTrace();
            }
            out.flush();
            out.close();
           }

           

          posted on 2012-04-22 15:46 hantai 閱讀(134) 評論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 宁明县| 张家川| 茂名市| 临泉县| 玛曲县| 万州区| 黄梅县| 吐鲁番市| 安多县| 于都县| 多伦县| 武定县| 革吉县| 稷山县| 巴彦淖尔市| 根河市| 利川市| 定远县| 兴城市| 栾城县| 岱山县| 中西区| 平罗县| 蒲江县| 天柱县| 苍山县| 黄浦区| 湘阴县| 常山县| 祥云县| 榆树市| 通海县| 永康市| 清涧县| 高邮市| 涪陵区| 礼泉县| 新闻| 繁峙县| 大安市| 永嘉县|