commons-fileupload源码分析

public void doPost(HttpServletRequest req, HttpServletResponse res) {
   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()
   factory.setRepository(new File("/tmp"));

   ServletFileUpload upload = new ServletFileUpload(factory);
   // maximum size before a FileUploadException will be thrown
   upload.setSizeMax(1000000);

   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 i = fileItems.iterator();
   String comment = ((FileItem)i.next()).getString();
   FileItem fi = (FileItem)i.next();
   // filename on the client
   String fileName = fi.getName();
   // save comment and filename to database
   ...
   // write the file
   fi.write(new File("/www/uploads/", fileName));
 }
以上代码为apache官网的一段commons-fileupload的demo。其中主要分为两个部分
1、创建DiskFileItemFactory
2、创建SerletFileUpload
 
(1)DiskFileItemFactory实现了FileItemFactory接口createItem,在里面指定了默认的阀值。同时声明一个FileCleaningTracker去跟踪是否超过阀值。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值