/**PageBeginHtml Block Begin **/ /***自定义返回顶部小火箭***/ /*生成博客目录的JS 开始*/ /*生成博客目录的JS 结束*/

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

* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。
* @author Alan
* @Email no008@foxmail.com

 

正文

文件上传---普通文件和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:牛奶、不加糖

posted @   一品堂.技术学习笔记  阅读(515)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示

目录导航