jspsmartupload组件修改经验谈

通过对jspsmartupload组件的源码反编译与修改,解决了JSP程序中因文件上传导致的Java内存溢出问题。
最近在JSP程序开发过程中,发现了很严重的Java内存溢出问题。我采用了Resin服务器自动重启的方法,临时保证了网站的正常运行。但是我想去寻找问题的症结之所在,我想一定是JSP程序或Java程序没有及时地释放内存,但到底是哪些类的哪些对象没有被释放? 
    我认真的看了一下Resin的错误日志(error.log文件),发现程序出现java.lang.OutOfMemoryError的错误大多情况是 在运行一个上传附件的文件时发生的。在这个上传附件的JSP文件中,我调用了目前网上很流行的jspsmartupload组件,难道是这个组件本身有问 题? 
   于是我就下载了一个Java反编译工具,把jspsmartupload组件的源码文件(class文件)反编译成Java文件。这 个组件用到了五个自定义的类,分别是File类、Files类、Request类、SmartUpload类和SmartUploadException 类。里面最核心的方法莫过于SmartUpload类的upload()方法。我看到upload方法中有这样两个语句: 
      m_totalBytes = m_request.getContentLength();     (*) 
         m_binArray = new byte[m_totalBytes];                    (**) 
     其中,变量m_totalBytes表示用户上传的文件(二进制文件)的内容的总长度,我做了一个测试,一个55.3M 
