Example usage for java.net URLConnection getInputStream

List of usage examples for java.net URLConnection getInputStream

Introduction

In this page you can find the example usage for java.net URLConnection getInputStream.

Prototype

public InputStream getInputStream() throws IOException 

Source Link

Document

Returns an input stream that reads from this open connection.

Usage

From source file:Main.java

static private BitmapDrawable bitmapDrawableFromURL(Resources resources, String str_url, boolean scale, int w,
        int h) {/*from w  ww .j a  v  a  2s  . c o m*/
    if (str_url == null || str_url.length() <= 0)
        return null;

    BitmapDrawable thumb = null;
    try {
        URL url = new URL(str_url);
        URLConnection connection = url.openConnection();
        connection.connect();

        InputStream stream = connection.getInputStream();
        BufferedInputStream data = new BufferedInputStream(stream);

        Bitmap bitmap = BitmapFactory.decodeStream(data);
        if (bitmap != null) {
            if (scale)
                thumb = new BitmapDrawable(resources, Bitmap.createScaledBitmap(bitmap, w, h, true));
            else
                thumb = new BitmapDrawable(resources, bitmap);
        }

    } catch (MalformedURLException e) {
        //e.printStackTrace();
    } catch (IOException e) {
        //e.printStackTrace();
    }

    return thumb;
}

From source file:Util.java

/**
 * Finds this computer's global IP address
 * /*from w  w w .  j av a  2  s.c  om*/
 * @return The global IP address, or null if a problem occurred
 */
public static Inet4Address getGlobalAddress() {
    try {
        URLConnection uc = new URL("http://www.whatismyip.org/").openConnection();
        BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
        return (Inet4Address) InetAddress.getByName(br.readLine());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;

}

From source file:Main.java

private static String getRawText(String link) throws IOException {

    URL url;/*ww  w.  ja va 2  s  . c  o m*/
    url = new URL(link);

    URLConnection connection = url.openConnection();

    BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(connection.getInputStream(), "UTF-8"));

    StringBuffer stringBuffer = new StringBuffer();
    String inputLine;
    // read from buffer line by line
    while ((inputLine = bufferedReader.readLine()) != null) {
        stringBuffer.append(inputLine + "\n");
    }
    bufferedReader.close();
    return stringBuffer.toString();
}

From source file:be.geecko.openlauncher.net.SuggestionsTask.java

private static String readUrl(String http) throws IOException {
    URL url = new URL(http);
    URLConnection connection = url.openConnection();
    InputStream in = connection.getInputStream();
    String encoding = connection.getContentEncoding();
    encoding = encoding == null ? "UTF-8" : encoding;
    return IOUtils.toString(in, encoding);
}

From source file:Main.java

public static String getResponseInFile(String address, File file) throws IOException {
    String dataString = "";

    InputStream inputStream = null;
    OutputStream outputStream = null;
    try {//from  ww w.j a v  a 2  s.c  o m
        URL url = new URL(address);
        URLConnection connection = url.openConnection();

        inputStream = connection.getInputStream();

        byte[] data;
        if (inputStream.available() > BUFFER_SIZE) {
            data = new byte[BUFFER_SIZE];
        } else {
            data = new byte[inputStream.available()];
        }

        int dataCount;
        while ((dataCount = inputStream.read(data)) > 0) {
            outputStream.write(data, 0, dataCount);
        }

        OutputStream output = new OutputStream() {
            private StringBuilder string = new StringBuilder();

            @Override
            public void write(int b) throws IOException {
                this.string.append((char) b);
            }

            //Netbeans IDE automatically overrides this toString()
            public String toString() {
                return this.string.toString();
            }
        };
        output.write(data);

        dataString = output.toString();

    } finally {
        try {
            inputStream.close();
        } catch (Exception e) {
        }
        try {
            outputStream.close();
        } catch (Exception e) {
        }
    }

    return dataString;
}

From source file:Main.java

public static File downloadFile(String urlstr, File saveFile) {
    try {//from w ww  .  ja v  a2  s . c  o  m
        URL url = new URL(urlstr);// cause speed low.
        URLConnection con = url.openConnection();
        con.setDoInput(true);
        con.connect();
        InputStream ins = con.getInputStream();
        final int bufsize = 102400;

        byte[] buffer = new byte[bufsize];
        int len = -1;
        FileOutputStream bos = new FileOutputStream(saveFile);
        while ((len = ins.read(buffer)) != -1) {
            bos.write(buffer, 0, len);

        }
        ins.close();
        bos.close();
        return saveFile;
    } catch (Error e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.google.android.feeds.ContentHandlerUtils.java

/**
 * Returns the uncompressed {@link InputStream} for the given
 * {@link URLConnection}.//from w w w  . j  a v  a  2  s.  c  o  m
 */
public static InputStream getUncompressedInputStream(URLConnection connection) throws IOException {
    InputStream source = connection.getInputStream();
    String encoding = connection.getContentEncoding();
    if ("gzip".equalsIgnoreCase(encoding)) {
        return new GZIPInputStream(source);
    } else if ("deflate".equalsIgnoreCase(encoding)) {
        boolean noHeader = true;
        Inflater inflater = new Inflater(noHeader);
        return new InflaterInputStream(source, inflater);
    } else {
        return source;
    }
}

From source file:it.unicaradio.android.utils.NetworkUtils.java

public static byte[] httpGet(String urlString) throws IOException {
    URL url = new URL(urlString);

    URLConnection ucon = url.openConnection();

    InputStream is = ucon.getInputStream();
    BufferedInputStream bis = new BufferedInputStream(is);

    // Read bytes to the Buffer until there is nothing more to read(-1).
    ByteArrayBuffer baf = new ByteArrayBuffer(50);
    int current = 0;
    while ((current = bis.read()) != -1) {
        baf.append((byte) current);
    }/* w w w.jav  a  2 s.c  o m*/

    return baf.toByteArray();
}

From source file:Main.java

private static String readXmlFromURL(String theUrl) {
    StringBuilder content = new StringBuilder();
    try {//from  www.jav a 2s. c  o m
        URL url = new URL(theUrl);
        URLConnection urlConnection = url.openConnection();

        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(urlConnection.getInputStream()));

        String line;
        while ((line = bufferedReader.readLine()) != null) {
            content.append(line + "\n");
        }
        bufferedReader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return content.toString();
}

From source file:InsertClobToMySqlServlet.java

public static String getClobsContentAsString(String urlAsString) throws Exception {
    InputStream content = null;// ww  w  . j  a va  2 s  . co m
    try {
        URL url = new URL(urlAsString);
        URLConnection urlConn = url.openConnection();
        urlConn.connect();
        content = urlConn.getInputStream();

        int BUFFER_SIZE = 1024;
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        int length;
        byte[] buffer = new byte[BUFFER_SIZE];

        while ((length = content.read(buffer)) != -1) {
            output.write(buffer, 0, length);
        }
        return new String(output.toByteArray());
    } finally {
        content.close();
    }
}