Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Convert ISO-8859-1 to UTF-8

807589Oct 29 2008 — edited Oct 31 2008
Dear All,

I am making an HTTP request in a normal Java application. The contents of the html page that i am requesting is encoded using ISO-8859-1.

I want to be able to convert that data to UTF-8 since I want to store the content in an MySQL database. Which is the best way in which I can do the conversion.

Following is my HTTP request code:

public static String MakeHTTPRequestISO(String requestURL)
{
try {
//Create a URL object from the passed URL parameter.
URL url = new URL(requestURL);

//Create a URL connection
URLConnection urlConn = url.openConnection();

//Get an input stream reader for reading.
InputStream input = urlConn.getInputStream();

//Create a buffered reader for efficiency
//purposes.
BufferedInputStream bi = new BufferedInputStream(input);

//Create a byte array that will store the response. The length of this array is
//set to the size of the response.
byte [] result = new byte[urlConn.getContentLength()];

//This is an integer that stores that offset of this area. The offset increases
//with every read.
int offset = 0;

//The read method of the buffered input stream returns the number of bytes read.
int bytesRead = 0;

//Read until the byte array has been filled.
while ((urlConn.getContentLength()-offset>0)&&((bytesRead = bi.read(result, offset, urlConn.getContentLength()-offset))!=-1))
{
//increment the offset with the number of bytes read
offset += bytesRead;
}

//Get the resultant string from the byte array. Use the UTF-8 encoding because
//of the Language symbols.
String resultString = new String(result,"ISO-8859-1");

//Return the result
return resultString;
}
catch (Exception ex) {
//return null if an exception is caught.
return null;
}
}

What I would like is a method by which I can convert the string from the method above to a UTF-8 string in order to be able to store it in a database.

Many Thanks for your help,
Chris Farrugia
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 28 2008
Added on Oct 29 2008
14 comments
2,670 views