关于common-fileupload的一些网络资料1

来源:互联网 发布:为什么淘宝点不开客服 编辑:程序博客网 时间:2024/05/29 19:44

首先要了解上传的本质,首先上传需要在jsp页面的form标签中配置enctype="multipart/form-data"

因为这样配置后,在http请求发出时才会以2进制的方式去传输上传文件

 

当请求到达服务端后,在action我们的代码是

 

Java代码
  1. DiskFileItemFactory factory = new DiskFileItemFactory();   
  2. // 当文件大小超过300k时,就在磁盘上建立临时文件   
  3. factory.setSizeThreshold(300000);   
  4. //设计文件上传的临时目录   
  5. factory.setRepository(new File("D:<SPAN style="COLOR: #0000ff">//</SPAN>temp"));   
  6. ServletFileUpload upload = new ServletFileUpload(factory);   
  7. // 文件大小不能超过20M   
  8. upload.setSizeMax(20000000);  

在设定好了一系列参数后,我们就要开始主要的方法了

ServletFileUpload.parseRequest(Request  request)

 

Java代码 复制代码
  1. public List /* FileItem */ parseRequest(HttpServletRequest request)   
  2.   throws FileUploadException {   
  3.       return parseRequest(new ServletRequestContext(request));   
  4.   }  

 

 

Java代码 复制代码
  1.  public List /* FileItem */ parseRequest(RequestContext ctx)   
  2.             throws FileUploadException {   
  3.         try {   
  4.             FileItemIterator iter = getItemIterator(ctx);//这个方法是相当重要的,在下面介绍   
  5.             List items = new ArrayList();   
  6.             FileItemFactory fac = getFileItemFactory();   
  7.             if (fac == null) {   
  8.                 throw new NullPointerException(   
  9.                     "No FileItemFactory has been set.");   
  10.             }   
  11.             while (iter.hasNext()) {   
  12.                 FileItemStream item = iter.next();   
  13.                 FileItem fileItem = fac.createItem(item.getFieldName(),   
  14.                         item.getContentType(), item.isFormField(),   
  15.                         item.getName());   
  16.                 try {   
  17. //将2个流copy,item.openStream()是request中的流,fileItem.getOutputStream()是准备向指定上传文件夹写文件的流   
  18. //fileItem.getOutputStream() 中就设定了临时文件的路径 下面将介绍   
  19.                    Streams.copy(item.openStream(), fileItem.getOutputStream(),   
  20.                             true);   
  21.                 } catch (FileUploadIOException e) {   
  22.                     throw (FileUploadException) e.getCause();   
  23.                 } catch (IOException e) {   
  24.                     throw new IOFileUploadException(   
  25.                             "Processing of " + MULTIPART_FORM_DATA   
  26.                             + " request failed. " + e.getMessage(), e);   
  27.                 }   
  28.                 if (fileItem instanceof FileItemHeadersSupport) {   
  29.                     final FileItemHeaders fih = item.getHeaders();   
  30.                     ((FileItemHeadersSupport) fileItem).setHeaders(fih);   
  31.                 }   
  32.                 items.add(fileItem);   
  33.             }   
  34. //返回FileItem的集合(关于FileItem的实现类,大家可以参考DiskFileItem)   
  35.             return items;   
  36.         } catch (FileUploadIOException e) {   
  37.             throw (FileUploadException) e.getCause();   
  38.         } catch (IOException e) {   
  39.             throw new FileUploadException(e.getMessage(), e);   
  40.         }   
  41.     }  

 

  

Java代码 复制代码
  1. public OutputStream getOutputStream()   
  2.       throws IOException {   
  3.       if (dfos == null) {   
  4.           File outputFile = getTempFile();   
  5.           dfos = new DeferredFileOutputStream(sizeThreshold, outputFile);   
  6.       }   
  7.       return dfos;   
  8.   }   
  9.   
  10.   protected File getTempFile() {   
  11.     //如果没设置临时目录,就用System.getProperty("java.io.tmpdir")   
  12.       if (tempFile == null) {   
  13.           File tempDir = repository;   
  14.           if (tempDir == null) {   
  15.               tempDir = new File(System.getProperty("java.io.tmpdir"));   
  16.           }   
  17.     //设计临时文件名   
  18.           String tempFileName =   
  19.               "upload_" + UID + "_" + getUniqueId() + ".tmp";   
  20.   
  21.           tempFile = new File(tempDir, tempFileName);   
  22.       }   
  23.       return tempFile;   
  24.   }  

 

 

Java代码 复制代码
  1. public FileItemIterator getItemIterator(RequestContext ctx)   
  2.   throws FileUploadException, IOException {   
  3.       return new FileItemIteratorImpl(ctx);   
  4.   }  

 

Java代码 复制代码
  1. FileItemIteratorImpl(RequestContext ctx)   
  2.               throws FileUploadException, IOException {   
  3.           if (ctx == null) {   
  4.               throw new NullPointerException("ctx parameter");   
  5.           }   
  6.   
  7.           String contentType = ctx.getContentType();  
Java代码 复制代码

  1. //这就是为什么需要将请求写成multipart/form-data   
  2. //  public static final String MULTIPART = "multipart/"     
  3.             if ((null == contentType)   
  4.                     || (!contentType.toLowerCase().startsWith(MULTIPART))) {   
  5.                 throw new InvalidContentTypeException(   
  6.   "the request doesn't contain a "  + MULTIPART_FORM_DATA  + " or "   + MULTIPART_MIXED  + " stream, content type header is "+ contentType);   
  7.             }   
  8.   
  9.             InputStream input = ctx.getInputStream();   
  10.   
  11. //此处的sizeMax就是我们在action中set进去的值   
  12.             if (sizeMax >= 0) {   
  13.                 int requestSize = ctx.getContentLength();   
  14.                 if (requestSize == -1) {   
  15.                     input = new LimitedInputStream(input, sizeMax) {   
  16.                         protected void raiseError(long pSizeMax, long pCount)   
  17.                                 throws IOException {   
  18.                             FileUploadException ex =   
  19.                                 new SizeLimitExceededException(   
  20.                                     "the request was rejected because"   + " its size (" + pCount      + ") exceeds the configured maximum" + " (" + pSizeMax + ")",  pCount, pSizeMax);   
  21.                             throw new FileUploadIOException(ex);   
  22.                         }   
  23.                     };   
  24.                 } else {   
  25. //此处主要是判断上传文件的大小是否超过了设定值   
  26.                     if (sizeMax >= 0 && requestSize > sizeMax) {   
  27.                         throw new SizeLimitExceededException(   
  28.                                 "the request was rejected because its size ("  
  29.  + requestSize   + ") exceeds the configured maximum ("+ sizeMax + ")", requestSize, sizeMax);   
  30.                     }   
  31.                 }   
  32.             }  

    当返回了这个FileItem集合后,我们的代码如下

    Java代码 复制代码
    1. List<FileItem> items = getUploadFileIteams(request,pushObj,goUrl);   
    2.   
    3.     for (FileItem item : items) {   
    4.         File file = new File(realPath);   
    5.         item.write(file);   
    6.     }  

    这里就将文件真正写到了服务器上

    再最后把item.write这个方法贴下吧,大家就更明白了

    Java代码 复制代码
    1. public void write(File file) throws Exception {   
    2.         if (isInMemory()) {   
    3.             FileOutputStream fout = null;   
    4.             try {   
    5.                 fout = new FileOutputStream(file);   
    6.                 fout.write(get());   
    7.             } finally {   
    8.                 if (fout != null) {   
    9.                     fout.close();   
    10.                 }   
    11.             }   
    12.         } else {   
    13.             File outputFile = getStoreLocation();   
    14.             if (outputFile != null) {   
    15.                 // Save the length of the file   
    16.                 size = outputFile.length();   
    17.               //看到下面的代码,大家就应该明白了吧   
    18.                 if (!outputFile.renameTo(file)) {   
    19.                     BufferedInputStream in = null;   
    20.                     BufferedOutputStream out = null;   
    21.                     try {   
    22.                         in = new BufferedInputStream(   
    23.                             new FileInputStream(outputFile));   
    24.                         out = new BufferedOutputStream(   
    25.                                 new FileOutputStream(file));   
    26.                         IOUtils.copy(in, out);   
    27.                     } finally {   
    28.                         if (in != null) {   
    29.                             try {   
    30.                                 in.close();   
    31.                             } catch (IOException e) {   
    32.                                 // ignore   
    33.                             }   
    34.                         }   
    35.                         if (out != null) {   
    36.                             try {   
    37.                                 out.close();   
    38.                             } catch (IOException e) {   
    39.                                 // ignore   
    40.                             }   
    41.                         }   
    42.                     }   
    43.                 }   
    44.             } else {   
    45.                 /*  
    46.                  * For whatever reason we cannot write the  
    47.                  * file to disk.  
    48.                  */  
    49.                 throw new FileUploadException(   
    50.                     "Cannot write uploaded file to disk!");   
    51.             }   
    52.         }   
    53.     }  

 

 

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 坐火车忘记带票怎么办 距离二本线差几分怎么办 行李包落火车候车厅怎么办 高铁票买错地点怎么办 高铁票买错日期怎么办 票买错时间了怎么办 上高铁了票丢了怎么办 上车前高铁票丢了怎么办 高铁安检没收的东西怎么办 高铁安检员老了怎么办 高铁安检喷雾拍照了怎么办 十个小时的高铁怎么办 华为开机需要激活码怎么办 高铁提前上车了出站怎么办 买火车票忘记带身份证怎么办 买高铁票没赶上怎么办 电脑放视频没有声音怎么办 内业计算中角度超限怎么办 遇到飞机出故障乘客怎么办 模拟城市5矿产满了怎么办 模拟城市5劳工短缺怎么办 模拟城市5土地价值低怎么办 考上研究生想二战研究生学籍怎么办 小米3屏幕花屏怎么办 狗被城管抓了怎么办 隔壁家的狗扰民怎么办 遛狗碰到碰瓷老太太怎么办 碰到保安碰瓷的人怎么办 碰到碰瓷的车怎么办 商品房楼上邻居违规装修怎么办 遇到不讲道理的邻居怎么办 邻居告我我该怎么办 丈夫判刑十年回来妻子怎么办 有人发色情信息给妻子怎么办 小孩拉蛋花样便怎么办 鸡下的蛋壳是软怎么办 幼升小错过了现场审核怎么办 老人户口迁移到北京医保怎么办 过山洞经常堵耳朵怎么办 门有了一个洞该怎么办 公司要求补税没钱补怎么办