I am facing a problem with converting UTF-8 to windows-1252. I have to
output symbols like ²,³,°. The customer wants to open the file in Excel
without importing the file by double clicking.
System: Frontend in gwt 2.7.0 with massive usage of gxt 3.1.4 Server on
customer side is a tomcat v7 Testing is done on gwt build in server
The problem right now is, that the application supports Japanese symbols,
which are displayed perfectly fine in UTF-8 but not in windows-1252. On the
other hand, the ²,³,° symbols are displayed. The current solution is to
collect the rows of the csv and put them in hidden fields inside a FormPanel.
The FormPanel is then encoded and submitted.
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();
}
The Japanese characters are only displayed in the header. Facing this
problem, I have extended the HttpServlet for POST operations to translate
the UTF-8 like following and removed the FormElement.as( this.getElement()
).setAcceptCharset( "Cp1252" ); part from the method above.
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();
}
}}
But this seems not to work. Am I missing sth.
Further information: I have observed one strange thing. The doPost method,
although being called has no effect on the encoding of the file. I have
tried to encode it in UTF-8 but the output was still windows-1252. When I
removed the encoding of the FormPanel in the method before, the result was
UTF-8.
Another question is, what is the correct encoding for windows-1252, I have
tried both versions, cp1252 and windows-1252, I cant spot a difference in
the result.
--
You received this message because you are subscribed to the Google Groups "GWT
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.