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.bdaum.zoom.net.core.internal.Activator.java

public static InputStream openStream(String url, int timeout)
        throws MalformedURLException, IOException, HttpException {
    URL u = new URL(url);
    URLConnection con = u.openConnection();
    con.setConnectTimeout(timeout);//from   w  w  w  .  j a va  2 s.c o m
    con.setReadTimeout(timeout);
    try {
        return new BufferedInputStream(con.getInputStream());
    } catch (IOException e) {
        int responseCode = -1;
        if (con instanceof HttpURLConnection)
            responseCode = ((HttpURLConnection) con).getResponseCode();
        else if (con instanceof HttpsURLConnection)
            responseCode = ((HttpsURLConnection) con).getResponseCode();
        if (responseCode >= 0 && responseCode != HttpStatus.SC_OK)
            throw new HttpException(getStatusText(responseCode), e);
        throw e;
    }
}

From source file:AIR.Common.Web.FileFtpHandler.java

public static InputStream openStream(URI requestUri) throws FtpResourceException {
    try {/* w  w w .  j  av  a 2s . c  o  m*/
        URL url = requestUri.toURL();
        URLConnection urlc = url.openConnection();
        return urlc.getInputStream();
    } catch (IOException exp) {
        throw new FtpResourceException(exp);
    }
}

From source file:controlador.Red.java

/**
 * Obtencion de un JTree mapeado pasandole una URL de un server FTP.
 * @param url URL formateada de la siguiente manera: protocolo://user:pwd@ip:puerto".
 * @return JTree formateado para hacer un set directamente.
 *///from  ww w.j  a va2 s. co  m
public static JTree setArbolFTP(URL url) {
    try {
        URLConnection conn = url.openConnection();
        InputStream is = conn.getInputStream();

        JTree tree = new Mapeador().mapearServer(is);

        is.close();
        return tree;
    } catch (IOException ex) {
        System.out.println("Problema al hacer set del ArbolFTP: " + ex.getLocalizedMessage());
    }

    return null;
}

From source file:Main.java

public static String ParserHtml(String url) {
    String html = "";
    BufferedReader in = null;/*from w w w  . j a v  a 2s .com*/
    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;
}

From source file:$.FileUtils.java

/**
     * Read file of URL into file.//ww w  .j a va2  s  .c  o m
     * @param url URL where the input file is located
     * @return Result file
     */
    public static File urlToFile(URL url, String ext) throws IOException {
        File fOut = null;

        fOut = getTmpFile("fromurl", "." + ext);

        URLConnection uc = url.openConnection();
        logger.info("ContentType: " + uc.getContentType());
        InputStream in = uc.getInputStream();
        org.apache.commons.io.FileUtils.copyInputStreamToFile(in, fOut);
        logger.info("File of length " + fOut.length() + " created from URL " + url.toString());

        in.close();

        return fOut;
    }

From source file:Main.java

private static String consume(URLConnection connection) throws IOException {
    String encoding = getEncoding(connection);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    InputStream in = connection.getInputStream();
    try {// w w w  . j  av a 2s  .co m
        in = connection.getInputStream();
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = in.read(buffer)) > 0) {
            out.write(buffer, 0, bytesRead);
        }
    } finally {
        try {
            in.close();
        } catch (IOException ioe) {
            // continue
        }
    }
    try {
        return new String(out.toByteArray(), encoding);
    } catch (UnsupportedEncodingException uee) {
        try {
            return new String(out.toByteArray(), "UTF-8");
        } catch (UnsupportedEncodingException uee2) {
            // can't happen
            throw new IllegalStateException(uee2);
        }
    }
}

From source file:org.liberty.android.fantastischmemo.downloader.DownloaderUtils.java

public static File downloadFile(String url, String savedPath) throws IOException {
    File outFile = new File(savedPath);
    OutputStream out;/*from  ww w  . ja v a  2  s.  c om*/

    // Delete and backup if the file exists
    AMUtil.deleteFileWithBackup(savedPath);

    // Create the dir if necessary
    File parentDir = outFile.getParentFile();
    parentDir.mkdirs();

    outFile.createNewFile();
    out = new FileOutputStream(outFile);

    Log.i(TAG, "URL to download is: " + url);
    URL myURL = new URL(url);
    URLConnection ucon = myURL.openConnection();
    byte[] buf = new byte[8192];

    InputStream is = ucon.getInputStream();
    BufferedInputStream bis = new BufferedInputStream(is, 8192);
    int len = 0;
    while ((len = bis.read(buf)) != -1) {
        out.write(buf, 0, len);
    }
    out.close();
    is.close();
    return outFile;
}

From source file:cc.vidr.datum.util.FreebaseUtils.java

/**
 * Return the list of JSON objects returned by the API for the given query.
 * /*from w  ww .j a  v a 2  s  .  c om*/
 * @param path  the API service path
 * @return      the list of JSON objects
 * @throws      IOException if there was an error communicating with the
 *              server
 * @throws      ParseException if the server returns malformed JSON
 */
@SuppressWarnings("unchecked")
public static List<JSONObject> getResultList(String path) throws IOException, ParseException {
    URLConnection connection = new URL(endpoint + path).openConnection();
    connection.connect();
    InputStream stream = connection.getInputStream();
    Reader reader = new InputStreamReader(stream);
    JSONObject o = (JSONObject) JSONValue.parse(reader);
    return (List<JSONObject>) o.get("result");
}

From source file:com.microsoft.azure.engagement.ws.VideoParser.java

/**
 * Method that returns the result object
 *
 * @param urlString The url to parse/*from w  ww .  j  a  v a 2s  .c  o  m*/
 * @return The result object
 */
private static final JSONObject getJsonObject(String urlString) {
    Log.d(VideoParser.TAG, "Parse URL: " + urlString);

    InputStream inputStream = null;

    try {
        final URL url = new URL(urlString);
        final URLConnection urlConnection = url.openConnection();

        inputStream = new BufferedInputStream(urlConnection.getInputStream());
        final BufferedReader reader = new BufferedReader(
                new InputStreamReader(urlConnection.getInputStream(), "utf-8"), 8);
        final StringBuilder sb = new StringBuilder();

        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        final String json = sb.toString();
        return new JSONObject(json);
    } catch (Exception e) {
        Log.w(VideoParser.TAG, "Failed to parse the json for media list", e);
        return null;
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                Log.w(VideoParser.TAG, "JSON feed closed", e);
            }
        }
    }

}

From source file:javarestart.Utils.java

private static String getText(final URL url) throws IOException {
    final URLConnection connection = url.openConnection();
    try (LineNumberReader in = new LineNumberReader(
            new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")))) {

        final StringBuilder response = new StringBuilder();
        String inputLine;//from   w w  w  . ja v a2s  . c  o m
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }

        return response.toString();
    }
}