登录 注册
当前位置:主页 > 资源下载 > 50 > commons-fileupload-1.2.jar和commons-io-1.3.2.jar下载

commons-fileupload-1.2.jar和commons-io-1.3.2.jar下载

  • 更新:2024-06-06 14:09:08
  • 大小:131KB
  • 推荐:★★★★★
  • 来源:网友上传分享
  • 类别:Java - 后端
  • 格式:RAR

资源介绍

第1个上传组件commons-fileupload =============commons-fileupload ================ common-fileupload组件是apache的一个开源项目之一,可以从http://jakarta.apache.org/commons/fileupload/下载。该组件简单易用,可实现一次上传一个或多个文件,并可限制文件大小。 -下载后解压zip包,将commons-fileupload-1.1.1.jar,和commons-io-1.2.jar(这里我们用的是更新的版本,但是用法是一样的)复制到tomcat的webapps\你的webapp\WEB-INF\lib\下,如果目录不存在请自建目录。 新建一个servlet: FileUpload.java用于文件上传: package com.drp.util.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.*; import java.util.*; import java.util.regex.*; import java.io.*; import org.apache.commons.fileupload.servlet.*; import org.apache.commons.fileupload.disk.DiskFileItemFactory; public class FileUpload extends HttpServlet { private String uploadPath = ""; // 用于存放上传文件的目录 private File tempPath = new File("D:\\Tomcat 5.5\\webapps\\drp1.2\\tempimages\\"); // 用于存放临时文件的目录 public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html; charset=GB18030"); PrintWriter out = res.getWriter(); System.out.println(req.getContentLength()); System.out.println(req.getContentType()); DiskFileItemFactory factory = new DiskFileItemFactory(); // maximum size that will be stored in memory //允许设置内存中存储数据的门限,单位:字节 factory.setSizeThreshold(4096); // the location for saving data that is larger than getSizeThreshold() //如果文件大小大于SizeThreshold,则保存到临时目录 factory.setRepository(new File("D:\\Tomcat 5.5\\webapps\\drp1.2\\tempimages")); ServletFileUpload upload = new ServletFileUpload(factory); // maximum size before a FileUploadException will be thrown //最大上传文件,单位:字节 upload.setSizeMax(1000000); try { List fileItems = upload.parseRequest(req); // assume we know there are two files. The first file is a small // text file, the second is unknown and is written to a file on // the server Iterator iter = fileItems.iterator(); // 正则匹配,过滤路径取文件名 String regExp = ".+\\\\(.+)$"; // 过滤掉的文件类型 String[] errorType = { ".exe", ".com", ".cgi", ".asp" }; Pattern p = Pattern.compile(regExp); String itemNo = "";//文件存放路径 while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); // 忽略其他不是文件域的所有表单信息 if (!item.isFormField()) { String name = item.getName(); long size = item.getSize(); if ((name == null || name.equals("")) && size == 0) continue; Matcher m = p.matcher(name); boolean result = m.find(); if (result) { for (int temp = 0; temp < errorType.length; temp++) { if (m.group(1).endsWith(errorType[temp])) { throw new IOException(name + ": wrong type"); } } try { // 保存上传的文件到指定的目录 // 在下文中上传文件至数据库时,将对这里改写 item.write(new File("d:\\" + m.group(1))); out.print(name+"  "+size+"
"); } catch (Exception e) { out.println(e); } } else { throw new IOException("fail to upload"); } } } } catch (IOException e) { out.println(e); } catch (FileUploadException e) { out.println(e); } } public void init() throws ServletException { this.uploadPath = this.getServletConfig().getInitParameter("upload_path");//的到web.xml中的配置文件用于保存上传文件,也可以在已开始定义的时候初始化,不过这样可以通过改动配置文件来改动存放路径,不用该代码,增加了灵活性。 } } web.xml中相应的配置如下: FileUpload com.drp.util.servlet.FileUpload//注意路径 upload_path D:\\Tomcat 5.5\\webapps\\drp1.2\\images\\item\\//存放地址 FileUpload /servlet/FileUpload 对应的请求文件: index.html //注意action地址,还有enctype要写成multipart/form-data,和method="POST"
文件1:
文件2:
文件3: