Use apache upload framework to manage to upload file

 If your appliction need a feature to upload file , why don't you try a frame , that is called fileupload from apache . I trongly recommended that you try maven also from apache .
configure pom.xml file with dependecy like below :
  1. <dependency>
  2. <groupId>commons-fileupload</groupId>
  3. <artifactId>commons-fileupload</artifactId>
  4. <version>1.2.1</version>
  5. </dependency>
we have add maven jar to our project already .
Next , write a servlet class .

  1. package com.captech.server.service.common;
  2. import java.io.File;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import org.apache.commons.fileupload.FileItem;
  9. import org.apache.commons.fileupload.FileUploadException;
  10. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  11. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  12. import com.captech.server.service.license.ProductLicenseServiceImpl;
  13. /**
  14.  * This class is not for user , but for tomcat ( It is a servlet).
  15.  * 
  16.  * @author WeiXing
  17.  *
  18.  */
  19. public class FileUploader extends HttpServlet{
  20.     
  21.     private static final long serialVersionUID = 8375418953160288723L;
  22.     private String uploadPath;
  23.     
  24.     private int maxPostSize = 100 * 1024 * 1024;
  25.     
  26.     private ProductLicenseServiceImpl service ;
  27.     
  28.     public ProductLicenseServiceImpl getService() {
  29.         return service;
  30.     }
  31.     public void setService(ProductLicenseServiceImpl service) {
  32.         this.service = service;
  33.     }
  34.     public void init(){
  35.         
  36.         // this.getServletContext().getRealPath("/") is get servlet container 's real path .
  37.         uploadPath = this.getServletContext().getRealPath("/") + getInitParameter("UPLOADPARAM");
  38.     }
  39.     @SuppressWarnings("unchecked")
  40.     public void doPost(HttpServletRequest req, HttpServletResponse res) {
  41.         res.setContentType("text/html; charset=UTF-8");
  42.         DiskFileItemFactory factory = new DiskFileItemFactory();
  43.         factory.setSizeThreshold(4096);
  44.         ServletFileUpload upload = new ServletFileUpload(factory);
  45.         upload.setSizeMax(maxPostSize);
  46.         try {
  47.             List fileItems = upload.parseRequest(req);
  48.             Iterator iter = fileItems.iterator();
  49.             String name;
  50.             File file = null ;
  51.             while (iter.hasNext()) {
  52.                 FileItem item = (FileItem) iter.next();
  53.                 if (!item.isFormField()) {
  54.                     name = item.getName();
  55.                     try {
  56.                         String fullName = uploadPath + name ;
  57.                         file = new File(fullName);
  58.                         ProductLicenseServiceImpl.setKeyPath(uploadPath);
  59.                         file.createNewFile();
  60.                         item.write(file);
  61.                     } catch (Exception e) {
  62.                         e.printStackTrace();
  63.                     }
  64.                 }
  65.             }
  66.             
  67.         } catch (FileUploadException e) {
  68.             e.printStackTrace();
  69.         }
  70.     }
  71.     
  72.     
  73. }
  74.     


OK, we can use the servlet to upload file from client to server . try it , have fun .
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值