eXtremeComponents导出时的中文文件名问题

eXtremeComponents导出时的中文文件名问题

        eXtremeComponents的中文的问题,目前知道的就是导出使用中文文件名的乱码问题,eXtremeComponents已经默认使用UTF来导出XLS,也已经给出了PDF导出的解决方案: 最新eXtremeComponents包:支持 PDF中文导出
         网友seno指出可以参照SpringSide的解决方案,对文件名进行toUtf8编码。不过,在我自己实际应用中,我的一个应用根本不需要进行任何形式的修正就能正确地生成正确的文件名输出,如果我在eXtremeComponents添加了toUtf8导出的文件名反而乱码。所以现在的问题是我什么时候需要对文件名进行toUtf8编码?这是这段时间一直困扰我的问题,在网友冷月宫主和MagicYang的帮助,昨天经过一整天的查找测试找到了一个暂时看来令我比较满意的解决方案(暂时没有CheckIn): 我对得到的文件名字符串使用jchardet(http://jchardet.sourceforge.net/)进行编码检测,如果检测编码是ASCII码则直接返回原字符串,否则的话是用SpringSide提供的toUtf8方法(比我原来的简洁有效)对字符串进行编码后再返回新的字符串。对应的代码如下:
    
/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    
http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 
*/
package  org.extremecomponents.table.filter;

import  org.apache.commons.lang.StringUtils;
import  org.apache.commons.logging.Log;
import  org.apache.commons.logging.LogFactory;
import  org.extremecomponents.table.context.Context;
import  org.extremecomponents.table.core.TableConstants;
import  org.mozilla.intl.chardet.nsDetector;
import  org.mozilla.intl.chardet.nsICharsetDetectionObserver;
import  org.mozilla.intl.chardet.nsPSMDetector;

/**
 * 
@author  Jeff Johnston
 
*/
public   final   class  ExportFilterUtils {
    
private   static  Log logger  =  LogFactory.getLog(ExportFilterUtils. class );
    
public   static   boolean  found  =   false ;

    
private  ExportFilterUtils() {
    }

    
public   static   boolean  isExported(Context context) {
        
return  StringUtils.isNotBlank(getTableId(context));
    }

    
public   static  String getExportFileName(Context context) {

        String tableId 
=  getTableId(context);

        
if  (StringUtils.isNotBlank(tableId)) {
            String exportFileNameStr 
=  tableId  +   " _ "   +  TableConstants.EXPORT_FILE_NAME;
            String exportFileName 
=  verifyEncoding(context.getParameter(exportFileNameStr));

            
if  (logger.isDebugEnabled()) {
                logger.debug(
" eXtremeTable export file name [ "   +  exportFileNameStr  +   " ] is [ "   +  exportFileName  +   " ] " );
            }

            
return  exportFileName;
        }

        
return   null ;
    }

    
private   static  String verifyEncoding(String exportFileName) {
        nsDetector det 
=   new  nsDetector(nsPSMDetector.ALL);
        det.Init(
new  nsICharsetDetectionObserver() {
            
public   void  Notify(String charset) {
                ExportFilterUtils.found 
=   true ;
            }
        });

        
boolean  done  =   false ;
        
boolean  isAscii  =   true ;
        
byte [] buf  =  exportFileName.getBytes();
        
for  ( int  i  =   0 ; i  <  buf.length; i ++ ) {
            
if  (isAscii)
                isAscii 
=  det.isAscii(buf, i);
            
if  ( ! isAscii  &&   ! done)
                done 
=  det.DoIt(buf, i,  false );
        }
        det.DataEnd();

        
if  (isAscii) {
            
return  exportFileName;
        }
        
return  toUtf8(exportFileName);

    }

    
public   static  String toUtf8(String src) {
        
byte [] b  =  src.getBytes();
        
char [] c  =   new   char [b.length];
        
for  ( int  i  =   0 ; i  <  b.length; i ++ ) {
            c[i] 
=  ( char ) (b[i]  &   0x00FF );
        }
        
return   new  String(c);
    }

    
/**
     * There can only be one table instance (tableId) per form. If the instance
     * variable exists that means there is an export being done.
     *
     * 
@param  context
     * 
@return
     
*/
    
public   static  String getTableId(Context context) {
        
return  context.getParameter(TableConstants.EXPORT_TABLE_ID);
    }
}
      经过我们三个人测试是成功的。欢迎大家帮助测试。大家如果有任何意见、建议可与我联系: [email protected]
      压缩文件只包含: eXtremeComponents.jar eXtremeComponents.tld 和 jchardet.jar
       eXtremeComponents.rar  

0
0
0
0

你可能感兴趣的:(eXtremeComponents导出时的中文文件名问题)