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:de.unigoettingen.sub.commons.util.stream.StreamUtils.java

/************************************************************************************
 * get {@link InputStream} from given URL using a basis path and proxy informations
 * //from   ww w  .j  a  v  a  2  s  .c om
 * @param url the url from where to get the {@link InputStream}
 * @param basepath the basispath
 * @param httpproxyhost the host for proxy
 * @param httpproxyport the port for proxy
 * @param httpproxyusername the username for the proxy
 * @param httpproxypassword the password for the proxy
 * @return {@link InputStream} for url
 * @throws IOException
 ************************************************************************************/
public static InputStream getInputStreamFromUrl(URL url, String basepath, String httpproxyhost,
        String httpproxyport, String httpproxyusername, String httpproxypassword) throws IOException {
    InputStream inStream = null;

    if (url.getProtocol().equalsIgnoreCase("http")) {
        if (httpproxyhost != null) {
            Properties properties = System.getProperties();
            properties.put("http.proxyHost", httpproxyhost);
            if (httpproxyport != null) {
                properties.put("http.proxyPort", httpproxyport);
            } else {
                properties.put("http.proxyPort", "80");
            }
        }
        URLConnection con = url.openConnection();
        if (httpproxyusername != null) {
            String login = httpproxyusername + ":" + httpproxypassword;
            String encodedLogin = new String(Base64.encodeBase64(login.getBytes()));
            con.setRequestProperty("Proxy-Authorization", "Basic " + encodedLogin);
        }
        inStream = con.getInputStream();
    } else if (url.getProtocol().equalsIgnoreCase("file")) {
        int size = url.openConnection().getContentLength();
        Integer maxFileLength = ContentServerConfiguration.getInstance().getMaxFileLength();

        if (maxFileLength != 0 && size > maxFileLength) {
            // System.out.println("File " + url.getFile() + " is too large (" + size + "/" + maxFileLength + ")");
            return getInputStreamFromUrl(new URL(ContentServerConfiguration.getInstance().getErrorFile()));
        }
        String filepath = url.getFile();

        filepath = URLDecoder.decode(filepath, System.getProperty("file.encoding"));

        File f = new File(filepath);
        if (!f.isFile()) {
            // try for a file with different suffix case
            int suffixIndex = filepath.lastIndexOf('.');
            f = new File(filepath.substring(0, suffixIndex) + filepath.substring(suffixIndex).toLowerCase());
            if (!f.isFile()) {
                f = new File(
                        filepath.substring(0, suffixIndex) + filepath.substring(suffixIndex).toUpperCase());
            }
            // search all files in this directory for this case-insensitive name
            if (!f.isFile()) {
                File[] files = f.getParentFile().listFiles();
                if (files != null) {
                    for (File file : files) {
                        if (file.getName().compareToIgnoreCase(f.getName()) == 0) {
                            f = file;
                            break;
                        }
                    }
                }
            }
        }
        inStream = new FileInputStream(f);

    } else if (url.getProtocol().length() == 0) {
        String filepath = url.getFile();
        // we just have the relative path, need to find the absolute path
        String path = basepath + filepath;

        // call this method again
        URL completeurl = new URL(path);
        inStream = getInputStreamFromUrl(completeurl);
    }

    return inStream;
}

From source file:com.linkedin.pinot.controller.helix.ControllerTest.java

public static String sendPostRequest(String urlString, String payload)
        throws UnsupportedEncodingException, IOException, JSONException {
    LOGGER.info("Sending POST to " + urlString + " with payload " + payload);
    final long start = System.currentTimeMillis();
    final URL url = new URL(urlString);
    final URLConnection conn = url.openConnection();
    conn.setDoOutput(true);//from  ww  w  . j a  va2s.c  o m
    final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));

    writer.write(payload, 0, payload.length());
    writer.flush();
    final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

    final StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }

    final long stop = System.currentTimeMillis();

    LOGGER.info(" Time take for Request : " + payload + " in ms:" + (stop - start));

    return sb.toString();
}

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

