Java examples for Network:URL
load File from URL and return InputStream
//package com.java2s; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; public class Main { public static void main(String[] argv) throws Exception { String urlString = "java2s.com"; System.out.println(loadFile(urlString)); }// ww w . j a v a2 s . co m public static InputStream loadFile(String urlString) { InputStream inuputStream = null; try { URL url = new URL(urlString); //create the new connection HttpURLConnection urlConnection = (HttpURLConnection) url .openConnection(); //set up some things on the connection urlConnection.setRequestMethod("GET"); urlConnection.setDoOutput(true); //and connect! urlConnection.connect(); //this will be used in reading the data from the internet inuputStream = urlConnection.getInputStream(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (inuputStream != null) return inuputStream; else return null; } }