关于JspSmartUpload上传文件大小限制的问题

来源:互联网 发布:韩顺平java满汉楼资源 编辑:程序博客网 时间:2024/05/20 21:22

这是jspsmartupload本身一个缺陷!用jspsmartupload上传东西,当大小超过了某个值后就无法上传了.也就报出了以下异常:
java.lang.OutOfMemoryError: Java heap space

如果是上传小的东西,用这个jspsmartupload这个组件足够了,但是上传大的文件就不行了.建议用commonupload组件.
究其原因在jspsmartupload源码中有:
m_totalBytes = m_request.getContentLength();
m_binArray = new byte[m_totalBytes];
int j;
for(; i < m_totalBytes; i += j)
           ....



而m_request就是HttpServletRequest,它一次将文件流全部读入内存中,也就造成m_totalBytes超级的大,而在new byte[m_totalBytes];时就在内存在分配了一个超大的空间,内存受不了也就直接报异常了.所以除非改掉这种方式的上传否则是没法解决这个问题的.

而commonupload就不一般了,它可以设置一次读取文件最大部分是多少,比部文件有200Mb,你设置一次读取文件的大小是4MB,那么也就超过了一次读4MB到内存,然后就此4MB的部分写入硬盘中的临时文件中,然后再读取下面的4MB,接着把内存的东西刷到硬盘中.也就不会一次读入内存的东西太多,而造成内存的泻漏.

以下是使用commonupload上传的部分代码
String fileName = " ";
  String appPath = request.getSession().getServletContext().getRealPath("/") ;
  DiskFileItemFactory factory = new DiskFileItemFactory();
  factory.setSizeThreshold(cacheSize);  //缓冲大小
  File temFile = new File(appPath+tempFileFold);  //超过缓冲小放在临时文件夹,再一步一步上传
  if(!temFile.exists()){
   temFile.mkdirs();
  }
  factory.setRepository(temFile);
  ServletFileUpload upload = new ServletFileUpload(factory);
  upload.setSizeMax(maxFileSize);  //最大大小
  
  List fileList = null ;
  
  try {
   fileList = upload.parseRequest(request);
   
  } catch (FileUploadException e) {
   if (e instanceof SizeLimitExceededException) {
      System.out.println("超过大小了,返回!");
      double maxSize =  maxFileSize/(1024.0*1024.0);
             if(maxSize>1.0){
                 float fileSize = Math.round(maxSize*1000)/1000;
                 request.setAttribute("message", MessageResource.readByString("file_size_overflow")+fileSize+"M");
             }else{
              double kMaxSize = maxFileSize/(1024.0);
              float fileSize = Math.round(kMaxSize*100)/100;
              request.setAttribute("message", MessageResource.readByString("file_size_overflow")+fileSize+"K");
             }
             request.setAttribute("page", request.getParameter("failpage"));
             System.out.println("page:"+request.getAttribute("page")+"   messgae:"+request.getAttribute("message"));
       return "";
      }
   e.printStackTrace();
  }
  
  if (fileList == null || fileList.size() == 0) {
   System.out.println("空文件,返回!");
      return "";
  }
  
  // 得到所有上传的文件
    Iterator fileItr = fileList.iterator();
    // 循环处理所有文件
    while (fileItr.hasNext()) {
      FileItem fileItem = null;
      String path = null;
      long size = 0;
      // 得到当前文件
      fileItem = (FileItem) fileItr.next();
      // 忽略简单form字段而不是上传域的文件域(<input type="text" />等)
      if (fileItem == null || fileItem.isFormField()) {
       continue;
      }
      // 得到文件的完整路径
      path = fileItem.getName();
      // 得到文件的大小
      size = fileItem.getSize();
      if ("".equals(path) || size == 0) {
      
       System.out.println("空文件2,返回!");
       return "" ;
      }
   
   // 得到去除路径的文件名
      String t_name = path.substring(path.lastIndexOf("//") + 1);
      // 得到文件的扩展名(无扩展名时将得到全名)
      String t_ext = t_name.substring(t_name.lastIndexOf(".") + 1);
      String[] allowFiles = allowedFilesList.split(",");
      boolean isPermited = false ;
      for(String allowFile:allowFiles){
       if(t_ext.equals(allowFile)){
        isPermited = true ;
        break ;
       }
      
      }
      if(!isPermited){
       request.setAttribute("message", t_ext+MessageResource.readByString("file_format_error")+allowedFilesList);
       request.setAttribute("page", request.getParameter("failpage"));
       System.out.println(t_ext+"文件格式不合法,合法文件格式为:"+allowedFilesList);
       return "" ;
      }
     
      long now = System.currentTimeMillis();
      // 根据系统时间生成上传后保存的文件名
      String newFileName = String.valueOf(now)+"."+t_ext;
      // 保存的最终文件完整路径,保存在web根目录下的ImagesUploaded目录下
      File desctemFile = new File(appPath + fileLocationFold);  //超过缓冲小放在临时文件夹,再一步一步上传
    if(!desctemFile.exists()){
     desctemFile.mkdirs();
    }
      String u_name = appPath + fileLocationFold
        + newFileName ;
      fileName = fileLocationFold+newFileName ;
     
     
     
     
      try {
      
           fileItem.write(new File(u_name));
     
    } catch (Exception e) {
   
     e.printStackTrace();
    }

   
   }
  
  
  return fileName ;