public static String fetchContent(final String stateUrl, final String ipStr, final int port, final int timeout)
        throws IOException {
    final URLConnection conn = new URL(stateUrl)
            .openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ipStr, port)));
    if (timeout != 0) {
        conn.setConnectTimeout(timeout);
        conn.setReadTimeout(timeout);/*  w  w w .  jav  a2s  . co  m*/
    }
    conn.connect();

    return IOUtils.toString(new InputStreamReader(conn.getInputStream(), UTF8_STR));
}

From source file:com.hazelcast.simulator.utils.FileUtils.java

public static String getText(String url) {
    try {//from  w w w  .j a v  a  2s  .co m
        URL website = new URL(url);
        URLConnection connection = website.openConnection();

        InputStreamReader streamReader = null;
        BufferedReader reader = null;
        try {
            streamReader = new InputStreamReader(connection.getInputStream());
            reader = new BufferedReader(streamReader);

            StringBuilder response = new StringBuilder();
            String inputLine;
            while ((inputLine = reader.readLine()) != null) {
                response.append(inputLine);
            }
            return response.toString();
        } finally {
            closeQuietly(reader);
            closeQuietly(streamReader);
        }
    } catch (IOException e) {
        throw new FileUtilsException(e);
    }
}

From source file:modelcreation.ModelCreation.java

public static Team[] getTeams(int seasonId) {
    String response = "";
    try {/*from   w w w.  ja  v  a 2  s  .  c o m*/
        URL api = new URL("http://api.football-data.org/v1/soccerseasons/" + seasonId + "/teams");
        URLConnection connection = api.openConnection();
        connection.setRequestProperty("X-Auth-Token", TOKEN);
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            response += inputLine;
        }
        in.close();
    } catch (MalformedURLException ex) {
        Logger.getLogger(ModelCreation.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(ModelCreation.class.getName()).log(Level.SEVERE, null, ex);
    }

    if (response.isEmpty()) {
        System.out.println("Response is empty!");
        return null;
    }

    Team[] teams = Team.createTeams(response);

    return teams;
}

From source file:modelcreation.ModelCreation.java

public static Team[] getTeamsWithFixtures(int seasonId, int seasonYear) {
    String response = "";
    try {/*from  ww  w.  j  ava  2s  . co  m*/
        URL api = new URL("http://api.football-data.org/v1/soccerseasons/" + seasonId + "/teams");
        URLConnection connection = api.openConnection();
        connection.setRequestProperty("X-Auth-Token", TOKEN);
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            response += inputLine;
        }
        in.close();
    } catch (MalformedURLException ex) {
        Logger.getLogger(ModelCreation.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(ModelCreation.class.getName()).log(Level.SEVERE, null, ex);
    }

    if (response.isEmpty()) {
        System.out.println("Response is empty!");
        return null;
    }

    Team[] teams = Team.createTeamsWithFixtures(response, seasonYear);

    return teams;
}

From source file:com.liferay.util.Http.java

public static String URLtoString(URL url) throws IOException {
    String xml = null;/*from   w  w w  . jav  a  2 s.co m*/

    if (url != null) {
        URLConnection con = url.openConnection();

        con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        con.setRequestProperty("User-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");

        InputStream is = con.getInputStream();

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        byte[] bytes = new byte[512];

        for (int i = is.read(bytes, 0, 512); i != -1; i = is.read(bytes, 0, 512)) {

            buffer.write(bytes, 0, i);
        }

        xml = new String(buffer.toByteArray());

        is.close();
        buffer.close();
    }

    return xml;
}

From source file:com.buglabs.dragonfly.util.WSHelper.java

