博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
文件上传---普通文件fileupload.jar和url文件httpUrlConnection
阅读量:6920 次
发布时间:2019-06-27

本文共 4544 字,大约阅读时间需要 15 分钟。

文件上传---普通文件和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
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的学习资料请参考:

 

 

 

 

内容来自:

转载于:https://www.cnblogs.com/ios9/p/7476605.html

你可能感兴趣的文章
统计系统剩余的内存、数据类型转换计算(计算mac地址)、数据类型转换(列表与字典相互转换)...
查看>>
DS5100,DS5300磁盘配置注意事项
查看>>
gzip的工作原理及在Nginx和Apache服务中的应用
查看>>
六步实现Rest风格的API
查看>>
Centos7手动安装OpenStack Mitaka版本--KeyStone安装
查看>>
order by不走索引的思考
查看>>
PHP常用函数
查看>>
摄像头变成文字扫描器
查看>>
**PHP分步表单提交思路(分页表单提交)
查看>>
鱼鹰软件签约活动管理专家蓝色方略
查看>>
Scott Schema创建脚本
查看>>
软件开发所用bug管理系统之mantis之windows版
查看>>
RHEL/CentOS6.6SSHD服务安装、配置、使用
查看>>
报错nginx failed error: during websocket handshake
查看>>
爬姓名大全网站的姓名
查看>>
ORA-01102: cannot mount database in EXCLUSIVE 处理方法
查看>>
linux下sshd_config的StrictModes参数
查看>>
华为底部虚拟导航栏挡住布局
查看>>
项目管理的几个概念(WBS、OBS、RBS、BOM、CWS、CA)总结与区分
查看>>
菜鸟眼中的vim 编译器
查看>>