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:Downloader.java

/**
 * Downloads the specified page to disk.
 * /*  w  w  w .  ja  v a2  s .co m*/
 * @param url The URL to retrieve
 * @param file The file to save the page to
 * @param listener The progress listener for this download
 * @throws java.io.IOException If there's an I/O error while downloading
 */
public static void downloadPage(final String url, final String file, final DownloadListener listener)
        throws IOException {

    final URLConnection urlConn = getConnection(url, "");
    final File myFile = new File(file);

    final FileOutputStream output = new FileOutputStream(myFile);
    final InputStream input = urlConn.getInputStream();
    final int length = urlConn.getContentLength();
    int current = 0;

    final byte[] buffer = new byte[512];
    int count;

    do {
        count = input.read(buffer);

        if (count > 0) {
            current += count;
            output.write(buffer, 0, count);

            if (listener != null) {
                listener.downloadProgress(100 * current / length);
            }
        }
    } while (count > 0);

    input.close();
    output.close();
}

From source file:net.dv8tion.discord.util.GoogleSearch.java

public static List<SearchResult> performSearch(String engineId, String terms, int requiredResultsCount) {
    try {/*w w w  . ja va 2s.  co m*/
        if (GOOGLE_API_KEY == null)
            throw new IllegalStateException(
                    "Google API Key is null, Cannot preform google search without a key! Set one in the settings!");
        if (engineId == null || engineId.isEmpty())
            throw new IllegalArgumentException("Google Custom Search Engine id cannot be null or empty!");

        LocalDateTime currentTime = LocalDateTime.now();
        if (currentTime.isAfter(dayStartTime.plusDays(1))) {
            dayStartTime = currentTime;
            currentGoogleUsage = 1;
        } else if (currentGoogleUsage >= 80) {
            throw new IllegalStateException("Google usage has reached the premature security cap of 80");
        }

        terms = terms.replace(" ", "%20");
        String searchUrl = String.format(GOOGLE_URL, engineId, GOOGLE_API_KEY, requiredResultsCount, terms);

        URL searchURL = new URL(searchUrl);
        URLConnection conn = searchURL.openConnection();
        currentGoogleUsage++;
        conn.setRequestProperty("User-Agent",
                "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:39.0) Gecko/20100101 " + randomName(10));
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        StringBuilder json = new StringBuilder();
        String line;
        while ((line = in.readLine()) != null) {
            json.append(line).append("\n");
        }
        in.close();

        JSONArray jsonResults = new JSONObject(json.toString()).getJSONArray("items");
        List<SearchResult> results = new LinkedList<>();
        for (int i = 0; i < jsonResults.length(); i++) {
            results.add(SearchResult.fromGoogle(jsonResults.getJSONObject(i)));
        }
        return results;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:net.sf.jabref.importer.fetcher.MedlineFetcher.java

/**
 * Fetch and parse an medline item from eutils.ncbi.nlm.nih.gov.
 *
 * @param id One or several ids, separated by ","
 *
 * @return Will return an empty list on error.
 *///from  w  w w .ja va2  s  .  c  o m
private static List<BibEntry> fetchMedline(String id, OutputPrinter status) {
    String baseUrl = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&retmode=xml&rettype=citation&id="
            + id;
    try {
        URL url = new URL(baseUrl);
        URLConnection data = url.openConnection();
        ParserResult result = new MedlineImporter()
                .importDatabase(new BufferedReader(new InputStreamReader(data.getInputStream())));
        if (result.hasWarnings()) {
            status.showMessage(result.getErrorMessage());
        }
        return result.getDatabase().getEntries();
    } catch (IOException e) {
        return new ArrayList<>();
    }
}

From source file:heigit.ors.routing.RoutingProfilesUpdater.java

public static String downloadFileContent(String url) throws Exception {
    URL website = new URL(url);
    URLConnection connection = website.openConnection();

    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

    StringBuilder response = new StringBuilder();
    String inputLine;/*from  w  w  w  .  j  ava  2  s  .  c  om*/

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

    in.close();

    return response.toString();
}

From source file:UrlHelper.java

public static void downloadFile(String adresse, File dest) {
    BufferedReader reader = null;
    FileOutputStream fos = null;//from   w  w  w .  j ava 2  s. c om
    InputStream in = null;
    try {

        // cration de la connection
        URL url = new URL(adresse);
        URLConnection conn = url.openConnection();
        String FileType = conn.getContentType();

        int FileLenght = conn.getContentLength();
        if (FileLenght == -1) {
            throw new IOException("Fichier non valide.");
        }

        // lecture de la rponse
        in = conn.getInputStream();
        reader = new BufferedReader(new InputStreamReader(in));
        if (dest == null) {
            String FileName = url.getFile();
            FileName = FileName.substring(FileName.lastIndexOf('/') + 1);
            dest = new File(FileName);
        }
        fos = new FileOutputStream(dest);
        byte[] buff = new byte[1024];
        int l = in.read(buff);
        while (l > 0) {
            fos.write(buff, 0, l);
            l = in.read(buff);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:eionet.cr.util.FileUtil.java

/**
 * Downloads the contents of the given URL into the given file and closes the file.
 *
 * @param urlString downloadable url/* www  . j a v a2s.co  m*/
 * @param toFile output file location
 * @throws IOException if input of output fails
 */
public static void downloadUrlToFile(final String urlString, final File toFile) throws IOException {

    URLConnection urlConnection = null;
    InputStream inputStream = null;
    try {
        URL url = new URL(urlString == null ? urlString : StringUtils.replace(urlString, " ", "%20"));
        urlConnection = url.openConnection();
        urlConnection.setRequestProperty("Connection", "close");
        inputStream = urlConnection.getInputStream();
        FileUtil.streamToFile(inputStream, toFile);
    } finally {
        IOUtils.closeQuietly(inputStream);
        URLUtil.disconnect(urlConnection);
    }
}

From source file:mashapeautoloader.MashapeAutoloader.java

/**
 * @param librayName//w w  w.j  a  va 2 s. c  o  m
 * @param methodName
 * @param arguments
 * @return JSONObject
 * @throws MalformedURLException
 * @throws IOException
 * 
 * Returns the json result of invoked method on called api
 */
public static JSONObject exec(String librayName, String methodName, String... arguments)
        throws MalformedURLException, IOException {
    // check for local api store
    if (MashapeAutoloader.apiStore == null)
        return null;

    // check for library existance
    if (!downloadLib(librayName))
        return null;

    Object libraryInstance;
    Method method;
    Object result;
    JSONObject trueResult;

    try {
        // instance .class library
        ClassLoaderExt loader = new ClassLoaderExt();

        String filePath = new File(apiStore + librayName + ".class").getAbsolutePath();
        String fileUrl = "file:" + filePath.replace("\\", "/");
        URL myUrl = new URL(fileUrl);
        URLConnection connection = myUrl.openConnection();
        InputStream input = connection.getInputStream();
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();

        int data = input.read();
        while (data != -1) {
            buffer.write(data);
            data = input.read();
        }
        input.close();

        byte[] classData = buffer.toByteArray();
        Class<?> libraryClass = loader.defineClassCallable(librayName, classData);

        // call constructor
        try {
            Constructor<?> constructor = libraryClass.getConstructor(String.class, String.class);
            libraryInstance = constructor.newInstance(publicKey, privateKey);
        } catch (NoSuchMethodException ex) {
            throw new RuntimeException(ex);
        } catch (SecurityException ex) {
            throw new RuntimeException(ex);
        } catch (InstantiationException ex) {
            throw new RuntimeException(ex);
        } catch (IllegalAccessException ex) {
            throw new RuntimeException(ex);
        } catch (IllegalArgumentException ex) {
            throw new RuntimeException(ex);
        } catch (InvocationTargetException ex) {
            throw new RuntimeException(ex);
        }

        // invoke method
        try {
            switch (arguments.length) {
            case 0:
                method = libraryClass.getMethod(methodName);
                result = method.invoke(libraryInstance);
                break;

            case 1:
                method = libraryClass.getMethod(methodName, String.class);
                result = method.invoke(libraryInstance, arguments[0]);
                break;

            case 2:
                method = libraryClass.getMethod(methodName, String.class);
                result = method.invoke(libraryInstance, arguments[0], arguments[1]);
                break;

            case 3:
                method = libraryClass.getMethod(methodName, String.class);
                result = method.invoke(libraryInstance, arguments[0], arguments[1], arguments[2]);
                break;

            case 4:
                method = libraryClass.getMethod(methodName, String.class);
                result = method.invoke(libraryInstance, arguments[0], arguments[1], arguments[2], arguments[3]);
                break;

            case 5:
                method = libraryClass.getMethod(methodName, String.class);
                result = method.invoke(libraryInstance, arguments[0], arguments[1], arguments[2], arguments[3],
                        arguments[4]);
                break;

            default:
                throw new NoSuchMethodException();
            }
        } catch (NoSuchMethodException ex) {
            throw new RuntimeException(ex);
        } catch (SecurityException ex) {
            throw new RuntimeException(ex);
        } catch (IllegalAccessException ex) {
            throw new RuntimeException(ex);
        } catch (IllegalArgumentException ex) {
            throw new RuntimeException(ex);
        } catch (InvocationTargetException ex) {
            throw new RuntimeException(ex);
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

    // convert result
    try {
        trueResult = (JSONObject) result;
    } catch (ClassCastException ex) {
        throw new RuntimeException(ex);
    }

    return trueResult;
}

From source file:mp3downloader.ZingSearch.java

private static String getSearchResult(String term, int searchType)
        throws UnsupportedEncodingException, MalformedURLException, IOException {
    String data = "{\"kw\": \"" + term + "\", \"t\": " + searchType + ", \"rc\": 50}";
    String jsonData = URLEncoder.encode(Base64.getEncoder().encodeToString(data.getBytes("UTF-8")), "UTF-8");
    String signature = hash_hmac(jsonData, privateKey);
    String urlString = searchUrl + "publicKey=" + publicKey + "&signature=" + signature + "&jsondata="
            + jsonData;/*ww  w  . j  a  va  2 s  .co m*/
    URL url = new URL(urlString);
    URLConnection urlConn = url.openConnection();
    urlConn.addRequestProperty("User-Agent",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36");
    InputStream is = urlConn.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
    String line;
    StringBuilder sb = new StringBuilder();
    while ((line = reader.readLine()) != null) {
        sb.append(line);
        sb.append("\n");
    }
    is.close();
    String content = sb.toString();
    return content;
}

From source file:mp3downloader.ZingSearch.java

private static String getDetailResult(String id, String searchType)
        throws UnsupportedEncodingException, MalformedURLException, IOException {
    String data = "{\"id\": \"" + id + "\", \"t\": \"" + searchType + "\"}";
    String jsonData = URLEncoder.encode(Base64.getEncoder().encodeToString(data.getBytes("UTF-8")), "UTF-8");
    String signature = hash_hmac(jsonData, privateKey);
    String urlString = detailUrl + "publicKey=" + publicKey + "&signature=" + signature + "&jsondata="
            + jsonData;/*  w ww.ja v a 2s .c o  m*/
    URL url = new URL(urlString);
    URLConnection urlConn = url.openConnection();
    urlConn.addRequestProperty("User-Agent",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36");
    InputStream is = urlConn.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
    String line;
    StringBuilder sb = new StringBuilder();
    while ((line = reader.readLine()) != null) {
        sb.append(line);
        sb.append("\n");
    }
    is.close();
    String content = sb.toString();
    return content;
}

From source file:apiPull.getItem.java

public static ArrayList numberOfIds() {
    // go to gw2 https://api.guildwars2.com/v2/items and get a list of items
    // return the arraylist, use to loop through implimentations

    ArrayList ids = new ArrayList();

    // Make a URL to the web page
    URL url = null;//  ww  w .  j  a va2  s .c  om
    try {
        url = new URL("https://api.guildwars2.com/v2/items");
    } catch (MalformedURLException ex) {
        Logger.getLogger(getItem.class.getName()).log(Level.SEVERE, null, ex);
    }

    // Get the input stream through URL Connection
    URLConnection con = null;
    try {
        con = url.openConnection();
    } catch (IOException ex) {
        Logger.getLogger(getItem.class.getName()).log(Level.SEVERE, null, ex);
    }
    InputStream is = null;
    try {
        is = con.getInputStream();
        // Once you have the Input Stream, it's just plain old Java IO stuff.
        // For this case, since you are interested in getting plain-text web page
        // I'll use a reader and output the text content to System.out.
        // For binary content, it's better to directly read the bytes from stream and write
        // to the target file.

    } catch (IOException ex) {
        Logger.getLogger(getItem.class.getName()).log(Level.SEVERE, null, ex);
    }

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

    String line = null;

    try {
        // read each line 
        while ((line = br.readLine()) != null) {

            Scanner scanner = new Scanner(line);
            scanner.useDelimiter(",");
            while (scanner.hasNext()) {
                ids.add(scanner.next());
            }
            scanner.close();
        }
    } catch (IOException ex) {
        Logger.getLogger(getItem.class.getName()).log(Level.SEVERE, null, ex);
    }

    return ids;
}