幸せのちから

平凡的世界
看似平常实崎岖
成如容易却艰辛

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);
    }
}
      经过我们三个人测试是成功的。欢迎大家帮助测试。大家如果有任何意见、建议可与我联系: xplucky@gmail.com
      压缩文件只包含: eXtremeComponents.jar eXtremeComponents.tld 和 jchardet.jar
       eXtremeComponents.rar  

posted on 2006-04-08 18:03 Lucky 阅读(2986) 评论(14)  编辑  收藏 所属分类: extremeComponents

评论

# re: eXtremeComponents导出时的中文文件名问题 2006-04-08 21:07 江南白衣

期望能尽快将此特性改进核心版本里,springside就可以去掉对et的扩展了:)  回复  更多评论   

# re: eXtremeComponents导出时的中文文件名问题 2006-04-09 14:58 xplucky

我也想在eXtremeComponents正式发布之前将此特性纳入核心版本,所以希望大家能帮助测试一下,看看会不会有别的暂时还没有想到的问题。  回复  更多评论   

# re: eXtremeComponents导出时的中文文件名问题 2006-04-11 12:43 Ivan Chen

这里有必要搞一个自己的toUtf8方法吗?直接用URLEncoder.encode也可以啊。  回复  更多评论   

# re: eXtremeComponents导出时的中文文件名问题 2006-04-11 12:53 xplucky

@Ivan Chen
呵呵,如果你能给我一个示例的话那会更好。
  回复  更多评论   

# re: eXtremeComponents导出时的中文文件名问题 2006-11-08 16:30 dodo

请问如何用eXtremeComponents实现点击表头,按中文拼音排序的功能?  回复  更多评论   

# re: eXtremeComponents导出时的中文文件名问题 2006-11-09 11:46 yaolin

我只是把ExportFilterUtils中的

exportFileName = context.getParameter(exportFileNameStr);

改成:

try {
exportFileName = URLEncoder.encode(context
.getParameter(exportFileNameStr), DEFAULT_ENCODING);
} catch (UnsupportedEncodingException e) {
exportFileName = context.getParameter(exportFileNameStr);
logger.error(e);
}

就好用了。  回复  更多评论   

# re: eXtremeComponents导出时的中文文件名问题 2006-11-09 12:09 yaolin

哦,补充一句,那个DEFAULT_ENCODING实在开头定义的:

private static final String DEFAULT_ENCODING = "UTF-8";  回复  更多评论   

# re: eXtremeComponents导出时的中文文件名问题 2006-11-23 17:40

我测试不行吗,“业务室名称”文件名变成CA4H2V41  回复  更多评论   

# re: eXtremeComponents导出时的中文文件名问题 2006-11-23 17:53

两位,怎么偶试都不行呢

  回复  更多评论   

# 无法导出xls文件 2007-06-01 15:42 sweetleaf

我在appfuse中,试着使用eXtremeComponents的标签库,test.jsp页面能正常显示,但是加入<ec:exportXls
fileName="dd.xls"
tooltip="Export Excel"
text="XLS"
/>后,点击导出xls的图标,页面跳转到一个空页面,名称也为test.jsp,无任何内容显示,请问,这是怎么回事?
  回复  更多评论   

# re: eXtremeComponents导出时的中文文件名问题 2007-06-29 18:44 peterwillcn

@sweetleaf
需要在web.xml文件中加入eXtremeComponents的导出过滤器的配置,内容如下:

<filter> <filter-name>eXtremeExport</filter-name> <filter-class>org.extremecomponents.table.filter.ExportFilter</filter-class> <init-param> <param-name>responseHeadersSetBeforeDoFilter</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>eXtremeExport</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
这样筛选功能就变成乱码了  回复  更多评论   

# re: eXtremeComponents导出时的中文文件名问题 2009-01-06 17:30 Ivan Chen(西滨)

toUtf8在ie7下出错。  回复  更多评论   

# re: eXtremeComponents导出时的中文文件名问题 2009-05-13 20:01 jti

导出excel时,数据列长度过长,导致显示为 科学技术法,请问如何解决这种问题?  回复  更多评论   

# re: eXtremeComponents导出时的中文文件名问题 2009-05-14 00:05 Lucky

这个项目已经快3年没有维护了,建议你看看GT-grid http://ecside.group.javaeye.com/  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2006年4月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

导航

随笔分类(125)

文章分类(5)

日本語

搜索

积分与排名

最新随笔

最新评论