Example usage for java.net HttpURLConnection getInputStream

List of usage examples for java.net HttpURLConnection getInputStream

Introduction

In this page you can find the example usage for java.net HttpURLConnection 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 InputStream getRequest(String path) throws Exception {
    URL url = new URL(path);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setConnectTimeout(5000);//from w  w w.  j a v a  2s.c o  m
    if (conn.getResponseCode() == 200) {
        return conn.getInputStream();
    }
    return null;
}

From source file:Main.java

public static boolean downloadFile(File file, URL url) throws IOException {
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    HttpURLConnection conn = null;
    try {/*from   w  ww  .j a v a2 s.c  o m*/
        conn = (HttpURLConnection) url.openConnection();
        bis = new BufferedInputStream(conn.getInputStream());
        bos = new BufferedOutputStream(new FileOutputStream(file));

        int contentLength = conn.getContentLength();

        byte[] buffer = new byte[BUFFER_SIZE];
        int read = 0;
        int count = 0;
        while ((read = bis.read(buffer)) != -1) {
            bos.write(buffer, 0, read);
            count += read;
        }
        if (count < contentLength)
            return false;
        int responseCode = conn.getResponseCode();
        if (responseCode / 100 != 2) {
            // error
            return false;
        }
        // finished
        return true;
    } finally {
        try {
            bis.close();
            bos.close();
            conn.disconnect();
        } catch (Exception e) {
        }
    }
}

From source file:Main.java

public static String stringFromHttpGet(String urlString) {
    try {/*from   ww  w.  j  ava2s.  c o  m*/
        URL url = new URL(urlString);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        return response.toString();
    } catch (Exception e) {
        e.printStackTrace();
        logError(e.getMessage());
        return null;
    }
}

From source file:Main.java

@Nullable
private static InputStream getStreamFromUri(Uri selectedImageURI, Context theContext) throws IOException {
    switch (selectedImageURI.getScheme()) {
    case "content":
        return theContext.getContentResolver().openInputStream(selectedImageURI);

    case "file":
        return new FileInputStream(new File(URI.create(selectedImageURI.toString())));

    case "http":
        // Fall through
    case "https":
        final URL url = new URL(selectedImageURI.toString());
        final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);/*w  w w  .j a va 2s  . c  o  m*/
        connection.connect();
        return connection.getInputStream();

    default:
        Log.w(TAG, "getStreamFromUri(): unsupported Uri scheme: " + selectedImageURI.getScheme());
        return null;
    }
}

From source file:Main.java

private static String getResponseMessage(InputStream inputStream, HttpURLConnection connection)
        throws UnsupportedEncodingException, IOException {
    String responseMessage = null;
    StringBuffer sb = new StringBuffer();
    InputStream dis = connection.getInputStream();
    int chr;//from  www .  j a  va2  s.  c o  m
    while ((chr = dis.read()) != -1) {
        sb.append((char) chr);
    }
    if (sb != null) {
        responseMessage = sb.toString();
    }
    return responseMessage;
}

From source file:org.apache.nifi.minifi.integration.util.LogUtil.java

public static void verifyLogEntries(String expectedJsonFilename, Container container) throws Exception {
    List<ExpectedLogEntry> expectedLogEntries;
    try (InputStream inputStream = LogUtil.class.getClassLoader().getResourceAsStream(expectedJsonFilename)) {
        List<Map<String, Object>> expected = new ObjectMapper().readValue(inputStream, List.class);
        expectedLogEntries = expected.stream()
                .map(map -> new ExpectedLogEntry(Pattern.compile((String) map.get("pattern")),
                        (int) map.getOrDefault("occurrences", 1)))
                .collect(Collectors.toList());
    }/*  w w  w .j  a  v  a 2 s .  co  m*/
    DockerPort dockerPort = container.port(8000);
    URL url = new URL("http://" + dockerPort.getIp() + ":" + dockerPort.getExternalPort());
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    try (InputStream inputStream = urlConnection.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
        String line;
        for (ExpectedLogEntry expectedLogEntry : expectedLogEntries) {
            boolean satisfied = false;
            int occurrences = 0;
            while ((line = bufferedReader.readLine()) != null) {
                if (expectedLogEntry.pattern.matcher(line).find()) {
                    logger.info("Found expected: " + line);
                    if (++occurrences >= expectedLogEntry.numOccurrences) {
                        logger.info("Found target " + occurrences + " times");
                        satisfied = true;
                        break;
                    }
                }
            }
            if (!satisfied) {
                fail("End of log reached without " + expectedLogEntry.numOccurrences + " match(es) of "
                        + expectedLogEntry.pattern);
            }
        }
    } finally {
        urlConnection.disconnect();
    }
}

From source file:com.apps.android.viish.encyclolpedia.tools.ImageManager.java

private static void downloadAndSaveImageWithPrefix(Context context, String prefix, String fileName)
        throws IOException {
    String stringUrl = BASE_URL + prefix + fileName;
    URL url = new URL(stringUrl);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    InputStream is = urlConnection.getInputStream();
    BufferedInputStream bis = new BufferedInputStream(is);
    FileOutputStream fos = getFileOutputStream(context, fileName);

    ByteArrayBuffer baf = new ByteArrayBuffer(65535);
    int current = 0;
    while ((current = bis.read()) != -1) {
        baf.append((byte) current);
    }//from   www .j a  v  a  2s.c  o m
    fos.write(baf.toByteArray());
    fos.close();
    bis.close();
}

From source file:Main.java

public static Bitmap getBitmapFromURL(String urlStr) {
    Bitmap bitmap = null;/*from w  w w .j  ava  2s  .co m*/
    try {
        URL url = new URL(urlStr);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();

        InputStream input = connection.getInputStream();
        bitmap = BitmapFactory.decodeStream(input);
    } catch (IOException e) {
        Log.e(LOG_TAG, "cannot get the image from link :" + urlStr);
    }
    return bitmap;
}

From source file:Main.java

public static String accessToFlashAir(String uri) throws IOException {
    URL url = new URL(uri);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

    String result = null;//from www .  j  av  a 2s . com
    try {
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        result = inputStreamToString(in);
        in.close();
    } finally {
        urlConnection.disconnect();
    }

    return result;
}

From source file:Main.java

/**
 * download and return bitmap for the given url
 * DO not call this method from main thread
 * @param src/*from ww  w  .j a v a  2 s .  c o  m*/
 * @return
 */
public static Bitmap getBitmapFromURL(String imageUrl) {
    try {
        URL url = new URL(imageUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        if (myBitmap != null) {
            myBitmap = getResizedBitmap(myBitmap, 200, 200);
        }
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}