0

Have an interesting issue.. We have a wordpress XML / RSS service behind our firewall that is localized to multiple countries and works perfectly with chinese, russian, etc.

To expose this service publically we created a simple JSP service that for some reason won't render the foreign characters.

Any ideas would be greatly appreciated.

<%@ page language="java" contentType="application/xml; charset=utf-8" pageEncoding="utf-8"%>
<%@ page session="false"%>
<%@ page import='java.io.BufferedReader' %> 
<%@ page import='java.io.InputStreamReader' %> 
<%@ page import='java.io.IOException' %> 
<%@ page import='java.io.PrintWriter' %> 
<%@ page import='java.net.URL' %> 
<%@ page import='java.net.URLConnection' %> 

<%

//final String URL_BASE = "http://localhost:8080/index.php/";
final String URL_BASE = "http://INTERNALSERVERIP:8888/index.php/";
final String NL = System.getProperty("line.separator");

BufferedReader br = null;
try
{
    String feedName = request.getParameter("name");

    // if no feed was specified
    if (feedName == null || feedName.trim().length() == 0)
    {
        feedName = "feed1";
    }

    // set the URL based off the feed name
    String urlStr = URL_BASE + feedName.trim().toLowerCase();
    System.out.println("urlStr: " + urlStr);

    URL url = new URL(urlStr);
    URLConnection urlCon = url.openConnection();

    br = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));

    String resLine = null;
    while ((resLine = br.readLine()) != null) 
    {
        resLine= new String(resLine.getBytes("ISO8859_1"), "UTF8");  
        out.println(resLine);
    }
}
catch (Throwable t)
{
    t.printStackTrace(new PrintWriter(out));
}
finally
{
    if (br != null)
    {
        try
        {
            br.close();
        }
        catch (IOException ioe)
        {}
    }
}
%>
Ryan Goodman
  • 165
  • 1
  • 7

1 Answers1

1

As suggested by Jasper de Vries create InputStreamReader with UTF8 charset (as given below).

br = new BufferedReader(new InputStreamReader(urlCon.getInputStream(), "UTF8"));

Then, you dont need to convert the charset of String in the loop, so you can remove/comment it. So below is the modified version of the jsp.

<%@ page language="java" contentType="application/xml; charset=utf-8" pageEncoding="utf-8"%>
<%@ page session="false"%>
<%@ page import='java.io.BufferedReader' %> 
<%@ page import='java.io.InputStreamReader' %> 
<%@ page import='java.io.IOException' %> 
<%@ page import='java.io.PrintWriter' %> 
<%@ page import='java.net.URL' %> 
<%@ page import='java.net.URLConnection' %> 

<%

//final String URL_BASE = "http://localhost:8080/index.php/";
final String URL_BASE = "http://INTERNALSERVERIP:8888/index.php/";
final String NL = System.getProperty("line.separator");

BufferedReader br = null;
try
{
    String feedName = request.getParameter("name");

    // if no feed was specified
    if (feedName == null || feedName.trim().length() == 0)
    {
        feedName = "feed1";
    }

    // set the URL based off the feed name
    String urlStr = URL_BASE + feedName.trim().toLowerCase();
    System.out.println("urlStr: " + urlStr);

    URL url = new URL(urlStr);
    URLConnection urlCon = url.openConnection();

    br = new BufferedReader(new InputStreamReader(urlCon.getInputStream(), "UTF8"));

    String resLine = null;
    while ((resLine = br.readLine()) != null) 
    {
        // resLine= new String(resLine.getBytes("ISO8859_1"), "UTF8");  
        out.println(resLine);
    }
}
catch (Throwable t)
{
    t.printStackTrace(new PrintWriter(out));
}
finally
{
    if (br != null)
    {
        try
        {
            br.close();
        }
        catch (IOException ioe)
        {}
    }
}
%>

Hope it helps. :)

A P
  • 166
  • 1
  • 3