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.omertron.thetvdbapi.tools.WebBrowser.java

/**
 * Request the web page at the specified URL
 *
 * @param url//  ww  w  .j av a 2s.c o m
 * @throws IOException
 */
public static String request(URL url) throws IOException {
    StringBuilder content = new StringBuilder();

    BufferedReader in = null;
    URLConnection cnx = null;
    InputStreamReader isr = null;
    GZIPInputStream zis = null;

    try {
        cnx = openProxiedConnection(url);

        sendHeader(cnx);
        readHeader(cnx);

        // Check the content encoding of the connection. Null content encoding is standard HTTP
        if (cnx.getContentEncoding() == null) {
            //in = new BufferedReader(new InputStreamReader(cnx.getInputStream(), getCharset(cnx)));
            isr = new InputStreamReader(cnx.getInputStream(), "UTF-8");
        } else if (cnx.getContentEncoding().equalsIgnoreCase("gzip")) {
            zis = new GZIPInputStream(cnx.getInputStream());
            isr = new InputStreamReader(zis, "UTF-8");
        } else {
            return "";
        }

        in = new BufferedReader(isr);

        String line;
        while ((line = in.readLine()) != null) {
            content.append(line);
        }
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        if (isr != null) {
            try {
                isr.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        if (zis != null) {
            try {
                zis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        if (cnx instanceof HttpURLConnection) {
            ((HttpURLConnection) cnx).disconnect();
        }
    }
    return content.toString();
}

From source file:com.ibuildapp.romanblack.FanWallPlugin.data.Statics.java

/**
 * download file url and save it/*from  www.  jav  a  2 s . c o  m*/
 *
 * @param url
 */
public static String downloadFile(String url) {
    int BYTE_ARRAY_SIZE = 1024;
    int CONNECTION_TIMEOUT = 30000;
    int READ_TIMEOUT = 30000;

    // downloading cover image and saving it into file
    try {
        URL imageUrl = new URL(URLDecoder.decode(url));
        URLConnection conn = imageUrl.openConnection();
        conn.setConnectTimeout(CONNECTION_TIMEOUT);
        conn.setReadTimeout(READ_TIMEOUT);
        BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());

        File resFile = new File(cachePath + File.separator + Utils.md5(url));
        if (!resFile.exists()) {
            resFile.createNewFile();
        }

        FileOutputStream fos = new FileOutputStream(resFile);
        int current = 0;
        byte[] buf = new byte[BYTE_ARRAY_SIZE];
        Arrays.fill(buf, (byte) 0);
        while ((current = bis.read(buf, 0, BYTE_ARRAY_SIZE)) != -1) {
            fos.write(buf, 0, current);
            Arrays.fill(buf, (byte) 0);
        }

        bis.close();
        fos.flush();
        fos.close();
        Log.d("", "");
        return resFile.getAbsolutePath();
    } catch (SocketTimeoutException e) {
        return null;
    } catch (IllegalArgumentException e) {
        return null;
    } catch (Exception e) {
        return null;
    }
}

From source file:com.waku.mmdataextract.ComprehensiveSearch.java

public static void saveImage(String imgSrc, String toFileName) {
    String toFile = "output/images/" + toFileName;
    if (new File(toFile).exists()) {
        logger.info("File already saved ->" + toFile);
        return;//from w  ww .  j  a  v a  2s.co  m
    }
    URL u = null;
    URLConnection uc = null;
    InputStream raw = null;
    InputStream in = null;
    FileOutputStream out = null;
    try {
        int endIndex = imgSrc.lastIndexOf("/") + 1;
        String encodeFileName = URLEncoder.encode(imgSrc.substring(endIndex), "UTF-8").replaceAll("[+]", "%20");
        u = new URL("http://shouji.gd.chinamobile.com" + imgSrc.substring(0, endIndex) + encodeFileName);
        uc = u.openConnection();
        String contentType = uc.getContentType();
        int contentLength = uc.getContentLength();
        if (contentType.startsWith("text/") || contentLength == -1) {
            logger.error("This is not a binary file. -> " + imgSrc);
        }
        raw = uc.getInputStream();
        in = new BufferedInputStream(raw);
        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;
        }
        if (offset != contentLength) {
            logger.error("Only read " + offset + " bytes; Expected " + contentLength + " bytes");
        }
        out = new FileOutputStream(toFile);
        out.write(data);
        out.flush();
        logger.info("Saved file " + u.toString() + " to " + toFile);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (Exception e) {
        }
        try {
            out.close();
        } catch (Exception e) {
        }
    }
}

From source file:it.intecs.pisa.toolbox.util.URLReader.java

public static Object getURLContent(String url) throws Exception {
    HttpMethod method;/*from w w w  .  j a  va2  s .c  o  m*/
    try {
        ToolboxConfiguration toolboxConfiguration;
        toolboxConfiguration = ToolboxConfiguration.getInstance();

        String host = "http://services.eoportal.org"; //toolboxConfiguration.getConfigurationValue(ToolboxConfiguration.SSE_PORTAL);
        String fullUrl;

        URLConnection conn;
        URL connUrl;

        if (url.startsWith("http://"))
            fullUrl = url;
        else
            fullUrl = host + url;

        connUrl = new URL(fullUrl);
        conn = connUrl.openConnection();

        Reader in = new InputStreamReader(conn.getInputStream());
        StringBuffer out = new StringBuffer();
        char[] buffer = new char[1024];
        for (int count = in.read(buffer); count >= 0; count = in.read(buffer)) {
            out.append(buffer, 0, count);
        }
        return out.toString();
    } catch (Exception e) {
        e.printStackTrace(System.out);
        return null;
    }
}

From source file:com.vmware.thinapp.common.util.AfUtil.java

/**
 * Attempt to download the icon specified by the given URL.  If the resource at the URL
 * has a content type of image/*, the binary data for this resource will be downloaded.
 *
 * @param iconUrlStr URL of the image resource to access
 * @return the binary data and content type of the image resource at the given URL, null
 * if the URL is invalid, the resource does not have a content type starting with image/, or
 * on some other failure./*from w  w  w  . j  av a 2  s  .  co  m*/
 */
public static final @Nullable IconInfo getIconInfo(String iconUrlStr) {
    if (!StringUtils.hasLength(iconUrlStr)) {
        log.debug("No icon url exists.");
        return null;
    }
    URL iconUrl = null;
    try {
        // Need to encode any invalid characters before creating the URL object
        iconUrl = new URL(UriUtils.encodeHttpUrl(iconUrlStr, "UTF-8"));
    } catch (MalformedURLException ex) {
        log.debug("Malformed icon URL string: {}", iconUrlStr, ex);
        return null;
    } catch (UnsupportedEncodingException ex) {
        log.debug("Unable to encode icon URL string: {}", iconUrlStr, ex);
        return null;
    }

    // Open a connection with the given URL
    final URLConnection conn;
    final InputStream inputStream;
    try {
        conn = iconUrl.openConnection();
        inputStream = conn.getInputStream();
    } catch (IOException ex) {
        log.debug("Unable to open connection to URL: {}", iconUrlStr, ex);
        return null;
    }

    String contentType = conn.getContentType();
    int sizeBytes = conn.getContentLength();

    try {
        // Make sure the resource has an appropriate content type
        if (!conn.getContentType().startsWith("image/")) {
            log.debug("Resource at URL {} does not have a content type of image/*.", iconUrlStr);
            return null;
            // Make sure the resource is not too large
        } else if (sizeBytes > MAX_ICON_SIZE_BYTES) {
            log.debug("Image resource at URL {} is too large: {}", iconUrlStr, sizeBytes);
            return null;
        } else {
            // Convert the resource to a byte array
            byte[] iconBytes = ByteStreams.toByteArray(new InputSupplier<InputStream>() {
                @Override
                public InputStream getInput() throws IOException {
                    return inputStream;
                }
            });
            return new IconInfo(iconBytes, contentType);
        }
    } catch (IOException e) {
        log.debug("Error reading resource data.", e);
        return null;
    } finally {
        Closeables.closeQuietly(inputStream);
    }
}

From source file:edu.ucsb.nceas.metacat.util.RequestUtil.java

public static String get(String urlString, Hashtable<String, String[]> params) throws MetacatUtilException {
    try {/*from  w  w w .j  av  a 2s . c om*/
        URL url = new URL(urlString);
        URLConnection urlConn = url.openConnection();

        urlConn.setDoOutput(true);

        PrintWriter pw = new PrintWriter(urlConn.getOutputStream());
        String queryString = paramsToQuery(params);
        logMetacat.debug("Sending get request: " + urlString + "?" + queryString);
        pw.print(queryString);
        pw.close();

        // get the input from the request
        BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));

        StringBuffer sb = new StringBuffer();
        String line;
        while ((line = in.readLine()) != null) {
            sb.append(line);
        }
        in.close();

        return sb.toString();
    } catch (MalformedURLException mue) {
        throw new MetacatUtilException("URL error when contacting: " + urlString + " : " + mue.getMessage());
    } catch (IOException ioe) {
        throw new MetacatUtilException("I/O error when contacting: " + urlString + " : " + ioe.getMessage());
    }
}

