Apache Commons fileUpload实现文件上传(二)

Apache Commons fileUpload实现文件上传(二)
2010年07月30日
  [b]示例[/b][b]3[/b]
  上传一个文件到指定的目录,并限定文件大小。
  demo3.html
  
  
  
  File upload
  
  
  
  File:
  
  
  
  
  
  
  demo3.jsp
  
  
  
  
  
  
  
  上传文件目录
  [b]if[/b] (!uploadPath.exists()) {
  uploadPath.mkdirs();
  }
  // 临时文件目录
  File tempPathFile = [b]new[/b] File("d:\\temp\\buffer\\");
  [b]if[/b] (!tempPathFile.exists()) {
  tempPathFile.mkdirs();
  }
  [b]try[/b] {
  // Create a factory for disk-based file items
  DiskFileItemFactory factory = [b]new[/b] DiskFileItemFactory();
  // Set factory constraints
  factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
  factory.setRepository(tempPathFile);//设置缓冲区目录
  // Create a new file upload handler
  ServletFileUpload upload = [b]new[/b] ServletFileUpload(factory);
  // Set overall request size constraint
  upload.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
  List items = upload.parseRequest(request);//得到所有的文件
  Iterator i = items.iterator();
  [b]while[/b] (i.hasNext()) {
  FileItem fi = (FileItem) i.next();
  String fileName = fi.getName();
  [b]if[/b] (fileName != [b]null[/b]) {
  File fullFile = [b]new[/b] File(fi.getName());
  File savedFile = [b]new[/b] File(uploadPath, fullFile
  .getName());
  fi.write(savedFile);
  }
  }
  out.print("upload succeed");
  } [b]catch[/b] (Exception e) {
  e.printStackTrace();
  }
  %>
  
  
  
  File upload
  
  
  
  
  [b]示例[/b][b]4[/b]
  利用Servlet来实现文件上传。
  Upload.java
  [b]package[/b] com.zj.sample;
  [b]import[/b] java.io.File;
  [b]import[/b] java.io.IOException;
  [b]import[/b] java.util.Iterator;
  [b]import[/b] java.util.List;
  [b]import[/b] javax.servlet.ServletException;
  [b]import[/b] javax.servlet.http.HttpServlet;
  [b]import[/b] javax.servlet.http.HttpServletRequest;
  [b]import[/b] javax.servlet.http.HttpServletResponse;
  [b]import[/b] org.apache.commons.fileupload.FileItem;
  [b]import[/b] org.apache.commons.fileupload.disk.DiskFileItemFactory;
  [b]import[/b] org.apache.commons.fileupload.servlet.ServletFileUpload;
  @SuppressWarnings("serial")
  [b]public[/b] [b]class[/b] Upload [b]extends[/b] HttpServlet {
  [b]private[/b] String uploadPath = "D:\\temp"; // 上传文件的目录
  [b]private[/b] String tempPath = "d:\\temp\\buffer\\"; // 临时文件目录
  File tempPathFile;
  @SuppressWarnings("unchecked")
  [b]public[/b] [b]void[/b] doPost(HttpServletRequest request, HttpServletResponse response)
  [b]throws[/b] IOException, ServletException {
  [b]try[/b] {
  // Create a factory for disk-based file items
  DiskFileItemFactory factory = [b]new[/b] DiskFileItemFactory();
  // Set factory constraints
  factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
  factory.setRepository(tempPathFile);// 设置缓冲区目录
  // Create a new file upload handler
  ServletFileUpload upload = [b]new[/b] ServletFileUpload(factory);
  // Set overall request size constraint
  upload.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
  List items = upload.parseRequest(request);// 得到所有的文件
  Iterator i = items.iterator();
  [b]while[/b] (i.hasNext()) {
  FileItem fi = (FileItem) i.next();
  String fileName = fi.getName();
  [b]if[/b] (fileName != [b]null[/b]) {
  File fullFile = [b]new[/b] File(fi.getName());
  File savedFile = [b]new[/b] File(uploadPath, fullFile.getName());
  fi.write(savedFile);
  }
  }
  System.out.print("upload succeed");
  } [b]catch[/b] (Exception e) {
  // 可以跳转出错页面
  e.printStackTrace();
  }
  }
  [b]public[/b] [b]void[/b] init() [b]throws[/b] ServletException {
  File uploadFile = [b]new[/b] File(uploadPath);
  [b]if[/b] (!uploadFile.exists()) {
  uploadFile.mkdirs();
  }
  File tempPathFile = [b]new[/b] File(tempPath);
  [b]if[/b] (!tempPathFile.exists()) {
  tempPathFile.mkdirs();
  }
  }
  }
  demo4.html
  
  
  
  File upload
  
  
  // action="fileupload"对应web.xml中中的设置.
  
  File:
  
  
  
  
  
  
  web.xml
  
  Upload
  com.zj.sample.Upload
  
  
  Upload
  /fileupload
  
  本文出自 “子 孑” 博客,请务必保留此出处http://zhangjunhd.blog.51cto.com/113473/18331
  [b]附件下载:
  [/b]  
  
  fileUpload工程
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值