国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JSP > 正文

JSP使用Common FileUpload組件實(shí)現(xiàn)文件上傳及限制上傳類型實(shí)例代碼

2024-09-05 00:23:21
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

1、將commons-fileupload-1.3.3.jar復(fù)制到Web應(yīng)用的lib文件夾下,在WebRoot目錄下創(chuàng)建limit.jsp/10836.html">jsp頁(yè)面,在該頁(yè)面中添加一個(gè)文件域的表單,設(shè)置類型為    multipart/form-data。代碼如下:

<body>  <h2>上傳圖書課件</h2>  <form action="LimitFile" name="one" enctype="multipart/form-data" method="post">    選擇一個(gè)rar文件:    <input type="file" name="fileupload" value="upload" />     <input type="submit" value="上傳"> <input type="reset" value="取消">  </form> </body> 

  上述代碼指定提交后將請(qǐng)求提交給LimitFile處理,LimitFile(Servlet)用來(lái)處理上傳文件及判斷文件類型是否匹配,顯示上傳結(jié)果。

2、創(chuàng)建名為L(zhǎng)imitFile的Servlet,并在doPost()方法中編寫實(shí)現(xiàn)代碼,如下所示:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    request.setCharacterEncoding("utf-8");    response.setCharacterEncoding("utf-8");    response.setContentType("text/html");    PrintWriter out = response.getWriter();    String uploadpath = "";    DiskFileItemFactory factory = new DiskFileItemFactory();    //設(shè)置是否使用臨時(shí)文件保存解析出來(lái)的數(shù)據(jù)的那個(gè)臨界值,該方法傳入?yún)?shù)的單位是字節(jié)。    factory.setSizeThreshold(30 * 1024);    //用于設(shè)置setSizeThreshold()方法中提到的臨時(shí)文件的存放目錄,這里要求使用絕對(duì)路徑。    factory.setRepository(factory.getRepository());    ServletFileUpload upload = new ServletFileUpload(factory);    List list = null;    try{      list = upload.parseRequest(request);      String[] limit = new String[]{".jpg", ".gif", ".png", ".bmp"};      //定義限制的文件類型      SuffixFileFilter filter = new SuffixFileFilter(limit);      //獲取SuffixFileFilter實(shí)例      Iterator iterator = list.iterator();      while(iterator.hasNext()){        FileItem item =(FileItem)iterator.next();        if(!item.isFormField()){          String filePath = item.getName();          if(filePath != null){            File filename= new File(filePath);            File uploadFile = new File(request.getSession().getServletContext().getRealPath("/") + "upload");            uploadpath = uploadFile.getAbsolutePath()+File.pathSeparator + uploadpath;            //因?yàn)槁窂胶竺娑嗔藗€(gè)";"號(hào),所以要去掉            uploadpath = uploadpath.substring(0, uploadpath.length()-1);            File saveFile = new File(uploadpath,filename.getName());            boolean flag = filter.accept(saveFile);            if(flag){              out.print("禁止上傳傳圖片文件");              break;            }else{              try {                item.write(saveFile);                out.print("文件上傳成功");              } catch (Exception e) {                out.print("文件上傳失敗了");                e.printStackTrace();              }            }          }        }      }    }catch(FileUploadException e){      e.printStackTrace();    }  }

上述代碼在字節(jié)串?dāng)?shù)組limit中定義了不允許上傳的文件類型,然后將該數(shù)組傳遞給SuffixFileFilter類的構(gòu)造函數(shù)。在通過(guò)該類的accept()方法驗(yàn)證當(dāng)前上傳的文件是否符合條件。最后將文件保存到項(xiàng)目的upload目錄下。

總結(jié)

以上所述是小編給大家介紹的JSP使用Common FileUpload組件實(shí)現(xiàn)文件上傳及限制上傳類型實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JSP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 资中县| 新晃| 日土县| 虹口区| 炉霍县| 南投县| 连城县| 内江市| 鞍山市| 韩城市| 云和县| 古蔺县| 巴彦淖尔市| 海门市| 云阳县| 志丹县| 伊宁县| 沽源县| 肇州县| 新郑市| 黄冈市| 岳阳市| 金昌市| 岳池县| 兴化市| 甘孜县| 交口县| 彩票| 桐梓县| 鄂伦春自治旗| 盐源县| 宜兰市| 资源县| 万安县| 奉贤区| 长乐市| 江阴市| 行唐县| 天镇县| 聂拉木县| 巫山县|