From source file:org.arasthel.almeribus.utils.LoadFromWeb.java

public static ResultTiempo calcularTiempo(Context context, int parada, int linea) {
    ResultTiempo result = new ResultTiempo();
    if (!isConnectionEnabled(context)) {
        Log.d("AlmeriBus", "No hay conexin");
        result.setTiempo(ERROR_IO);//from w  w w.j  ava  2 s .  c om
    }
    try {
        loadCookie();
        URL url = new URL(QUERY_ADDRESS_TIEMPO_PARADA + linea + "/" + parada + "/" + "3B5579C8FFD6");
        URLConnection connection = url.openConnection();
        connection.setConnectTimeout(10000);
        connection.setReadTimeout(15000);
        connection.setRequestProperty("REFERER", "http://m.surbus.com/tiempo-espera/");
        connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
        connection.setRequestProperty("X-Requested-With", "XMLHttpRequest");
        connection.setRequestProperty("Cookie", "ASP.NET_SessionId=" + cookie);
        Scanner scan = new Scanner(connection.getInputStream());
        StringBuilder strBuilder = new StringBuilder();
        while (scan.hasNextLine()) {
            strBuilder.append(scan.nextLine());
        }
        scan.close();
        Log.d("Almeribus", strBuilder.toString());
        JSONObject json = new JSONObject(strBuilder.toString());
        boolean isSuccessful = json.getBoolean("success");
        int type = json.getInt("waitTimeType");
        if (isSuccessful && type > 0) {
            int time = json.getInt("waitTime");
            if (time == Integer.MAX_VALUE) {
                time = NO_DATOS;
            }
            if (time <= 0) {
                time = 0;
            }
            result.setTiempo(time);
            result.setTiempoTexto(json.getString("waitTimeString"));
        } else {
            result.setTiempo(NO_DATOS);
        }
    } catch (Exception e) {
        Log.d("Almeribus", e.toString());
        result.setTiempo(ERROR_IO);
        return result;
    }
    return result;
}

