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

Posted ✧*꧁一品堂.技术学习笔记꧂*✧.

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件上传---普通文件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:牛奶、不加糖

以上是关于文件上传---普通文件fileupload.jar和url文件httpUrlConnection的主要内容,如果未能解决你的问题,请参考以下文章

文件上传

commons-fileupload.jar实现文件上传

struts2的文件上传

文件上传

上传文件

struts2文件上传下载