1、将commons-fileupload-1.3.3.jar复制到Web应用的lib文件夹下,在WebRoot目录下创建limit.jsp页面,在该页面中添加一个文件域的表单,设置类型为 multipart/form-data。代码如下:
<body> ???<h2>上传图书课件</h2> ???<form action="LimitFile" name="one" enctype="multipart/form-data" method="post"> ???选择一个rar文件: ???<input type="file" name="fileupload" value="upload" /> ???<input type="submit" value="上传"> <input type="reset" value="取消"> ???</form> ?</body>
上述代码指定提交后将请求提交给LimitFile处理,LimitFile(Servlet)用来处理上传文件及判断文件类型是否匹配,显示上传结果。
2、创建名为LimitFile的Servlet,并在doPost()方法中编写实现代码,如下所示:
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(); ???????//设置是否使用临时文件保存解析出来的数据的那个临界值,该方法传入参数的单位是字节。 ???????factory.setSizeThreshold(30 * 1024); ???????//用于设置setSizeThreshold()方法中提到的临时文件的存放目录,这里要求使用绝对路径。 ???????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实例 ???????????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; ???????????????????????//因为路径后面多了个";"号,所以要去掉 ???????????????????????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(); ???????} ???}
上述代码在字节串数组limit中定义了不允许上传的文件类型,然后将该数组传递给SuffixFileFilter类的构造函数。在通过该类的accept()方法验证当前上传的文件是否符合条件。最后将文件保存到项目的upload目录下。
jsp 使用Common-FileUpload组件文件上传及限制上传类型
原文地址:http://www.cnblogs.com/lichengcai/p/7469049.html