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:org.gwtwidgets.server.spring.test.BaseTest.java

protected String readResource(String resource) throws Exception {
    URL url = getClass().getClassLoader().getResource(resource);
    if (url == null)
        url = ClassLoader.getSystemResource(resource);
    if (url == null)
        url = ClassLoader.getSystemClassLoader().getResource(resource);
    URLConnection conn = url.openConnection();
    byte[] b = new byte[conn.getContentLength()];
    InputStream in = conn.getInputStream();
    in.read(b);//from w  ww.  j a  va  2  s.  c  o  m
    in.close();
    return new String(b, "UTF-8");
}

From source file:Hack.FBGraph.java

public String getFBGraph() {
    String graph = null;/*w  w  w .  ja  va  2 s . c o  m*/
    try {

        String g = "https://graph.facebook.com/me?" + accessToken;
        URL u = new URL(g);
        URLConnection c = u.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
        String inputLine;
        StringBuffer b = new StringBuffer();
        while ((inputLine = in.readLine()) != null)
            b.append(inputLine + "\n");
        in.close();
        graph = b.toString();
        System.out.println(graph);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("ERROR in getting FB graph data. " + e);
    }
    return graph;
}

From source file:com.enonic.cms.core.http.HTTPService.java

private BufferedReader setUpReader(String encoding, URLConnection urlConn) throws IOException {
    InputStream in = urlConn.getInputStream();
    BufferedReader reader;//from  w  w  w  . j av a2 s.  c o m
    if (encoding == null) {
        reader = new BufferedReader(new InputStreamReader(in, "utf8"));
    } else {
        reader = new BufferedReader(new InputStreamReader(in, encoding));
    }
    return reader;
}

From source file:de.anycook.upload.UserUploader.java

private byte[] getURLData(URL url) {
    try {//w  w  w .  ja  v a 2s . co m
        URLConnection c = url.openConnection();
        InputStream in = new BufferedInputStream(c.getInputStream());
        int contentLength = c.getContentLength();
        byte[] data = new byte[contentLength];
        int bytesRead = 0;
        int offset = 0;
        while (offset < contentLength) {
            bytesRead = in.read(data, offset, data.length - offset);
            if (bytesRead == -1)
                break;
            offset += bytesRead;
        }

        in.close();

        return data;

    } catch (IOException e) {
        logger.error("failed to save FBImage", e);
    }

    return new byte[0];
}

From source file:com.seedboxer.seedboxer.sources.processors.QueueProcessor.java

private String downloadFile(URL url, String path) throws IOException {
    URLConnection conn = url.openConnection();
    InputStream in = conn.getInputStream();
    String disposition = conn.getHeaderField("Content-Disposition");
    String fileNameProperty = "filename=\"";
    String fileName = disposition.substring(disposition.indexOf(fileNameProperty),
            disposition.lastIndexOf("\""));
    fileName = fileName.substring(fileNameProperty.length(), fileName.length());
    path += File.separator + fileName;
    File file = new File(path);
    OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
    byte[] bytes = new byte[1024];
    int read;// w  ww.  j av a  2  s  .  co m
    while ((read = in.read(bytes)) != -1) {
        out.write(bytes, 0, read);
    }
    in.close();
    out.close();
    file.setReadable(true, false);
    file.setWritable(true, false);
    return fileName;
}

From source file:de.catma.document.source.contenthandler.DefaultProtocolHandler.java

private void handle(URI sourceDocURI) throws IOException {

    final String sourceDocURL = sourceDocURI.toURL().toString();
    final String sourceURIPath = sourceDocURI.getPath();

    SourceDocumentHandler sourceDocumentHandler = new SourceDocumentHandler();

    URLConnection urlConnection = new URL(sourceDocURL).openConnection();

    InputStream is = urlConnection.getInputStream();
    try {/*w  w  w  . j a va  2s  . co m*/
        this.byteContent = IOUtils.toByteArray(is);
        if (this.mimeType == null) {
            this.mimeType = sourceDocumentHandler.getMimeType(sourceURIPath, urlConnection,
                    FileType.TEXT.getMimeType());
        }

        this.encoding = Charset.defaultCharset().name();

        if (this.mimeType.equals(FileType.TEXT.getMimeType())
                || (this.mimeType.equals(FileType.HTML.getMimeType()))) {
            this.encoding = sourceDocumentHandler.getEncoding(urlConnection, byteContent,
                    Charset.defaultCharset().name());
        }
    } finally {
        is.close();
    }

}

From source file:org.nhnnext.android.androidnaming.ImageDownload.java

public void copy_img(String url, String save_name) {

    File img_cache_path;/*from   w  w  w . j  a  va2 s . c  o  m*/

    img_cache_path = new File(HomeView.FILES_DIR + save_name);
    if (!img_cache_path.exists()) {

        // ************************************
        try {
            URL Url = new URL(url);
            URLConnection conn = Url.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();

            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(50);

            int current = 0;

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

            FileOutputStream fos = context.openFileOutput(save_name, 0);
            fos.write(baf.toByteArray());
            fos.close();
        } catch (IOException e) {

        }

    }

}

From source file:org.nhnnext.android.day3.Img_Downloader.java

public void copy_img(String url, String save_name) {

    File img_cache_path;/*  w ww.j av  a2  s.  c om*/

    img_cache_path = new File(MainActivity.FILES_DIR + save_name);
    if (!img_cache_path.exists()) {

        // ************************************
        try {
            URL Url = new URL(url);
            URLConnection conn = Url.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();

            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(50);

            int current = 0;

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

            FileOutputStream fos = context.openFileOutput(save_name, 0);
            fos.write(baf.toByteArray());
            fos.close();
        } catch (IOException e) {

        }

    }

}

From source file:org.nhnnext.android.day5_simple.ImageDownloader.java

public void copy_img(String url, String save_name) {

    File img_cache_path;//  w ww . j  a  v  a2  s.c o  m

    img_cache_path = new File(NextgramController.FILES_DIR + save_name);
    if (!img_cache_path.exists()) {

        // ************************************
        try {
            URL Url = new URL(url);
            URLConnection conn = Url.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();

            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(50);

            int current = 0;

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

            FileOutputStream fos = context.openFileOutput(save_name, 0);
            fos.write(baf.toByteArray());
            fos.close();
        } catch (IOException e) {

        }

    }

}

From source file:org.nhnnext.android.basic.ImageDownload.java

public void copy_img(String url, String save_name) {

    File img_cache_path;/*from w  w w.  j a v a 2s.co  m*/

    img_cache_path = new File(pref.getString(context.getString(R.string.files_directory), "") + save_name);
    if (!img_cache_path.exists()) {

        // ************************************
        try {
            URL Url = new URL(url);
            URLConnection conn = Url.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();

            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(50);

            int current = 0;

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

            FileOutputStream fos = context.openFileOutput(save_name, 0);
            fos.write(baf.toByteArray());
            fos.close();
        } catch (IOException e) {

        }

    }

}