Encoding conversion options in struts2

I have a struts2 web application that accepts POST and GET requests in many different encodings, converts them to utf-8, displays the correct utf-8 characters on the screen, and then writes them to the utf-8 database.

I tried at least 5 different methods for simple conversion of seamless coding of windows-1250 to utf-8, and all of them did not work. Utf-8 is a "big set", it should work without problems (at least this is my understanding).

Can you suggest how to do a charset conversion from windows-1250 to utf-8, and is it possible that struts2 does something strange with charset params, which explains why I can't seem to be true.

This is my last attempt:

String inputData = getSimpleParamValue("some_input_param_from_get"); Charset inputCharset = Charset.forName("windows-1250"); Charset utfCharset = Charset.forName("UTF-8"); CharsetDecoder decoder = inputCharset.newDecoder(); CharsetEncoder encoder = utfCharset.newEncoder(); String decodedData = ""; try { ByteBuffer inputBytes = ByteBuffer.wrap(inputData.getBytes()); // I've tried putting UTF-8 here as well, with no luck CharBuffer chars = decoder.decode(inputBytes); ByteBuffer utfBytes = encoder.encode(chars); decodedData = new String(utfBytes.array()); } catch (CharacterCodingException e) { logger.error(e); } 

Any ideas on how to try to get this to work?

Thanks and best regards,

bozo

0
source share
1 answer

I am not sure about your scenario. In Java, String is Unicode, deals only with charset conversion, when you need to convert from / to String to / from binary representation. In your example, when getSimpleParamValue ("some_input_param_from_get") is called, inputData should already have the "correct" string, the conversion from the byte stream (which was moved from the client browser to the web server) to the string should already be done part (web server responsibility + web tier of your application). To do this, I use UTF-8 for webcasting by placing a filter in web.xml (before Struts), for example:

 public class CharsetFilter implements Filter { public void destroy() {} public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; req.setCharacterEncoding("UTF-8"); chain.doFilter(req, res); String contentType = res.getContentType(); if( contentType !=null && contentType.startsWith("text/html")) res.setCharacterEncoding("UTF-8"); } public void init(FilterConfig filterConfig) throws ServletException { } } 

If you cannot do this, and if your getSimpleParamValue () is “mistaken" in the encoding conversion (for example: it suggested that the byte stream was UTF-8 and was Windows-1250), now you have the “wrong” line, and you should try to restore it by canceling and converting the byte to string conversion - in this case you should know the wrong AND correct encoding - and, even worse, deal with the possibility of missing characters (if it was interpreted as UTF8, I maight found an illegal char sequence ) If you have to deal with this in the Struts2 action, I would say that you have problems, you should deal with it explicitly before / after it (in the top web layer - either in the database driver or in the file encoding or whatever something else)

0
source

All Articles