Convert Java string encoding to web page

I have a webpage that is encoded (via its title bar) as WIN-1255. The Java program creates a text string that is automatically added to the page. The problem is that the source strings are encoded in UTF-8, creating a Gibberish text field on the page.

Unfortunately, I can’t change the page encoding - this is required by the client’s compliance system.

Any ideas?

UPDATE:

The page I am creating is an RSS feed that should be installed on WIN-1255, showing information taken from another feed that is encoded in UTF-8.

SECOND UPDATE:

Thanks for all the answers. I managed to convert the string, and yet Gibberish. The problem was that in addition to the header encoding, an XML encoding should be set.

Adam

+3
source share
5 answers

To do this, you need to set the encoding of the author of the response . With only the response header, you basically only instruct the client application, which encoding is used to interpret / display the page. This will not work if the answer itself is written with a different encoding.

, , ( , ), :

JSP, JSP, :

<%@ page pageEncoding="WIN-1255" %>

Servlet, :

response.setCharacterEncoding("WIN-1255");

Content-Type charset, / . . .

, API java.net / java.io, OutputStreamWriter, 2 , :

Writer writer = new OutputStreamWriter(someOutputStream, "WIN-1255");
+2

, ( ) win-1255:

import java.nio.charset.*;
import java.nio.*;
Charset win1255 = Charset.forName("windows-1255");
ByteBuffer bb = win1255.encode(someString);
byte[] ba = new byte[bb.limit()];

ba .

EDIT: , ba, . , , :

ServletOutputStream os = ...
os.write(ba);

setContentType("text/html; charset=windows-1255") (setContentType), getWriter. , Windows-1255 HTTP.

, UTF-8, . UTF-8 , . InputStreamReader (someInputStream, Charset.forName( "utf-8" ))

+1

? ( UTF-8), - (Win-1255), Java ( - ) Win-1255 .

, ( -? Java?), .

0
source

The page I am creating is an RSS feed that should be installed on WIN-1255, showing information taken from another feed that is encoded in UTF-8.

In this case, use the parser to load the UTF-8 XML code. This should correctly decode the data for UTF-16 character data (Java strings are always UTF-16). Your output mechanism must be encoded from UTF-16 to Windows-1255.

0
source
byte[] originalUtf8;//Here input

//utf-8 to java String:
String internal = new String(originalUtf8,Charset.forName("utf-8");
//java string to w1255 String
byte[] win1255 = internal.getBytes(Charset.forName("cp1255"));

//Here output
0
source

Source: https://habr.com/ru/post/1731175/


All Articles