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:com.joliciel.jochre.search.webClient.SearchWebClientUtils.java

public static String getJson(URL url) {
    try {/*from www .j av a  2  s. c  o m*/
        URLConnection con = url.openConnection();
        InputStream in = con.getInputStream();
        StringWriter writer = new StringWriter();
        IOUtils.copy(in, writer, "UTF-8");
        String json = writer.toString();
        return json;
    } catch (IOException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Bitmap downloadImage(String urlStr) throws IOException {
    URL url = new URL(urlStr);
    URLConnection connection = url.openConnection();
    BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
    Bitmap result = BitmapFactory.decodeStream(bis);
    bis.close();/* www  . j  a  v  a 2  s  .c om*/
    return result;
}

From source file:Main.java

private static String readAsString(URLConnection connection) throws IOException {
    InputStream inputStream = connection.getInputStream();
    Reader reader = new InputStreamReader(inputStream, "UTF-8");
    try {//from  w w  w . ja  va  2  s . co  m
        StringBuilder sb = new StringBuilder();
        {
            char[] buffer = new char[2048];
            while (true) {
                int read = reader.read(buffer);
                if (read < 0)
                    break;
                sb.append(buffer, 0, read);
            }
        }

        return sb.toString();
    } finally {
        try {
            reader.close();
        } catch (IOException ignored) {
        }
    }
}

From source file:Main.java

public static Document openUrl(String location) throws Exception {
    URL url = new URL(location);
    //System.out.println(url);

    URLConnection con = url.openConnection();
    InputStream responseStream = con.getInputStream();

    Document result = loadFromStream(responseStream);
    responseStream.close();//from w w w . j a va 2s .  c  o m
    return result;
}

From source file:Main.java

public static byte[] fetchUrlToByte(String urlStr) {

    try {/* w  w  w  . ja  va2  s.  c  o m*/
        URL url = new URL(urlStr);
        URLConnection urlConnection = url.openConnection();
        InputStream is = urlConnection.getInputStream();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];
        int len = 0;

        while ((len = is.read(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }

        return baos.toByteArray();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] downloadImage(String imageUrl) {
    if (imageUrl.endsWith(".jpg") || imageUrl.endsWith(".bmp") || imageUrl.endsWith(".png")
            || imageUrl.endsWith(".gif")) {
        try {/*from  ww w.  j  a  va2 s  .  co m*/
            URL url = new URL(imageUrl);
            URLConnection urlConn = url.openConnection();
            InputStream is = urlConn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;

            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }

            return baf.toByteArray();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return null;
}

From source file:Main.java

public static String getContents(String url) {
    String contents = "";

    try {/* w  w  w  .  j  av  a 2  s  . c  o  m*/
        URLConnection conn = new URL(url).openConnection();

        InputStream in = conn.getInputStream();
        contents = convertStreamToString(in);
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }

    return contents;
}

From source file:com.digitallizard.bbcnewsreader.resource.web.HtmlParser.java

/**
 * @param args//from   ww w  . j av a2 s.co  m
 * @throws IOException
 * @throws ClientProtocolException
 */
public static byte[] getPage(String stringUrl) throws Exception {
    URL url = new URL(stringUrl);
    URLConnection connection = url.openConnection();

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

    ByteArrayBuffer arraybuffer = new ByteArrayBuffer(50);
    int current = 0;
    while ((current = inputbuffer.read()) != -1) {
        arraybuffer.append((byte) current);
    }
    return arraybuffer.toByteArray();
}

From source file:Main.java

public static InputStream HandlerData(String url) {
    InputStream inStream = null;//from   w ww  .j ava 2s  .c  om

    try {
        URL feedUrl = new URL(url);
        URLConnection conn = feedUrl.openConnection();
        conn.setConnectTimeout(10 * 1000);
        inStream = conn.getInputStream();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return inStream;
}

From source file:Main.java

public static String getJsonString(String urlString) throws Exception {
    InputStream is = null;/*from  w  w w.  j  a va  2  s .  c  o  m*/
    Reader reader = null;
    StringBuilder str = new StringBuilder();
    URL url = new URL(urlString);
    URLConnection URLConn = url.openConnection();
    URLConn.setRequestProperty("User-agent", "IE/6.0");
    is = URLConn.getInputStream();
    reader = new InputStreamReader(is, "UTF-8");
    char[] buffer = new char[1];
    while (reader.read(buffer) != -1) {
        str.append(new String(buffer));
    }
    return str.toString();
}