protected static String post(URL url, String payload, Map props) throws IOException {
    String propstr = new String();

    for (Iterator i = props.keySet().iterator(); i.hasNext();) {
        String key = (String) i.next();
        propstr = propstr + URLEncoder.encode(key, "UTF-8") + "="
                + URLEncoder.encode((String) props.get(key), "UTF-8");
        if (i.hasNext()) {
            propstr = propstr + "&";
        }/*from  w ww  . j a v a  2  s.  com*/
    }

    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);

    OutputStreamWriter osr = new OutputStreamWriter(conn.getOutputStream());
    osr.write(propstr);
    osr.flush();

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

    return resp;
}

From source file:htmlwordtag.HtmlWordTag.java

private static void loadhtml() {
    String current = System.getProperty("user.dir");
    System.out.println("Current working directory in Java : " + current);

    File dir = new File(current + "\\input");
    if (!dir.exists()) {
        if (dir.mkdir()) {
            System.out.println("Directory is created!");
        } else {// w w w .  j  a v  a2 s  .  c om
            System.out.println("Failed to create directory!");
        }
    }

    File f = null;
    try {
        // create new file
        f = new File(current + "\\input\\website1.html");

        // tries to create new file in the system
        if (f.exists()) {
            if (f.delete()) {
                System.out.println("file Website1.html is already exist.");
                System.out.println("file Website1.html has been delete.");
                if (f.createNewFile()) {
                    System.out.println("create Webstie1.html success");
                } else {
                    System.out.println("fail to create Webstie1.html");
                }
            } else {
                System.out.println("fail to delete Website1.html.");
            }
        } else {
            if (f.createNewFile()) {
                System.out.println("create Webstie1.html success");
            } else {
                System.out.println("fail to create Webstie1.html");
            }
        }

        PrintWriter outputStream = null;
        try {
            outputStream = new PrintWriter(new FileOutputStream(current + "\\input\\website1.html"));
        } catch (FileNotFoundException e) {
            System.out.println("Error to find file webstie1.html");
            System.exit(0);
        }
        URL website = new URL(
                "http://www.engadget.com/2014/11/23/hoverboard-cardboard-robots-toyota-prius-camper/");
        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();
        outputStream.println(response);
        outputStream.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

    File f2 = null;
    try {
        // create new file
        f2 = new File(current + "\\input\\website2.html");

        // tries to create new file in the system
        if (f2.exists()) {
            if (f2.delete()) {
                System.out.println("file Website2.html is already exist.");
                System.out.println("file Website2.html has been delete.");
                if (f.createNewFile()) {
                    System.out.println("create Webstie2.html success");
                } else {
                    System.out.println("fail to create Webstie2.html");
                }
            } else {
                System.out.println("fail to delete Website2.html.");
            }
        } else {
            if (f2.createNewFile()) {
                System.out.println("create Webstie2.html success");
            } else {
                System.out.println("fail to create Webstie2.html");
            }
        }

        PrintWriter outputStream2 = null;
        try {
            outputStream2 = new PrintWriter(new FileOutputStream(current + "\\input\\website2.html"));
        } catch (FileNotFoundException e) {
            System.out.println("Error to find file webstie2.html");
            System.exit(0);
        }
        URL website = new URL("http://www.engadget.com/2014/11/21/net-neutrality-lawsuit/");
        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();
        outputStream2.println(response);
        outputStream2.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.buglabs.dragonfly.util.WSHelper.java

protected static String post(URL url, Map props) throws IOException {
    String propstr = new String();

    for (Iterator i = props.keySet().iterator(); i.hasNext();) {
        String key = (String) i.next();
        propstr = propstr + URLEncoder.encode(key, "UTF-8") + "="
                + URLEncoder.encode((String) props.get(key), "UTF-8");
        if (i.hasNext()) {
            propstr = propstr + "&";
        }//from  w w w .  ja v a2s  . c o  m
    }

    SSLUtils.verifyHost();

    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);

    OutputStreamWriter osr = new OutputStreamWriter(conn.getOutputStream());
    osr.write(propstr);
    osr.flush();

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

    return resp;
}