List of usage examples for java.net URLConnection getContentType
public String getContentType()
From source file:Main.java
public static void main(String args[]) throws Exception { URL u = new URL("http://www.java2s.com"); URLConnection uc = u.openConnection(); System.out.println(uc.getContentType()); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { String encoding = "ISO-8859-1"; URL u = new URL("http://www.java2s.com"); URLConnection uc = u.openConnection(); String contentType = uc.getContentType(); int encodingStart = contentType.indexOf("charset="); if (encodingStart != -1) { encoding = contentType.substring(encodingStart + 8); }/*from ww w . j a v a2 s. c om*/ InputStream in = new BufferedInputStream(uc.getInputStream()); Reader r = new InputStreamReader(in, encoding); int c; while ((c = r.read()) != -1) { System.out.print((char) c); } }
From source file:Main.java
public static void main(String args[]) throws Exception { URL u = new URL("http://www.java2s.com"); URLConnection uc = u.openConnection(); System.out.println("Content-type: " + uc.getContentType()); System.out.println("Content-encoding: " + uc.getContentEncoding()); System.out.println("Date: " + new Date(uc.getDate())); System.out.println("Last modified: " + new Date(uc.getLastModified())); System.out.println("Expiration date: " + new Date(uc.getExpiration())); System.out.println("Content-length: " + uc.getContentLength()); }
From source file:Main.java
public static void main(String args[]) throws Exception { URL u = new URL("http://www.java2s.com"); URLConnection uc = u.openConnection(); uc.connect();/*from w w w.j a va 2 s . co m*/ System.out.println("Content-type: " + uc.getContentType()); System.out.println("Content-encoding: " + uc.getContentEncoding()); System.out.println("Date: " + new Date(uc.getDate())); System.out.println("Last modified: " + new Date(uc.getLastModified())); System.out.println("Expiration date: " + new Date(uc.getExpiration())); System.out.println("Content-length: " + uc.getContentLength()); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { URL u = new URL("http://www.java2s.com/binary.dat"); URLConnection uc = u.openConnection(); String contentType = uc.getContentType(); int contentLength = uc.getContentLength(); if (contentType.startsWith("text/") || contentLength == -1) { throw new IOException("This is not a binary file."); }/*from w w w .j a v a 2 s .c o m*/ InputStream raw = uc.getInputStream(); InputStream 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; } in.close(); if (offset != contentLength) { throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes"); } String filename = u.getFile().substring(filename.lastIndexOf('/') + 1); FileOutputStream out = new FileOutputStream(filename); out.write(data); out.flush(); out.close(); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { int c;/*from w ww.j a v a 2s .c o m*/ URL hp = new URL("http", "www.java2s.com", 80, "/"); URLConnection hpCon = hp.openConnection(); System.out.println("Date: " + hpCon.getDate()); System.out.println("Type: " + hpCon.getContentType()); System.out.println("Exp: " + hpCon.getExpiration()); System.out.println("Last M: " + hpCon.getLastModified()); System.out.println("Length: " + hpCon.getContentLength()); }
From source file:Main.java
public static void main(String args[]) throws Exception { int c;// w w w . j a va 2 s . co m URL hp = new URL("http://www.internic.net"); URLConnection hpCon = hp.openConnection(); long d = hpCon.getDate(); if (d == 0) System.out.println("No date information."); else System.out.println("Date: " + new Date(d)); System.out.println("Content-Type: " + hpCon.getContentType()); d = hpCon.getExpiration(); if (d == 0) System.out.println("No expiration information."); else System.out.println("Expires: " + new Date(d)); d = hpCon.getLastModified(); if (d == 0) System.out.println("No last-modified information."); else System.out.println("Last-Modified: " + new Date(d)); int len = hpCon.getContentLength(); if (len == -1) System.out.println("Content length unavailable."); else System.out.println("Content-Length: " + len); if (len != 0) { InputStream input = hpCon.getInputStream(); int i = len; while (((c = input.read()) != -1)) { // && (--i > 0)) { System.out.print((char) c); } input.close(); } else { System.out.println("No content available."); } }
From source file:Main.java
public static void main(String args[]) throws Exception { String fullURL = args[0];/*from ww w.java2s . c om*/ URL u = new URL(fullURL); URLConnection conn = u.openConnection(); conn.setDoInput(true); OutputStream theControl = conn.getOutputStream(); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(theControl)); for (int i = 1; i < args.length; i++) { out.write(args[i] + "\n"); } out.close(); InputStream theData = conn.getInputStream(); String contentType = conn.getContentType(); if (contentType.toLowerCase().startsWith("text")) { BufferedReader in = new BufferedReader(new InputStreamReader(theData)); String line; while ((line = in.readLine()) != null) { System.out.println(line); } } }
From source file:Main.java
public static void main(String args[]) throws Exception { String fullURL = args[0];/* w w w . j a v a 2s.c o m*/ URL u = new URL(fullURL); URLConnection conn = u.openConnection(); conn.setDoOutput(true); OutputStream theControl = conn.getOutputStream(); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(theControl)); for (int i = 1; i < args.length; i++) { out.write(args[i] + "\n"); } out.close(); InputStream theData = conn.getInputStream(); String contentType = conn.getContentType(); if (contentType.toLowerCase().startsWith("text")) { BufferedReader in = new BufferedReader(new InputStreamReader(theData)); String line; while ((line = in.readLine()) != null) { System.out.println(line); } } }
From source file:URLConnectionTest.java
public static void main(String[] args) { try {/*from w ww . j a v a2 s.c o m*/ String urlName; if (args.length > 0) urlName = args[0]; else urlName = "http://java.sun.com"; URL url = new URL(urlName); URLConnection connection = url.openConnection(); // set username, password if specified on command line if (args.length > 2) { String username = args[1]; String password = args[2]; String input = username + ":" + password; String encoding = base64Encode(input); connection.setRequestProperty("Authorization", "Basic " + encoding); } connection.connect(); // print header fields Map<String, List<String>> headers = connection.getHeaderFields(); for (Map.Entry<String, List<String>> entry : headers.entrySet()) { String key = entry.getKey(); for (String value : entry.getValue()) System.out.println(key + ": " + value); } // print convenience functions System.out.println("----------"); System.out.println("getContentType: " + connection.getContentType()); System.out.println("getContentLength: " + connection.getContentLength()); System.out.println("getContentEncoding: " + connection.getContentEncoding()); System.out.println("getDate: " + connection.getDate()); System.out.println("getExpiration: " + connection.getExpiration()); System.out.println("getLastModifed: " + connection.getLastModified()); System.out.println("----------"); Scanner in = new Scanner(connection.getInputStream()); // print first ten lines of contents for (int n = 1; in.hasNextLine() && n <= 10; n++) System.out.println(in.nextLine()); if (in.hasNextLine()) System.out.println(". . ."); } catch (IOException e) { e.printStackTrace(); } }