Example usage for java.net URL openConnection

List of usage examples for java.net URL openConnection

Introduction

In this page you can find the example usage for java.net URL openConnection.

Prototype

public URLConnection openConnection() throws java.io.IOException 

Source Link

Document

Returns a java.net.URLConnection URLConnection instance that represents a connection to the remote object referred to by the URL .

Usage

From source file:Main.java

public static Bitmap getImage(String urlpath) throws Exception {
    Bitmap bitmap = null;/* w  ww. jav  a2  s  . c o m*/
    URL url = new URL(urlpath);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setConnectTimeout(5 * 1000);
    if (conn.getResponseCode() == 200) {
        InputStream inputStream = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(inputStream);
    }
    return bitmap;
}

From source file:WebReader.java

static void getData(String address) throws Exception {
    URL page = new URL(address);
    StringBuffer text = new StringBuffer();
    HttpURLConnection conn = (HttpURLConnection) page.openConnection();
    conn.connect();/*from  www . java 2 s  . c  o m*/
    InputStreamReader in = new InputStreamReader((InputStream) conn.getContent());

    BufferedReader buff = new BufferedReader(in);
    String line = buff.readLine();
    while (line != null) {
        text.append(line + "\n");
        line = buff.readLine();
    }
    System.out.println(text.toString());
}

From source file:Main.java

public static boolean WriteFileFromUrl(String url, String filename) {
    try {/*from w w w .jav  a 2s.c o  m*/
        URL fileUrl = new URL(url);
        URLConnection connection = fileUrl.openConnection();
        InputStream inputStream = new BufferedInputStream(fileUrl.openStream(), 10240);
        File cacheFile = new File(filename);
        FileOutputStream outputStream = new FileOutputStream(cacheFile);

        byte buffer[] = new byte[1024];
        int dataSize;
        int loadedSize = 0;
        while ((dataSize = inputStream.read(buffer)) != -1) {
            loadedSize += dataSize;
            outputStream.write(buffer, 0, dataSize);
        }

        outputStream.close();
        return true;
    } catch (Exception e) {
        Log.d("#StorageHelper Error:", e.toString());
        return false;
    }
}

From source file:Main.java

private static InputStream openStream(URL url) throws IOException {
    URLConnection connection = url.openConnection();
    if (connection instanceof JarURLConnection) {
        // See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5041014
        connection.setUseCaches(false);/*  w w w .ja  v a 2s.  com*/
    }
    InputStream is = connection.getInputStream();
    return is;
}

From source file:Main.java

public static Bitmap getBitmapFromRemote(String address) {
    Bitmap bitmap = null;//from   w  ww .j a  v a 2s . c  o m
    try {
        URL url = new URL(address);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(is);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap getBitmapFromURL(String strURL) {
    try {/*from  w w w.  j  a va  2 s.  c o m*/
        URL url = new URL(strURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        return BitmapFactory.decodeStream(input);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.cloudhopper.commons.io.demo.FileServerMain.java

private static void saveFileFromUrl(URL url, String path) throws Exception {
    URLConnection urlc = url.openConnection();
    BufferedInputStream bis = new BufferedInputStream(urlc.getInputStream());
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(path)));
    int i;/*  w w  w.j  a  v  a2s .  c  o m*/
    while ((i = bis.read()) != -1) {
        bos.write(i);
    }
    bis.close();
    bos.close();
}

From source file:Main.java

public static Bitmap fetchImage(String fileUrl) {
    try {//from   ww w.  ja v a 2s. com
        URL myFileUrl = new URL(fileUrl);
        HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        Bitmap image = BitmapFactory.decodeStream(is);
        is.close();
        return image;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static byte[] fetchUrlToByte(String urlStr) {

    try {//from  www . j  a v a2  s .c  om
        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 String ParserHtml(String url) {
    String html = "";
    BufferedReader in = null;//from  w  ww .jav  a 2s .  c  om
    try {
        URL realUrl = new URL(url);
        URLConnection connection = realUrl.openConnection();
        connection.setRequestProperty("User-agent", "Mozilla/5.0");
        connection.connect();
        in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            html += line;
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return html;
}