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.buglabs.dragonfly.util.WSHelper.java

/**
 * @param url// www  .  j a v  a2  s  .c o m
 * @param inputStream
 * @return
 * @throws IOException
 */
protected static String post(URL url, InputStream inputStream) throws IOException {
    URLConnection connection = url.openConnection();
    connection.setDoInput(true);
    connection.setDoOutput(true);

    OutputStream stream = connection.getOutputStream();
    writeTo(stream, inputStream);
    stream.flush();
    stream.close();

    InputStream is = connection.getInputStream();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    String line, resp = new String("");
    while ((line = rd.readLine()) != null) {
        resp = resp + line + "\n";
    }
    rd.close();
    is.close();

    return resp;
}

From source file:net.pms.util.OpenSubtitle.java

public static String fetchSubs(String url, String outName) throws FileNotFoundException, IOException {
    if (!login()) {
        return "";
    }/*from  w w  w. j  a  v  a2s  .  c  o m*/
    if (StringUtils.isEmpty(outName)) {
        outName = subFile(String.valueOf(System.currentTimeMillis()));
    }
    File f = new File(outName);
    URL u = new URL(url);
    URLConnection connection = u.openConnection();
    connection.setDoInput(true);
    connection.setDoOutput(true);
    InputStream in = connection.getInputStream();
    OutputStream out;
    try (GZIPInputStream gzipInputStream = new GZIPInputStream(in)) {
        out = new FileOutputStream(f);
        byte[] buf = new byte[4096];
        int len;
        while ((len = gzipInputStream.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
    }
    out.close();
    if (!PMS.getConfiguration().isLiveSubtitlesKeep()) {
        int tmo = PMS.getConfiguration().getLiveSubtitlesTimeout();
        if (tmo <= 0) {
            PMS.get().addTempFile(f);
        } else {
            PMS.get().addTempFile(f, tmo);
        }
    }
    return f.getAbsolutePath();
}

From source file:de.schildbach.wallet.litecoin.ExchangeRatesProvider.java

private static Double getLycBtcRate() {
    try {/* www  . j a  v a 2 s  .c om*/
        final URL URL = new URL("http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=177");
        final URLConnection connection = URL.openConnection();
        connection.setConnectTimeout(TIMEOUT_MS);
        connection.setReadTimeout(TIMEOUT_MS);
        connection.connect();
        final StringBuilder content = new StringBuilder();

        Reader reader = null;
        try {
            reader = new InputStreamReader(new BufferedInputStream(connection.getInputStream(), 1024));
            IOUtils.copy(reader, content);

            final JSONObject head = new JSONObject(content.toString());
            final JSONObject o = head.getJSONObject("return").getJSONObject("markets").getJSONObject("LYC");
            final Double rate = o.getDouble("lasttradeprice");
            if (rate != null)
                return rate;
        } finally {
            if (reader != null)
                reader.close();
        }
    } catch (final IOException x) {
        x.printStackTrace();
    } catch (final JSONException x) {
        x.printStackTrace();
    }

    return null;
}

From source file:com.ibuildapp.romanblack.CataloguePlugin.utils.Utils.java

/**
 * Download URL data to String.//from www  . j  a v  a  2  s . com
 *
 * @param msgsUrl URL to download
 * @return data string
 */
private static String loadURLData(String msgsUrl) {
    try {
        URL url = new URL(msgsUrl);
        URLConnection conn = url.openConnection();
        InputStreamReader streamReader = new InputStreamReader(conn.getInputStream());

        BufferedReader br = new BufferedReader(streamReader);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = br.readLine()) != null) {
            sb.append(line);
            sb.append("\n");
        }
        br.close();
        String resp = sb.toString();

        return resp;
    } catch (IOException iOEx) {
        return "";
    }
}

From source file:com.nubits.nubot.utils.Utils.java

public static String getHTML(String url, boolean removeNonLatinChars) throws IOException {
    String line = "", all = "";
    URL myUrl = null;/*from   w  w w. j a v a2 s . co m*/
    BufferedReader br = null;
    try {
        myUrl = new URL(url);

        URLConnection con = myUrl.openConnection();
        con.setConnectTimeout(1000 * 8);
        con.setReadTimeout(1000 * 8);
        InputStream in = con.getInputStream();

        br = new BufferedReader(new InputStreamReader(in));

        while ((line = br.readLine()) != null) {
            all += line;
        }
    } finally {
        if (br != null) {
            br.close();
        }
    }

    if (removeNonLatinChars) {
        all = all.replaceAll("[^\\x00-\\x7F]", "");
    }
    return all;
}

From source file:de.schildbach.wallet.litecoin.ExchangeRatesProvider.java