的文件,它的m_totalBytes是58012662 
, 显然这是一个很大的数。如果用这样大的数去声明一个byte数组,并给数组的每个元素分配内存空间,Java内存不溢出才怪呢!另外,我发现 SmartUpload类中的m_binArray数组竟然从来没有被释放,难道真的不需要释放吗?JVM的功能有我们想象的那么强大吗? 
    我对SmartUpload类作了如下的改进: 
    (1)对upload()方法的(**)语句加上了totalBytes大小的限制,并加上了try...catch...finally去处理异常,如下: 
        try{ 
         if(m_totalBytes>55184652 || m_totalBytes<0)    //55184652相当于52.6M 
         { 
          throw new SmartUploadException("the file is too large to upload."); 
         } 
         else 
         { 
                           m_binArray = new byte[m_totalBytes]; 
                           ...... 
                } 
             catch(Exception e){......} 
             catch(Error err){......} 
             finally{  m_binArray=null;} 
       这样,就能保证不管用户传的文件是多大,不会出现java.lang.OutOfMemoryError的错误,而且如果upload失败,m_binArray所占的内存空间也会被释放。 
     (2)在SmartUpload类中增加了一个stop()方法,该方法完成的工作就是释放m_binArray数组所占的内存空间。代码如下: 
     try{ 
      m_binArray=null;  
     } 
     catch(Exception e) 
     { 
      System.out.println(e.toString());  
     } 
     jspsmartupload组件就做了这些修改,下面就需要更改调用该组件的JSP文件了。其实这个JSP文件有一些地方写得不规范。(以下代码中mySmartUpload表示SmartUpload的一个实例化对象) 
     (1)首先要设置上传附件的MaxSize。 
                    mySmartUpload.setTotalMaxFileSize(10240000); //10240000表示10M 
     (2)调用SmartUpload.upload()方法要捕获一个异常。 
              try{ 
            mySmartUpload.upload(); 
                    } 
                    catch(Exception e) 
                   { 
               out.print("文件太大,无法上传"); 
               return; 
                   } 
     (3)程序结束时要调用SmartUpload.stop()方法。 
                  mySmartUpload.stop(); 
                            mySmartUpload=null; 
     好了,修改完了,把修改后的文件用Resin进行测试(操作系统仍然用Linux9.0),程序至今还没有出现java.lang.OutOfMemoryError的错误。 
    但愿这种方法能彻底帮我解决问题。当然,还需做进一步测试,看看修改后的组件会不会出现新问题。 


http://www.jspcn.net/htmlnews/1116903759359128.html
解决jspsmartupload上传文件中文乱码 采用jspsmartupload上传文件时,如果文件名含有中文,在服务器端取得文件名是会出现乱码。如果表单项中填写了中文,一样会有乱码问题。看了下jspsmartupload的源码,改了两个地方,现在可以没有乱码问题了。 第一个地方,修改SmartUpload中的upload()方法 // //这次用jspSmartUpload做文件上传下载, //该组件默认是GBK编码,当上传的文件名为中文时, //我将文件名getBytes()下,将GBK改成UTF-8。测试了下,貌似没问题, //突然有一次上传一文件时,发现最后几个字乱码,一直是??。在拿些文件测试, // 后来知道了是当文件名为中文奇数时,会乱码,而且还上传不了。 // 再做测试,找原因,查看字符的长度,转成16进制看结果。觉得是jspSmartUpload组件对中文支持不足的问题。 http://ru-yi86.javaeye.com/blog/368553 Java代码 1. public void upload() 2. throws SmartUploadException, IOException, ServletException 3. { 4. int totalRead = 0; 5. int readBytes = 0; 6. long totalFileSize = 0L; 7. boolean found = false; 8. String dataHeader = new String(); 9. String fieldName = new String(); 10. String fileName = new String(); 11. String fileExt = new String(); 12. String filePathName = new String(); 13. String contentType = new String(); 14. String contentDisp = new String(); 15. String typeMIME = new String(); 16. String subTypeMIME = new String(); 17. boolean isFile = false; 18. m_totalBytes = m_request.getContentLength(); 19. m_binArray = new byte[m_totalBytes]; 20. for(; totalRead < m_totalBytes; totalRead += readBytes) 21. try 22. { 23. m_request.getInputStream(); 24. readBytes = m_request.getInputStream().read(m_binArray, totalRead, m_totalBytes - totalRead); 25. } 26. catch(Exception e) 27. { 28. throw new SmartUploadException("Unable to upload."); 29. } 30. 31. for(; !found && m_currentIndex < m_totalBytes; m_currentIndex++) 32. if(m_binArray[m_currentIndex] == 13) 33. found = true; 34. else 35. m_boundary = m_boundary + (char)m_binArray[m_currentIndex]; 36. 37. if(m_currentIndex == 1) 38. return; 39. m_currentIndex++; 40. do 41. { 42. if(m_currentIndex >= m_totalBytes) 43. break; 44. dataHeader = getDataHeader(); 45. m_currentIndex = m_currentIndex + 2; 46. isFile = dataHeader.indexOf("filename") > 0; 47. fieldName = getDataFieldValue(dataHeader, "name"); 48. if(isFile) 49. { 50. filePathName = getDataFieldValue(dataHeader, "filename"); 51. fileName = getFileName(filePathName); 52. fileExt = getFileExt(fileName); 53. contentType = getContentType(dataHeader); 54. contentDisp = getContentDisp(dataHeader); 55. typeMIME = getTypeMIME(contentType); 56. subTypeMIME = getSubTypeMIME(contentType); 57. } 58. getDataSection(); 59. if(isFile && fileName.length() > 0) 60. { 61. if(m_deniedFilesList.contains(fileExt)) 62. throw new SecurityException("The extension of the file is denied to be uploaded (1015)."); 63. if(!m_allowedFilesList.isEmpty() && !m_allowedFilesList.contains(fileExt)) 64. throw new SecurityException("The extension of the file is not allowed to be uploaded (1010)."); 65. if(m_maxFileSize > (long)0 && (long)((m_endData - m_startData) + 1) > m_maxFileSize) 66. throw new SecurityException(String.valueOf((new StringBuffer("Size exceeded for this file : ")).append(fileName).append(" (1105)."))); 67. totalFileSize += (m_endData - m_startData) + 1; 68. if(m_totalMaxFileSize > (long)0 && totalFileSize > m_totalMaxFileSize) 69. throw new SecurityException("Total File Size exceeded (1110)."); 70. } 71. if(isFile) 72. { 73. com.jspsmart.upload.File newFile = new com.jspsmart.upload.File(); 74. newFile.setParent(this); 75. newFile.setFieldName(fieldName); 76. newFile.setFileName(fileName); 77. newFile.setFileExt(fileExt); 78. newFile.setFilePathName(filePathName); 79. newFile.setIsMissing(filePathName.length() == 0); 80. newFile.setContentType(contentType); 81. newFile.setContentDisp(contentDisp); 82. newFile.setTypeMIME(typeMIME); 83. newFile.setSubTypeMIME(subTypeMIME); 84. if(contentType.indexOf("application/x-macbinary") > 0) 85. m_startData = m_startData + 128; 86. newFile.setSize((m_endData - m_startData) + 1); 87. newFile.setStartData(m_startData); 88. newFile.setEndData(m_endData); 89. m_files.addFile(newFile); 90. } else 91. { 92. /** 93. * 原来的代码 94. * String value = new String(m_binArray, m_startData, (m_endData - m_startData) + 1); 95. */ 96. /** 97. * 解决取得request的参数的中文编码问题 98. */ 99. String value = new String(m_binArray, m_startData, (m_endData - m_startData) + 1, "utf-8"); 100. 101. 102. m_formRequest.putParameter(fieldName, value); 103. } 104. if((char)m_binArray[m_currentIndex + 1] == '-') 105. break; 106. m_currentIndex = m_currentIndex + 2; 107. } while(true); 108. } 第二个地方,修改SmartUpload中的getDataHeader()方法 : Java代码 1. private String getDataHeader() 2. { 3. int start = m_currentIndex; 4. int end = 0; 5. int len = 0; 6. boolean found = false; 7. while(!found) 8. if(m_binArray[m_currentIndex] == 13 && m_binArray[m_currentIndex + 2] == 13) 9. { 10. found = true; 11. end = m_currentIndex - 1; 12. m_currentIndex = m_currentIndex + 2; 13. } else 14. { 15. m_currentIndex++; 16. } 17. 18. 19. //原始代码 20. //String dataHeader = new String(m_binArray, start, (end - start) + 1); 21. /** 22. * 解决文件名的中文乱码问题 23. */ 24. String dataHeader = null; 25. try { 26. dataHeader = new String(m_binArray, start, (end - start) + 1,"utf-8"); 27. } catch (UnsupportedEncodingException e) { 28. e.printStackTrace(); 29. } 30. return dataHeader; 31. }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值