Can't read js file from url in correct encoding

I want to read js file as string from url https://d3c3cq33003psk.cloudfront.net/opentag-67008-473432.js

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) {
            sb.append(line).append("\n");
        }
        in.close();
    } 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?

Solution:

It’s GZIPped. Use a GZIPInputStream.

UPDATE

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

UPDATE 2

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"));
Advertisements

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 = ImageIO.read(new URL(url2));
System.out.println(bi);

This code always fails with

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

What am I doing wrong?

Solution:

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();
System.out.println(validUrl);
BufferedImage bi = ImageIO.read(new URL(validUrl));

Console Output:
http://xn--nw2a.xn--j6w193g/images/wonton.jpg

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

REST

REpresentational State Transfer

call from client to server get data back

hypermedia as a engine to application state.

self-descriptive messages

http://www.ics.uci.edu/~fielding/pubs/dissertation/introduction.htm