private static Map<String, ExchangeRate> getBlockchainInfo() {
    try {/*from   w w w .  ja va2  s .co m*/
        final URL URL = new URL("https://blockchain.info/ticker");
        final URLConnection connection = URL.openConnection();
        connection.setConnectTimeout(TIMEOUT_MS);
        connection.setReadTimeout(TIMEOUT_MS);
        connection.connect();
        final StringBuilder content = new StringBuilder();

        Reader reader = null;
        try {
            reader = new InputStreamReader(new BufferedInputStream(connection.getInputStream(), 1024));
            IOUtils.copy(reader, content);

            final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>();
            final JSONObject head = new JSONObject(content.toString());
            for (final Iterator<String> i = head.keys(); i.hasNext();) {
                final String currencyCode = i.next();
                final JSONObject o = head.getJSONObject(currencyCode);
                final Double drate = o.getDouble("15m") * LycBtcRate;
                String rate = String.format("%.8f", drate);
                rate = rate.replace(",", ".");
                if (rate != null)
                    rates.put(currencyCode, new ExchangeRate(currencyCode, Utils.toNanoCoins(rate),
                            "cryptsy.com and " + URL.getHost()));
            }

            return rates;
        } finally {
            if (reader != null)
                reader.close();
        }
    } catch (final IOException x) {
        x.printStackTrace();
    } catch (final JSONException x) {
        x.printStackTrace();
    }

    return null;
}

From source file:net.sf.firemox.tools.Picture.java

/**
 * Download a file from the specified URL to the specified local file.
 * /* w ww  .j  a  v a  2  s  .c o  m*/
 * @param localFile
 *          is the new card's picture to try first
 * @param remoteFile
 *          is the URL where this picture will be downloaded in case of the
 *          specified card name has not been found locally.
 * @param listener
 *          the component waiting for this picture.
 * @since 0.83 Empty file are deleted to force file to be downloaded.
 */
public static synchronized void download(String localFile, URL remoteFile, MonitoredCheckContent listener) {
    BufferedOutputStream out = null;
    BufferedInputStream in = null;
    File toDownload = new File(localFile);
    if (toDownload.exists() && toDownload.length() == 0 && toDownload.canWrite()) {
        toDownload.delete();
    }
    if (!toDownload.exists() || (toDownload.length() == 0 && toDownload.canWrite())) {
        // the file has to be downloaded
        try {
            if ("file".equals(remoteFile.getProtocol())) {
                File localRemoteFile = MToolKit
                        .getFile(remoteFile.toString().substring(7).replaceAll("%20", " "), false);
                int contentLength = (int) localRemoteFile.length();
                Log.info("Copying from " + localRemoteFile.getAbsolutePath());
                LoaderConsole.beginTask(
                        LanguageManager.getString("downloading") + " " + localRemoteFile.getAbsolutePath() + "("
                                + FileUtils.byteCountToDisplaySize(contentLength) + ")");

                // Copy file
                in = new BufferedInputStream(new FileInputStream(localRemoteFile));
                byte[] buf = new byte[2048];
                int currentLength = 0;
                boolean succeed = false;
                for (int bufferLen = in.read(buf); bufferLen >= 0; bufferLen = in.read(buf)) {
                    if (!succeed) {
                        toDownload.getParentFile().mkdirs();
                        out = new BufferedOutputStream(new FileOutputStream(localFile));
                        succeed = true;
                    }
                    currentLength += bufferLen;
                    if (out != null) {
                        out.write(buf, 0, bufferLen);
                    }
                    if (listener != null) {
                        listener.updateProgress(contentLength, currentLength);
                    }
                }

                // Step 3: close streams
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(out);
                in = null;
                out = null;
                return;
            }

            // Testing mode?
            if (!MagicUIComponents.isUILoaded()) {
                return;
            }

            // Step 1: open streams
            final URLConnection connection = MToolKit.getHttpConnection(remoteFile);
            int contentLength = connection.getContentLength();
            in = new BufferedInputStream(connection.getInputStream());
            Log.info("Download from " + remoteFile + "(" + FileUtils.byteCountToDisplaySize(contentLength)
                    + ")");
            LoaderConsole.beginTask(LanguageManager.getString("downloading") + " " + remoteFile + "("
                    + FileUtils.byteCountToDisplaySize(contentLength) + ")");

            // Step 2: read and write until done
            byte[] buf = new byte[2048];
            int currentLength = 0;
            boolean succeed = false;
            for (int bufferLen = in.read(buf); bufferLen >= 0; bufferLen = in.read(buf)) {
                if (!succeed) {
                    toDownload.getParentFile().mkdirs();
                    out = new BufferedOutputStream(new FileOutputStream(localFile));
                    succeed = true;
                }
                currentLength += bufferLen;
                if (out != null) {
                    out.write(buf, 0, bufferLen);
                }
                if (listener != null) {
                    listener.updateProgress(contentLength, currentLength);
                }
            }

            // Step 3: close streams
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
            in = null;
            out = null;
            return;
        } catch (IOException e1) {
            if (MToolKit.getFile(localFile) != null) {
                MToolKit.getFile(localFile).delete();
            }
            if (remoteFile.getFile().equals(remoteFile.getFile().toLowerCase())) {
                Log.fatal("could not load picture " + localFile + " from URL " + remoteFile + ", "
                        + e1.getMessage());
            }
            String tmpRemote = remoteFile.toString().toLowerCase();
            try {
                download(localFile, new URL(tmpRemote), listener);
            } catch (MalformedURLException e) {
                Log.fatal("could not load picture " + localFile + " from URL " + tmpRemote + ", "
                        + e.getMessage());
            }
        }
    }
}

