文件上传---普通文件fileupload.jar和url文件httpUrlConnection

文件上传---普通文件和url文件

主要用来学习使用common-fileupload.jar和java.net.httpURLConnection

普通文件:

  1 //上传xls文件到临时目录
  2         if (! ServletFileUpload.isMultipartContent(request)) return;
  3         DiskFileItemFactory factory = new DiskFileItemFactory();  // 建立factory
  4         factory.setSizeThreshold(4*1024*1024);  // 设定缓存大小
  5         ServletFileUpload upload = new ServletFileUpload(factory);
  6         upload.setFileSizeMax(5120000);//5M
  7 
  8         File srcFile = null;
  9         try {
 10             List<FileItem> items = upload.parseRequest(request);
 11             for (FileItem item : items) {
 12                 if (!item.isFormField()) {
 13                     String ext = StringUtils.substringAfterLast(item.getName(), ".").toLowerCase();
 14                     //过滤文件格式
 15                     if(StringUtils.isEmpty(ext) || !"xls".equalsIgnoreCase(ext)) {
 16                         System.out.println("Unsupport file type: "+ext);
 17                         throw new Exception("Unsupport file type: "+ext);
 18                     }
 19 
 20                     String fileName = Long.toString(Calendar.getInstance().getTimeInMillis())+"."+ext;
 21                    //临时目录
 22                    String srcFilePath = "/tmp/" + fileName;
 23                    srcFile = new File(srcFilePath);
 24                    if (srcFile.exists()) {
 25                        srcFile.delete();
 26                    }
 27                    item.write(srcFile);
 28                 }
 29             }
 30         }catch (SizeLimitExceededException e){
 31             e.printStackTrace();
 32             return;
 33         }catch (FileUploadException fupEx) {
 34             fupEx.printStackTrace();
 35             return;
 36         }

 

url文件上传:

  1 System.out.println("Upload url file begins ");
  2     try {
  3         String srcURL = request.getParameter("srcURL");
  4         if(srcURL==null || srcURL.trim().length()<=0) {
  5             throw new Exception("No url found ");
  6         }
  7 
  8         String ext = StringUtils.substringAfterLast(srcURL, ".").toLowerCase();
  9         //初级过滤文件格式
 10         if(app.getFileExts()!=null && !app.getFileExts().contains(ext)) {
 11             throw new Exception("Unsupport file type: "+ext);
 12         }
 13 
 14         String fileName = Long.toString(Calendar.getInstance().getTimeInMillis())+"."+ext;
 15         String srcFilePath = "/tmp/" + fileName;
 16 
 17         URL urlfile = null;
 18         HttpURLConnection httpUrl = null;
 19         BufferedInputStream bis = null;
 20         BufferedOutputStream bos = null;
 21         File saveFile = new File(srcFilePath);
 22 
 23 
 24         //file wall
 25         String proxy = "192.168.1.1";
 26         String port = "8080";
 27         Properties systemProperties = System.getProperties();
 28         systemProperties.setProperty("http.proxyHost",proxy);
 29         systemProperties.setProperty("http.proxyPort",port);
 30 
 31         try{
 32             urlfile = new URL(srcURL);
 33             httpUrl = (HttpURLConnection)urlfile.openConnection();
 34 
 35             httpUrl.setConnectTimeout(3000);
 36             httpUrl.setReadTimeout(60000);
 37 
 38             httpUrl.connect();
 39             bis = new BufferedInputStream(httpUrl.getInputStream());
 40         }catch(Exception e)
 41         {
 42             e.printStackTrace();
 43             System.out.println("Souce url connect failed:"+srcURL+" by "+e.getMessage());
 44             return;
 45         }
 46 
 47         try{
 48             int count = 0;
 49             int size = 0;
 50             bos = new BufferedOutputStream(new FileOutputStream(saveFile));
 51             byte[] b = new byte[1024];
 52             while((count=bis.read(b))!=-1) {
 53                 bos.write(b, 0, count);
 54                 size +=count;
 55                 if(size>=app.getFileSize()) {
 56                     System.out.println("File size exceeded max limit ");
 57                     return;
 58                 }
 59             }
 60         }catch(Exception e)
 61         {
 62             e.printStackTrace();
 63             System.out.println("Save url file failed:"+srcURL+" by "+e.getMessage());
 64             return;
 65         }finally{
 66             try{
 67                 bos.flush();
 68                 bis.close();
 69                 httpUrl.disconnect();
 70             }catch(Exception e)
 71             {
 72                 System.out.println(e.toString());
 73             }
 74         }

更多详细关于HttpURLConnection的学习资料请参考:

HttpURLConnection学习

JDK中的URLConnection参数详解

 

 

 

 

内容来自:cnblogs:牛奶、不加糖

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值