解决jspsmartupload上传文件中文乱码

      采用jspsmartupload上传文件时,如果文件名含有中文,在服务器端取得文件名是会出现乱码。如果表单项中填写了中文,一样会有乱码问题。看了下jspsmartupload的源码,改了两个地方,现在可以没有乱码问题了。
第一个地方,修改类SmartUpload中的upload()方法

public void upload()   
      throws SmartUploadException, IOException, ServletException   
  {   
      int totalRead = 0;   
      int readBytes = 0;   
      long totalFileSize = 0L;   
      boolean found = false;   
      String dataHeader = new String();   
      String fieldName = new String();   
      String fileName = new String();   
      String fileExt = new String();   
      String filePathName = new String();   
      String contentType = new String();   
      String contentDisp = new String();   
      String typeMIME = new String();   
      String subTypeMIME = new String();   
      boolean isFile = false;   
      m_totalBytes = m_request.getContentLength();   
      m_binArray = new byte[m_totalBytes];   
      for(; totalRead < m_totalBytes; totalRead += readBytes)   
          try  
          {   
              m_request.getInputStream();   
              readBytes = m_request.getInputStream().read(m_binArray, totalRead, m_totalBytes - totalRead);   
          }   
          catch(Exception e)   
          {   
              throw new SmartUploadException("Unable to upload.");   
          }   
  
      for(; !found && m_currentIndex < m_totalBytes; m_currentIndex++)   
          if(m_binArray[m_currentIndex] == 13)   
              found = true;   
          else  
              m_boundary = m_boundary + (char)m_binArray[m_currentIndex];   
  
      if(m_currentIndex == 1)   
          return;   
      m_currentIndex++;   
      do  
      {   
          if(m_currentIndex >= m_totalBytes)   
              break;   
          dataHeader = getDataHeader();   
          m_currentIndex = m_currentIndex + 2;   
          isFile = dataHeader.indexOf("filename") > 0;   
          fieldName = getDataFieldValue(dataHeader, "name");   
          if(isFile)   
          {   
              filePathName = getDataFieldValue(dataHeader, "filename");   
              fileName = getFileName(filePathName);   
              fileExt = getFileExt(fileName);   
              contentType = getContentType(dataHeader);   
              contentDisp = getContentDisp(dataHeader);   
              typeMIME = getTypeMIME(contentType);   
              subTypeMIME = getSubTypeMIME(contentType);   
          }   
          getDataSection();   
          if(isFile && fileName.length() > 0)   
          {   
              if(m_deniedFilesList.contains(fileExt))   
                  throw new SecurityException("The extension of the file is denied to be uploaded (1015).");   
              if(!m_allowedFilesList.isEmpty() && !m_allowedFilesList.contains(fileExt))   
                  throw new SecurityException("The extension of the file is not allowed to be uploaded (1010).");   
              if(m_maxFileSize > (long)0 && (long)((m_endData - m_startData) + 1) > m_maxFileSize)   
                  throw new SecurityException(String.valueOf((new StringBuffer("Size exceeded for this file : ")).append(fileName).append(" (1105).")));   
              totalFileSize += (m_endData - m_startData) + 1;   
              if(m_totalMaxFileSize > (long)0 && totalFileSize > m_totalMaxFileSize)   
                  throw new SecurityException("Total File Size exceeded (1110).");   
          }   
          if(isFile)   
          {   
              com.jspsmart.upload.File newFile = new com.jspsmart.upload.File();   
              newFile.setParent(this);   
              newFile.setFieldName(fieldName);   
              newFile.setFileName(fileName);   
              newFile.setFileExt(fileExt);   
              newFile.setFilePathName(filePathName);   
              newFile.setIsMissing(filePathName.length() == 0);   
              newFile.setContentType(contentType);   
              newFile.setContentDisp(contentDisp);   
              newFile.setTypeMIME(typeMIME);   
              newFile.setSubTypeMIME(subTypeMIME);   
              if(contentType.indexOf("application/x-macbinary") > 0)   
                  m_startData = m_startData + 128;   
              newFile.setSize((m_endData - m_startData) + 1);   
              newFile.setStartData(m_startData);   
              newFile.setEndData(m_endData);   
              m_files.addFile(newFile);   
          } else  
          {       
              /**  
               * 原来的代码  
               * String value = new String(m_binArray, m_startData, (m_endData - m_startData) + 1);  
               */  
            /**  
             * 解决取得request的参数的中文编码问题  
             */  
              String value = new String(m_binArray, m_startData, (m_endData - m_startData) + 1, "utf-8");   
                 
                 
            m_formRequest.putParameter(fieldName, value);   
          }   
          if((char)m_binArray[m_currentIndex + 1] == '-')   
              break;   
          m_currentIndex = m_currentIndex + 2;   
      } while(true);   
  }   

 

第二个地方,修改类SmartUpload中的getDataHeader()方法 :

private String getDataHeader()   
   {   
       int start = m_currentIndex;   
       int end = 0;   
       int len = 0;   
       boolean found = false;   
       while(!found)    
           if(m_binArray[m_currentIndex] == 13 && m_binArray[m_currentIndex + 2] == 13)   
           {   
               found = true;   
               end = m_currentIndex - 1;   
               m_currentIndex = m_currentIndex + 2;   
           } else  
           {   
               m_currentIndex++;   
           }   
         
           
       //原始代码   
       //String dataHeader = new String(m_binArray, start, (end - start) + 1);   
       /**  
        * 解决文件名的中文乱码问题  
        */  
       String dataHeader = null;   
    try {   
        dataHeader = new String(m_binArray, start, (end - start) + 1,"utf-8");   
    } catch (UnsupportedEncodingException e) {   
        e.printStackTrace();   
    }   
       return dataHeader;           
   }  

 

如果还不行的话,试着将编码改为项目页面中指定的编码格式

真正解决jspSmartUpload组件上传下载文件时中文乱码问题。以前在网上也找过!!!下载了些,都没能解决中文乱码问题!自己改了源代码,并做成jar包,直接用就可以。 另,我把File()类 改成了 SmartFile()类。详情请下载。 另我的Blog有详细描述 http://blog.csdn.net/cartonwang/archive/2008/10/28/3168114.aspx 用朋友问到:译后的结果啊,怎么用啊 ------------------------------------------- 我看了下,给的是jar包。直接用就好了。用法和原版的差不多。稍微有点不同。原版:com.jspsmart.upload.File 我的:com.jspsmart.upload.SmartFile 原版的是File类,我的是SmartFile类。另,http://download.csdn.net/source/796632 中有上传文件时不刷新页面的方法。我浦发银行的项目就是用这些方法。很好用。 ------------------------------------------------ gylsm发表的评论 真是晕死,用原版的上传还是支持中文的,你的连上传都成了乱码了,又没讲下你的用法和原版的区别,都不知道可不可以用 --------------------------------------------------------------------------- 我上面已经讲了吧,其实和原版的没有什么区别的,唯一一个地方要注意的是File()类我改成SmartFile()了,你用该类时,改成SmartFile()类就好了。 如果还不能用,很可能是你没有把原来的Jar包给删除。 时间:2009-04-09 18:45:48 来自:61.142.100.* ming100star发表的评论 谢谢伟大的楼主!! 时间:2009-04-06 15:48:04 来自:220.249.99.* zwei27发表的评论 朋友太谢谢你了,我到网上怎么都找不到,你帮我搞定,SKS
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值