From source file:com.bjorsond.android.timeline.utilities.Utilities.java

public static File DownloadFromUrl(String imageURL, String fileName) { //this is the downloader method
    try {/*ww w. j  a v  a 2 s.  co  m*/
        URL url = new URL(imageURL); //you can write here any link
        File file = new File(fileName);
        System.out.println("THE FILENAME IS " + fileName);
        if (!file.exists()) {
            long startTime = System.currentTimeMillis();
            Log.d("ImageManager", "download begining");
            Log.d("ImageManager", "download url:" + url);
            Log.d("ImageManager", "downloaded file name:" + fileName);
            File downloadFolder = new File(Utilities.getFilePathFromURL(fileName));
            if (!downloadFolder.exists()) {
                downloadFolder.mkdirs();
            }

            /* Open a connection to that URL. */
            URLConnection ucon = url.openConnection();

            /*
            * Define InputStreams to read from the URLConnection.
            */
            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);

            /*
            * Read bytes to the Buffer until there is nothing more to read(-1).
            */
            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;
            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }

            /* Convert the Bytes read to a String. */
            FileOutputStream fos = new FileOutputStream(file);

            fos.write(baf.toByteArray());
            fos.close();
            Log.d("ImageManager",
                    "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
        } else {
            Log.d("ImageManager", "file exists!");
        }
        return file;

    } catch (IOException e) {
        Log.d("ImageManager", "Error: " + e);
        return null;
    }

}

From source file:net.roboconf.target.docker.internal.DockerUtils.java

/**
 * Downloads a remote file (supports Maven URLs).
 * <p>/*from  w  w  w.ja  va2  s  .  c o m*/
 * Please, refer to Pax URL's guide for more details about Maven URLs.
 * https://ops4j1.jira.com/wiki/display/paxurl/Mvn+Protocol
 * </p>
 *
 * @param url an URL
 * @param targetFile the file where it should be saved
 * @param mavenResolver the Maven resolver
 *
 * @throws IOException
 * @throws URISyntaxException
 */
public static void downloadRemotePackage(String url, File targetFile, MavenResolver mavenResolver)
        throws IOException, URISyntaxException {

    if (url.toLowerCase().startsWith("mvn:")) {
        if (mavenResolver == null)
            throw new IOException("Maven URLs are only resolved in Karaf at the moment.");

        File sourceFile = mavenResolver.resolve(url);
        Utils.copyStream(sourceFile, targetFile);

    } else {
        URL u = UriUtils.urlToUri(url).toURL();
        URLConnection uc = u.openConnection();
        InputStream in = null;
        try {
            in = new BufferedInputStream(uc.getInputStream());
            Utils.copyStream(in, targetFile);

        } finally {
            Utils.closeQuietly(in);
        }
    }
}

From source file:at.gv.egiz.pdfas.web.helper.RemotePDFFetcher.java

public static byte[] fetchPdfFile(String pdfURL) throws PdfAsWebException {
    URL url;//from w w w.j  a  va 2s  . co m
    String[] fetchInfos;
    try {
        fetchInfos = extractSensitiveInformationFromURL(pdfURL);
        url = new URL(fetchInfos[0]);
    } catch (MalformedURLException e) {
        logger.warn("Not a valid URL!", e);
        throw new PdfAsWebException("Not a valid URL!", e);
    } catch (IOException e) {
        logger.warn("Not a valid URL!", e);
        throw new PdfAsWebException("Not a valid URL!", e);
    }
    if (WebConfiguration.isProvidePdfURLinWhitelist(url.toExternalForm())) {
        if (url.getProtocol().equals("http") || url.getProtocol().equals("https")) {
            URLConnection uc = null;
            InputStream is = null;
            try {
                uc = url.openConnection();

                if (fetchInfos.length == 3) {
                    String userpass = fetchInfos[1] + ":" + fetchInfos[2];
                    String basicAuth = "Basic "
                            + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes("UTF-8"));
                    uc.setRequestProperty("Authorization", basicAuth);
                }

                is = uc.getInputStream();
                return StreamUtils.inputStreamToByteArray(is);
            } catch (Exception e) {
                logger.warn("Failed to fetch pdf document!", e);
                throw new PdfAsWebException("Failed to fetch pdf document!", e);
            } finally {
                IOUtils.closeQuietly(is);
            }
        } else {
            throw new PdfAsWebException(
                    "Failed to fetch pdf document protocol " + url.getProtocol() + " is not supported");
        }
    } else {
        throw new PdfAsWebException("Failed to fetch pdf document " + url.toExternalForm() + " is not allowed");
    }
}