将 UTF-8 转换为 windows-1252 并在 tomcat v7 上写入 gwt 2.7.0 中的 csv
Convert UTF-8 to windows-1252 and write into csv in gwt 2.7.0 on tomcat v7
我在将 UTF-8 转换为 windows-1252 时遇到问题。我必须输出像 ²,³,° 这样的符号。客户想打开Excel中的文件,而不是通过双击导入文件。
系统:
gwt 2.7.0 中的前端大量使用 gxt 3.1.4
客户端的服务器是 tomcat v7
测试是在服务器
中的 gwt 构建上完成的
现在的问题是,该应用程序支持日文符号,这些符号在 UTF-8 中显示完美,但在 windows-1252 中显示不佳。另一方面,显示 ²、³、° 符号。当前的解决方案是收集 csv
的行并将它们放在 FormPanel
内的隐藏字段中。然后 FormPanel
被编码并提交。
public void postCsvForExcel( String url, Map<String, String> postData )
{
setSize( "0px", "0px" );
setVisible( false );
sinkEvents( Event.ONLOAD );
setMethod( FormPanel.METHOD_POST );
setEncoding( FormPanel.ENCODING_URLENCODED );
VerticalPanel panel = new VerticalPanel();
add( panel );
for( Entry<String, String> data : postData.entrySet() )
{
Hidden hiddenField = new Hidden( data.getKey(), data.getValue() );
panel.add( hiddenField );
}
SubmitButton submit = new SubmitButton();
panel.add( submit );
setAction( url );
FormElement.as( this.getElement() ).setAcceptCharset( "Cp1252" );
RootPanel.get().add( this );
submit();
}
日语字符仅在header中显示。面对这个问题,我扩展了 HttpServlet
for POST 操作以翻译 UTF-8
如下所示,并从上面的方法中删除了 FormElement.as( this.getElement() ).setAcceptCharset( "Cp1252" );
部分。
public class ExporterServlet extends HttpServlet {
public ExporterServlet() {
}
@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
super.service(arg0, arg1);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String filename = req.getParameter("filename");
String content = req.getParameter("content");
if(filename != null) {
//resp.setContentType( getContentType( filename ) + "; charset=utf-8" );
resp.setContentType( "text/csv" + "; charset=windows-1252" );
resp.setHeader( "Content-Disposition", "attachment;filename=\"" + filename + "\"" );
resp.setIntHeader("Expires", 0);
resp.setContentLength(content.length());
resp.setStatus(200);
//resp.setCharacterEncoding( "UTF-8" );
resp.setCharacterEncoding( "windows-1252" );
//byte[] destinationBytes = content.getBytes( "utf-8" );
ByteBuffer bb = ByteBuffer.wrap( content.getBytes() );
CharBuffer cb = Charset.forName( "UTF-8" ).decode( bb );
bb = Charset.forName( "windows-1252" ).encode( cb );
resp.getOutputStream().write( bb.array() );
resp.getOutputStream().flush();
}
}
}
但这似乎行不通。我错过了什么吗?
更多信息:我观察到一件奇怪的事情。 doPost
方法虽然被调用,但对文件的编码没有影响。我试图用 UTF-8 对其进行编码,但输出仍然是 windows-1252。当我把之前方法中FormPanel
的编码去掉后,结果是UTF-8.
另一个问题是,windows-1252 的正确编码是什么,我已经尝试了两个版本,cp1252 和 windows-1252,我看不出结果有什么不同。
cp1252 无法显示日语字符。指令本身没有得到适当的调查。客户不知道 cp1252 有什么能力。在制定解决方案之前应该检查一下。
您可以添加以下代码,再试一次:
filename = new String(filename.getBytes(), "ISO-8859-1");
我在将 UTF-8 转换为 windows-1252 时遇到问题。我必须输出像 ²,³,° 这样的符号。客户想打开Excel中的文件,而不是通过双击导入文件。
系统: gwt 2.7.0 中的前端大量使用 gxt 3.1.4 客户端的服务器是 tomcat v7 测试是在服务器
中的 gwt 构建上完成的现在的问题是,该应用程序支持日文符号,这些符号在 UTF-8 中显示完美,但在 windows-1252 中显示不佳。另一方面,显示 ²、³、° 符号。当前的解决方案是收集 csv
的行并将它们放在 FormPanel
内的隐藏字段中。然后 FormPanel
被编码并提交。
public void postCsvForExcel( String url, Map<String, String> postData )
{
setSize( "0px", "0px" );
setVisible( false );
sinkEvents( Event.ONLOAD );
setMethod( FormPanel.METHOD_POST );
setEncoding( FormPanel.ENCODING_URLENCODED );
VerticalPanel panel = new VerticalPanel();
add( panel );
for( Entry<String, String> data : postData.entrySet() )
{
Hidden hiddenField = new Hidden( data.getKey(), data.getValue() );
panel.add( hiddenField );
}
SubmitButton submit = new SubmitButton();
panel.add( submit );
setAction( url );
FormElement.as( this.getElement() ).setAcceptCharset( "Cp1252" );
RootPanel.get().add( this );
submit();
}
日语字符仅在header中显示。面对这个问题,我扩展了 HttpServlet
for POST 操作以翻译 UTF-8
如下所示,并从上面的方法中删除了 FormElement.as( this.getElement() ).setAcceptCharset( "Cp1252" );
部分。
public class ExporterServlet extends HttpServlet {
public ExporterServlet() {
}
@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
super.service(arg0, arg1);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String filename = req.getParameter("filename");
String content = req.getParameter("content");
if(filename != null) {
//resp.setContentType( getContentType( filename ) + "; charset=utf-8" );
resp.setContentType( "text/csv" + "; charset=windows-1252" );
resp.setHeader( "Content-Disposition", "attachment;filename=\"" + filename + "\"" );
resp.setIntHeader("Expires", 0);
resp.setContentLength(content.length());
resp.setStatus(200);
//resp.setCharacterEncoding( "UTF-8" );
resp.setCharacterEncoding( "windows-1252" );
//byte[] destinationBytes = content.getBytes( "utf-8" );
ByteBuffer bb = ByteBuffer.wrap( content.getBytes() );
CharBuffer cb = Charset.forName( "UTF-8" ).decode( bb );
bb = Charset.forName( "windows-1252" ).encode( cb );
resp.getOutputStream().write( bb.array() );
resp.getOutputStream().flush();
}
}
}
但这似乎行不通。我错过了什么吗?
更多信息:我观察到一件奇怪的事情。 doPost
方法虽然被调用,但对文件的编码没有影响。我试图用 UTF-8 对其进行编码,但输出仍然是 windows-1252。当我把之前方法中FormPanel
的编码去掉后,结果是UTF-8.
另一个问题是,windows-1252 的正确编码是什么,我已经尝试了两个版本,cp1252 和 windows-1252,我看不出结果有什么不同。
cp1252 无法显示日语字符。指令本身没有得到适当的调查。客户不知道 cp1252 有什么能力。在制定解决方案之前应该检查一下。
您可以添加以下代码,再试一次:
filename = new String(filename.getBytes(), "ISO-8859-1");