Can't read js file from url in correct encoding

I want to read js file as string from url

I tried several ways (to read from url or to download and then read), but all the time I received unreadable characters, like �(��_�s��d������:`���.����i�….
The ways I tried it:
1. dowload file from url:

FileUtils.copyURLToFile(jsUrl, file);

2. read from url:

    StringBuilder sb = new StringBuilder();
    try {
        URL url = new URL(jsUrl);
        // read text returned by server
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
        String line;
        while ((line = in.readLine()) != null) {
    } catch (Exception e) {
    return sb.toString();

If I download the file manually from the url (page-> save as…) – it could be opened with Notepad++ in normal UTF-8 encoding.
Could anybody help me to handle the weird file?


It’s GZIPped. Use a GZIPInputStream.


        InputStream stream = url.openStream();
        if ("gzip".equalsIgnoreCase(cnt.getHeaderField("Content-Encoding"))) {
            stream = new GZIPInputStream(stream);
        BufferedReader in = new BufferedReader(new InputStreamReader(stream, "UTF-8"));


With URLConnection:

        URLConnection cnt = url.openConnection();
        InputStream stream = cnt.getInputStream();
        if ("gzip".equalsIgnoreCase(cnt.getHeaderField("Content-Encoding"))) {
            stream = new GZIPInputStream(stream);
        BufferedReader read = new BufferedReader(new InputStreamReader(stream, "UTF-8"));

Fetch image from URL with kanji

I want to fetch an image from a remote URL.

String url =  "http://見.香港/images/wonton.jpg";
String url2 = IDN.toUnicode(url);
BufferedImage bi = URL(url2));

This code always fails with

javax.imageio.IIOException: Can’t get input stream from URL!
Caused by: 見.香港

What am I doing wrong?


Only encode the host portion of the URL and make sure you are using IDN.toASCII() rather than IDN.toUnicode()

String fullUrl = "http://見.香港/images/wonton.jpg";
URL url = new URL(fullUrl);

url.getProtocol(); // "http"
url.getHost(); // "見.香港"
url.getPath(); // "/images/wonton.jpg"

String asciiHost = IDN.toASCII(url.getHost());
String validUrl = url.getProtocol() + "://" + asciiHost + url.getPath();
BufferedImage bi = URL(validUrl));

Console Output:

Note that you might need to URLEncode the resource portion of the URI if it were to contain characters such as spaces.

Git error: fatal: No such remote ‘origin’

Execute the following commands in git bash:

  1.  git remote add origin <url>
  2. git remote set-url –push origin <url>
  3. git push -u origin master


REpresentational State Transfer

call from client to server get data back

hypermedia as a engine to application state.

self-descriptive messages