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

public static Drawable getDrawable(String urlpath) {
    Drawable d = null;/*  www .  j  a  va2s . c  o m*/
    try {
        URL url = new URL(urlpath);
        URLConnection conn = url.openConnection();
        conn.connect();
        InputStream in;
        in = conn.getInputStream();
        d = Drawable.createFromStream(in, "background.jpg");
        // TODO Auto-generated catch block
    } catch (IOException e) {
        e.printStackTrace();
    }
    return d;
}

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;/*from  w  w w. java  2  s.  c  o  m*/
    while ((i = bis.read()) != -1) {
        bos.write(i);
    }
    bis.close();
    bos.close();
}

From source file:Main.java

/** Downloads a file from the specified URL and stores the file to the specified location 
 * @param fileUrl the URL from which the file should be downloaded
 * @param storageLocation the location to which the downloaded file should be stored. If the file exists, it will
 * be overridden! //ww w  . ja  va  2 s .  co  m
 * @throws IOException */
public static void downloadFileFromWebserver(String fileUrl, String storageLocation) throws IOException {
    URL url = new URL(fileUrl);
    File file = new File(storageLocation);

    URLConnection urlConnection = url.openConnection();
    InputStream inputStream = urlConnection.getInputStream();
    BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
    FileOutputStream fileOutputStream = new FileOutputStream(file);

    byte[] buffer = new byte[1024];
    int bytesInBuffer = 0;
    while ((bytesInBuffer = bufferedInputStream.read(buffer)) != -1) {
        fileOutputStream.write(buffer, 0, bytesInBuffer);
    }

    fileOutputStream.close();
}

From source file:com.mondospider.android.lib.LibHTTP.java

public static String get(String url) {
    String ReturnHTML = "";
    //      Log.d("LibHTTP->get->url",url);
    try {/*from w ww . jav  a 2s  .co  m*/
        URLConnection urlConn = new URL(url).openConnection();
        InputStream is = urlConn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is, 16000);
        ByteArrayBuffer baf = new ByteArrayBuffer(50);
        int current = 0;
        while ((current = bis.read()) != -1) {
            baf.append((byte) current);
        }
        ReturnHTML = new String(baf.toByteArray());
    } catch (Exception e) {
        ReturnHTML = e.getMessage();
    }
    //        Log.d("LibHTTP->get->ReturnHTML", ReturnHTML);
    return ReturnHTML;
}

From source file:Main.java

public static byte[] loadImageDataFromWeb(String url) {

    byte[] imageData = null;
    try {/*from w w w  . j  av  a 2  s . com*/

        URLConnection connection = new URL(url).openConnection();
        InputStream stream = connection.getInputStream();
        //BufferedInputStream in=new BufferedInputStream(stream);//default 8k buffer
        BufferedInputStream in = new BufferedInputStream(stream, 10240);//YG: 10k=10240, 2x8k=16384
        ByteArrayOutputStream out = new ByteArrayOutputStream(10240);
        int read;
        byte[] b = new byte[4096];

        while ((read = in.read(b)) != -1) {
            out.write(b, 0, read);
        }

        out.flush();
        out.close();

        imageData = out.toByteArray();

    } catch (Exception e) {

        Log.w(TAG, "Exc=" + e);
        return null;
    }

    return imageData;
}

From source file:com.sagalasan.eveapi.engine.RequestEngine.java

public static byte[] xmlRequest(XmlRequest xmlRequest) {
    try {/*  w w w .  ja v  a  2s .c  om*/
        URL url = new URL(xmlRequest.buildUri());
        URLConnection connection = url.openConnection();
        InputStream inputStream = connection.getInputStream();
        return IOUtils.toByteArray(inputStream);
    } catch (IOException e) {
        logger.error(e.getMessage());
    }

    return null;
}

From source file:com.peer2gear.nutch.xquery.XQueryParseFilter.java

private static Content createContent(Configuration conf, String urlStr)
        throws FileNotFoundException, IOException {
    String contentType = "text/html";
    URL url = new URL(urlStr);
    URLConnection connection = url.openConnection();
    InputStream is = connection.getInputStream();
    byte bytes[] = IOUtils.toByteArray(is);
    Content content = new Content(urlStr, urlStr, bytes, contentType, new Metadata(), conf);
    return content;
}

From source file:Main.java

public static Bitmap getBitmapAndScale(String url, int requiredSize) {
    Bitmap bm = null;/*w ww.  j ava  2  s  .c  o m*/
    try {
        URL aURL = new URL(url);
        URLConnection conn = aURL.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();

        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, o);
        // The new size we want to scale to

        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < requiredSize || height_tmp / 2 < requiredSize)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }
        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        bm = BitmapFactory.decodeStream(is, null, o2);
        return bm;

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        return bm;

    }
}

From source file:com.jjtree.utilities.JServeletManager.java

public static JSONObject fetchFrom(HttpServletRequest request, String url) {
    JSONObject object = null;/* ww w.j  a v a2  s . c  o  m*/
    try {
        String serverName = request.getServerName();
        int portNumber = request.getServerPort();
        String contextPath = request.getContextPath();

        String accountUrl = "http://" + serverName + ":" + portNumber + contextPath + url;

        URL urldemo = new URL(accountUrl);
        URLConnection urlCon = urldemo.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            object = new JSONObject(inputLine);
        }
        in.close();
    } catch (Exception e) {
        System.out.println(e);
    }
    return object;
}

From source file:cz.mgn.mediservice.rest.Loader.java

protected static String load(String request) {
    String result = null;//from w ww  . ja v a2s  .  c  om

    try {
        URL url = new URL(SERVER_URL + "?" + request);

        URLConnection con = url.openConnection();
        result = IOUtils.toString(con.getInputStream(), "UTF-8");
    } catch (IOException ex) {
        Logger.getLogger(Loader.class.getName()).log(Level.SEVERE, null, ex);
    }

    return result;
}