From source file:com.occamlab.te.parsers.ImageParser.java

public static Document parse(URLConnection uc, Element instruction, PrintWriter logger) {
    Document doc = null;/* w w  w  .  j av a 2 s . c  om*/
    InputStream is = null;
    try {
        is = uc.getInputStream();
        doc = parse(is, instruction, logger);
    } catch (Exception e) {
        String msg = String.format("Failed to parse %s resource from %s \n %s", uc.getContentType(),
                uc.getURL(), e.getMessage());
        jlogger.warning(msg);
    } finally {
        if (null != is) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
    }
    return doc;
}

From source file:com.comcast.cdn.traffic_control.traffic_monitor.util.Fetcher.java

public static File downloadFile(final String url) throws IOException {
    InputStream in = null;//from  w  w  w .  jav  a 2s.  com
    OutputStream out = null;
    try {
        LOGGER.info("downloadFile: " + url);
        final URL u = new URL(url);
        final URLConnection urlc = u.openConnection();
        if (urlc instanceof HttpsURLConnection) {
            final HttpsURLConnection http = (HttpsURLConnection) urlc;
            http.setInstanceFollowRedirects(false);
            http.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(final String arg0, final SSLSession arg1) {
                    return true;
                }
            });
            http.setRequestMethod(GET_STR);
            http.setAllowUserInteraction(true);
        }
        in = urlc.getInputStream();//new GZIPInputStream(dbURL.openStream());
        //      if(sourceCompressed) { in = new GZIPInputStream(in); }

        final File outputFile = File.createTempFile(tmpPrefix, tmpSuffix);
        out = new FileOutputStream(outputFile);

        IOUtils.copy(in, out);
        return outputFile;
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
}

From source file:io.starter.datamodel.Sys.java

/**
 * get cached URL text//  ww w . j  a  v a2s.  c om
 * 
 * @param url
 * @return
 * @throws Exception
 */
public static String getText(String url) throws Exception {
    Object o = System.getProperty(url);
    if (o != null) {
        // Logger.log("System.sendEmail text cache hit for: " + url);
        return o.toString();
    } else {
        Logger.log("System.sendEmail text cache MISS for: " + url);
    }
    String oldurl = url;
    if (url.indexOf("?") > -1) {
        url += "&cachebust=" + System.currentTimeMillis();
    } else {
        url += "?cachebust=" + System.currentTimeMillis();
    }

    URL website = new URL(url);

    URLConnection connection = website.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

    StringBuilder response = new StringBuilder();
    String inputLine;

    while ((inputLine = in.readLine()) != null)
        response.append(inputLine);

    in.close();

    String strz = response.toString();
    System.setProperty(oldurl, strz);
    return strz;
}