Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4597052
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:12:09+00:00 2026-05-21T23:12:09+00:00

Some pages have HTML special characters in their content, but they are appearing as

  • 0

Some pages have HTML special characters in their content, but they are appearing as a square (an unknown character).

What can I do?

Can I convert the String containg the carachters to another format(UTF-8)? It’s in the conversion from InputStream to String that happens this. I really don’t know what causes it.

public HttpURLConnection openConnection(String url) {
    try {
        URL urlDownload = new URL(url);
        HttpURLConnection con = (HttpURLConnection) urlDownload.openConnection();
        con.setInstanceFollowRedirects(true);
        con.connect();
        return con;
    } catch (Exception e) {
        return null;
    }
}

private String getContent(HttpURLConnection con) {
    try {
        return IOUtils.toString(con.getInputStream());
    } catch (Exception e) {
        System.out.println("Erro baixando página: " + e);
        return null;
    }
}

page.setContent(getContent(openConnection(con)));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:12:09+00:00Added an answer on May 21, 2026 at 11:12 pm

    You need to read the InputStream using InputStreamReader with the charset as specified in the Content-Type header of the downloaded HTML page. Otherwise the platform default charset will be used, which is apparently not the same as the HTML’s one in your case.

    Reader reader = new InputStreamReader(input, "UTF-8");
    // ...
    

    You can of course also use a HTML reader/parser like Jsoup which takes this automatically into account.

    String html = Jsoup.connect("http://stackoverflow.com").get().html();
    

    Update: as per your updated question, you seem to be using URLConnection to request the HTML page and IOUtils to convert InputStream to String. You need to use it as follows:

    String contentType = connection.getHeaderField("Content-Type");
    String charset = "UTF-8"; // Default to UTF-8
    for (String param : contentType.replace(" ", "").split(";")) {
        if (param.startsWith("charset=")) {
            charset = param.split("=", 2)[1];
            break;
        }
    }
    
    String html = IOUtils.toString(input, charset);
    

    If you’re still having problems with getting the characters right, then it can only mean that the console/viewer where you’re printing those characters to doesn’t support the charset. E.g., when you run the following in Eclipse

    System.out.println(html);
    

    Then you need to ensure that the Eclipse console uses UTF-8. You can set it by Window > Preferences > General > Workspace > Text File Encoding.

    Or if you’re writing it to some file by FileWriter, then you should rather be using InputStream/OutputStream from the beginning on without converting it to String first. If converting to String is really an important step, then you need to write it to new OutputStreamWriter(output, "UTF-8").

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of html pages which may contain certain encoded characters. Some
I have some URL that contains special characters. For example: http://www.example.com/bléèàû.html If you type
I'm using XSLT to extract some HTML content with special characters (like   )
I have a 404.html page, but in some cases I want to be able
I have a Tomcat 5.5 server that hosts some HTML pages. I want to
I'm trying to work with special chars in URLs but I have some trouble
I have HTML page with some HTML element with ID=logo . I need to
How to find all files in directory? I have HTML page in some directory.
I have an HTML page that contains some filenames that i want to download
I have a page with some HTML in it. and I need to set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.