Java/Network Protocol/URLConnection — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 07:21, 1 июня 2010

A CGI POST Example

     
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
public class CGIPost {
  public static void main(String args[]) throws Exception {
    String fullURL = args[0];
    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);
      }
    }
  }
}





Call a servlet from a Java command line application

     
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.URL;
import java.net.URLConnection;
public class CounterApp {
  public static void main(String args[])throws Exception {
    String sessionCookie = null;
    URL url = new java.net.URL("http://127.0.0.1/yourServlet");
    URLConnection con = url.openConnection();
    if (sessionCookie != null) {
      con.setRequestProperty("cookie", sessionCookie);
    }
    con.setUseCaches(false);
    con.setDoOutput(true);
    con.setDoInput(true);
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(byteOut);
    out.flush();
    byte buf[] = byteOut.toByteArray();
    con.setRequestProperty("Content-type", "application/octet-stream");
    con.setRequestProperty("Content-length", "" + buf.length);
    DataOutputStream dataOut = new DataOutputStream(con.getOutputStream());
    dataOut.write(buf);
    dataOut.flush();
    dataOut.close();
    DataInputStream in = new DataInputStream(con.getInputStream());
    int count = in.readInt();
    in.close();
    if (sessionCookie == null) {
      String cookie = con.getHeaderField("set-cookie");
      if (cookie != null) {
        sessionCookie = parseCookie(cookie);
        System.out.println("Setting session ID=" + sessionCookie);
      }
    }
    System.out.println(count);
  }
  public static String parseCookie(String raw) {
    String c = raw;
    if (raw != null) {
      int endIndex = raw.indexOf(";");
      if (endIndex >= 0) {
        c = raw.substring(0, endIndex);
      }
    }
    return c;
  }
}





Check if a file was modified on the server

    
import java.net.URL;
import java.net.URLConnection;
public class Main {
  public static void main(String[] argv) throws Exception {
    URL u = new URL("http://127.0.0.1/test.gif");
    URLConnection uc = u.openConnection();
    uc.setUseCaches(false);
    long timestamp = uc.getLastModified();
  }
}





Demonstrate URLConnection.

     
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;
public class MainClass {
  public static void main(String args[]) throws Exception {
    int c;
    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.");
    }
  }
}





Download from URL

 
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
public class Utils {
  public static void download(String urlStr, File destFile) throws IOException {
    URL srcUrl = new URL(urlStr);
    InputStream input = null;
    OutputStream output = null;
    try {
      input = srcUrl.openStream();
      FileOutputStream fos = new FileOutputStream(destFile);
      output = new BufferedOutputStream(fos);
      copyStreams(input, output);
    } finally {
      if (input != null) {
        input.close();
      }
      if (output != null) {
        output.flush();
        output.close();
      }
    }
  }
  private static void copyStreams(InputStream input, OutputStream output) throws IOException {
    int count;
    byte data[] = new byte[1024];
    while ((count = input.read(data, 0, 1024)) != -1) {
      output.write(data, 0, count);
    }
  }
}





Downloading a web page using URL and URLConnection classes

    
import java.io.BufferedInputStream;
import java.net.URL;
import java.net.URLConnection;
public class Main {
  public static void main(String[] args) throws Exception {
    URLConnection urlc = new URL("http://www.google.ru").openConnection();
    BufferedInputStream buffer = new BufferedInputStream(urlc.getInputStream());
    
    int byteRead;
    while ((byteRead = buffer.read()) != -1){
      System.out.println((char) byteRead);
    }
    buffer.close();
  }
}





File size from URL

    
import java.net.URL;
import java.net.URLConnection;
public class Main {
  public static void main(String[] argv) throws Exception {
    int size;
    URL url = new URL("http://www.server.ru");
    URLConnection conn = url.openConnection();
    size = conn.getContentLength();
    if (size < 0)
      System.out.println("Could not determine file size.");
    else
      System.out.println(size);
    conn.getInputStream().close();
  }
}





Get response header from HTTP request

    
 
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class Main {
  public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.google.ru/index.html");
    URLConnection connection = url.openConnection();
    Map responseMap = connection.getHeaderFields();
    for (Iterator iterator = responseMap.keySet().iterator(); iterator.hasNext();) {
      String key = (String) iterator.next();
      System.out.println(key + " = ");
      List values = (List) responseMap.get(key);
      for (int i = 0; i < values.size(); i++) {
        Object o = values.get(i);
        System.out.println(o + ", ");
      }
    }
  }
}





Getting an Image from a URL

    
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
public class Main {
  public static void main(String[] argv) throws Exception {
    URL url = new URL("http://hostname:80/image.gif");
    Image image = Toolkit.getDefaultToolkit().getDefaultToolkit().createImage(url);
  }
}





Getting Text from a URL

    
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class Main {
  public static void main(String[] argv) throws Exception {
    URL url = new URL("http://hostname:80/index.html");
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str;
    while ((str = in.readLine()) != null) {
      System.out.println(str); 
    }
    in.close();
  }
}





Get URLConnection Expiration

  
/*
    GNU LESSER GENERAL PUBLIC LICENSE
    Copyright (C) 2006 The Lobo Project
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    Contact info: lobochief@users.sourceforge.net
*/
/*
 * Created on Jun 12, 2005
 */
import java.net.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.*;
public class Urls {
  private static final Logger logger = Logger.getLogger(Urls.class.getName());
  public static final DateFormat PATTERN_RFC1123 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
  static {
    DateFormat df = PATTERN_RFC1123;
    df.setTimeZone(TimeZone.getTimeZone("GMT"));
  }
  
  private Urls() {
    super();
  }
  /** Whether the URL refers to a resource in the local file system. */
  public static boolean isLocal(java.net.URL url) {
    if(isLocalFile(url)) {
      return true;
    }
    String protocol = url.getProtocol();
    if("jar".equalsIgnoreCase(protocol)) {
      String path = url.getPath();
      int emIdx = path.lastIndexOf("!");
      String subUrlString = emIdx == -1 ? path : path.substring(0, emIdx);
      try {
        URL subUrl = new URL(subUrlString);
        return isLocal(subUrl);
      } catch(java.net.MalformedURLException mfu) {
        return false;
      }
    }
    else {
      return false;
    }
  }
  
  /** Whether the URL is a file in the local file system. */
  public static boolean isLocalFile(java.net.URL url) {
    String scheme = url.getProtocol();
    return "file".equalsIgnoreCase(scheme) && !hasHost(url);
  }
  public static boolean hasHost(java.net.URL url) {
    String host = url.getHost();
    return host != null && !"".equals(host);
  }
  /**
   * Creates an absolute URL in a manner equivalent to
   * major browsers. 
   */
  public static URL createURL(URL baseUrl, String relativeUrl) throws java.net.MalformedURLException {
    return new URL(baseUrl, relativeUrl);
  } 
  
  /**
   * Returns the time when the document should be considered expired.
   * The time will be zero if the document always needs to be revalidated.
   * It will be <code>null</code> if no expiration time is specified.
   */
  public static Long getExpiration(URLConnection connection, long baseTime) {
    String cacheControl = connection.getHeaderField("Cache-Control");
    if(cacheControl != null) {
      StringTokenizer tok = new StringTokenizer(cacheControl, ",");
      while(tok.hasMoreTokens()) {
        String token = tok.nextToken().trim().toLowerCase();
        if("must-revalidate".equals(token)) {
          return new Long(0);
        }
        else if(token.startsWith("max-age")) {
          int eqIdx = token.indexOf("=");
          if(eqIdx != -1) {
            String value = token.substring(eqIdx+1).trim();
            int seconds;
            try {
              seconds = Integer.parseInt(value);
              return new Long(baseTime + seconds * 1000);
            } catch(NumberFormatException nfe) {
              logger.warning("getExpiration(): Bad Cache-Control max-age value: " + value);
              // ignore
            }
          }
        }
      }
    }
    String expires = connection.getHeaderField("Expires");
    if(expires != null) {
      try {
        synchronized(PATTERN_RFC1123) {
          Date expDate = PATTERN_RFC1123.parse(expires);
          return new Long(expDate.getTime());
        }
      } catch(java.text.ParseException pe) {
        int seconds;
        try {
          seconds = Integer.parseInt(expires);
          return new Long(baseTime + seconds * 1000);
        } catch(NumberFormatException nfe) {
          logger.warning("getExpiration(): Bad Expires header value: " + expires);
        }
      }
    }
    return null;
  }
  
    private static String getDefaultCharset(URLConnection connection) {
      URL url = connection.getURL();
      if(Urls.isLocalFile(url)) {
        String charset = System.getProperty("file.encoding");
        return charset == null ? "ISO-8859-1" : charset;
      }
      else {
        return "ISO-8859-1";
      }
    }
    
    public static String getNoRefForm(URL url) {
      String host = url.getHost();
      int port = url.getPort();
      String portText = port == -1 ? "" : ":" + port;
      String userInfo = url.getUserInfo();
      String userInfoText = userInfo == null || userInfo.length() == 0 ? "" : userInfo + "@";
      String hostPort = host == null || host.length() == 0 ? "" : "//" + userInfoText + host + portText;
      return url.getProtocol() + ":" + hostPort + url.getFile();
    }

}





Http authentication header

     
 
 import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
public class BasicAuthNeeded {
  public static void main(String[] args) throws Exception {
    String s;
    s = "http://www.y.ru/authTest";
    URL url = new URL(s);
    URLConnection urlc = url.openConnection();
    Map<String, List<String>> hf = urlc.getHeaderFields();
    for (String key : hf.keySet())
      System.out.println(key + ": " + urlc.getHeaderField(key));
    System.out.println(((HttpURLConnection) urlc).getResponseCode());
  }
}





Http Constants

  
/*
 * Copyright 2007 Outerthought bvba and Schaubroeck nv
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
public class HttpConstants {
    public final static String GET = "GET",
            POST = "POST",
            HEAD = "HEAD",
            PUT = "PUT",
            OPTIONS = "OPTIONS",
            DELETE = "DELETE";
    // These are copy-pasted from the Jetty codebase
    public final static int
            _100_Continue = 100,
            _101_Switching_Protocols = 101,
            _102_Processing = 102,
            _200_OK = 200,
            _201_Created = 201,
            _202_Accepted = 202,
            _203_Non_Authoritative_Information = 203,
            _204_No_Content = 204,
            _205_Reset_Content = 205,
            _206_Partial_Content = 206,
            _207_Multi_Status = 207,
            _300_Multiple_Choices = 300,
            _301_Moved_Permanently = 301,
            _302_Moved_Temporarily = 302,
            _302_Found = 302,
            _303_See_Other = 303,
            _304_Not_Modified = 304,
            _305_Use_Proxy = 305,
            _400_Bad_Request = 400,
            _401_Unauthorized = 401,
            _402_Payment_Required = 402,
            _403_Forbidden = 403,
            _404_Not_Found = 404,
            _405_Method_Not_Allowed = 405,
            _406_Not_Acceptable = 406,
            _407_Proxy_Authentication_Required = 407,
            _408_Request_Timeout = 408,
            _409_Conflict = 409,
            _410_Gone = 410,
            _411_Length_Required = 411,
            _412_Precondition_Failed = 412,
            _413_Request_Entity_Too_Large = 413,
            _414_Request_URI_Too_Large = 414,
            _415_Unsupported_Media_Type = 415,
            _416_Requested_Range_Not_Satisfiable = 416,
            _417_Expectation_Failed = 417,
            _422_Unprocessable_Entity = 422,
            _423_Locked = 423,
            _424_Failed_Dependency = 424,
            _500_Internal_Server_Error = 500,
            _501_Not_Implemented = 501,
            _502_Bad_Gateway = 502,
            _503_Service_Unavailable = 503,
            _504_Gateway_Timeout = 504,
            _505_HTTP_Version_Not_Supported = 505,
            _507_Insufficient_Storage = 507,
            _999_Unknown = 999;
    public final static String
            MIMETYPE_TEXT_HTML = "text/html",
            MIMETYPE_TEXT_PLAIN = "text/plain",
            MIMETYPE_TEXT_XML = "text/xml",
            MIMETYPE_TEXT_HTML_8859_1 = "text/html; charset=iso-8859-1",
            MIMETYPE_TEXT_PLAIN_8859_1 = "text/plain; charset=iso-8859-1",
            MIMETYPE_TEXT_XML_8859_1 = "text/xml; charset=iso-8859-1",
            MIMETYPE_TEXT_HTML_UTF_8 = "text/html; charset=utf-8",
            MIMETYPE_TEXT_PLAIN_UTF_8 = "text/plain; charset=utf-8",
            MIMETYPE_TEXT_XML_UTF_8 = "text/xml; charset=utf-8";
}





Http Parser

  
/**
Copyright (C) 2004  Juho V&auml;h&auml;-Herttua
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

import java.io.*;
import java.util.*;
import java.text.*;
import java.net.URLDecoder;
public class HttpParser {
  private static final String[][] HttpReplies = {{"100", "Continue"},
                                                 {"101", "Switching Protocols"},
                                                 {"200", "OK"},
                                                 {"201", "Created"},
                                                 {"202", "Accepted"},
                                                 {"203", "Non-Authoritative Information"},
                                                 {"204", "No Content"},
                                                 {"205", "Reset Content"},
                                                 {"206", "Partial Content"},
                                                 {"300", "Multiple Choices"},
                                                 {"301", "Moved Permanently"},
                                                 {"302", "Found"},
                                                 {"303", "See Other"},
                                                 {"304", "Not Modified"},
                                                 {"305", "Use Proxy"},
                                                 {"306", "(Unused)"},
                                                 {"307", "Temporary Redirect"},
                                                 {"400", "Bad Request"},
                                                 {"401", "Unauthorized"},
                                                 {"402", "Payment Required"},
                                                 {"403", "Forbidden"},
                                                 {"404", "Not Found"},
                                                 {"405", "Method Not Allowed"},
                                                 {"406", "Not Acceptable"},
                                                 {"407", "Proxy Authentication Required"},
                                                 {"408", "Request Timeout"},
                                                 {"409", "Conflict"},
                                                 {"410", "Gone"},
                                                 {"411", "Length Required"},
                                                 {"412", "Precondition Failed"},
                                                 {"413", "Request Entity Too Large"},
                                                 {"414", "Request-URI Too Long"},
                                                 {"415", "Unsupported Media Type"},
                                                 {"416", "Requested Range Not Satisfiable"},
                                                 {"417", "Expectation Failed"},
                                                 {"500", "Internal Server Error"},
                                                 {"501", "Not Implemented"},
                                                 {"502", "Bad Gateway"},
                                                 {"503", "Service Unavailable"},
                                                 {"504", "Gateway Timeout"},
                                                 {"505", "HTTP Version Not Supported"}};
  private BufferedReader reader;
  private String method, url;
  private Hashtable headers, params;
  private int[] ver;
  public HttpParser(InputStream is) {
    reader = new BufferedReader(new InputStreamReader(is));
    method = "";
    url = "";
    headers = new Hashtable();
    params = new Hashtable();
    ver = new int[2];
  }
  public int parseRequest() throws IOException {
    String initial, prms[], cmd[], temp[];
    int ret, idx, i;
    ret = 200; // default is OK now
    initial = reader.readLine();
    if (initial == null || initial.length() == 0) return 0;
    if (Character.isWhitespace(initial.charAt(0))) {
      // starting whitespace, return bad request
      return 400;
    }
    cmd = initial.split("\\s");
    if (cmd.length != 3) {
      return 400;
    }
    if (cmd[2].indexOf("HTTP/") == 0 && cmd[2].indexOf(".") > 5) {
      temp = cmd[2].substring(5).split("\\.");
      try {
        ver[0] = Integer.parseInt(temp[0]);
        ver[1] = Integer.parseInt(temp[1]);
      } catch (NumberFormatException nfe) {
        ret = 400;
      }
    }
    else ret = 400;
    if (cmd[0].equals("GET") || cmd[0].equals("HEAD")) {
      method = cmd[0];
      idx = cmd[1].indexOf("?");
      if (idx < 0) url = cmd[1];
      else {
        url = URLDecoder.decode(cmd[1].substring(0, idx), "ISO-8859-1");
        prms = cmd[1].substring(idx+1).split("&");
        params = new Hashtable();
        for (i=0; i<prms.length; i++) {
          temp = prms[i].split("=");
          if (temp.length == 2) {
            // we use ISO-8859-1 as temporary charset and then
            // String.getBytes("ISO-8859-1") to get the data
            params.put(URLDecoder.decode(temp[0], "ISO-8859-1"),
                       URLDecoder.decode(temp[1], "ISO-8859-1"));
          }
          else if(temp.length == 1 && prms[i].indexOf("=") == prms[i].length()-1) {
            // handle empty string separatedly
            params.put(URLDecoder.decode(temp[0], "ISO-8859-1"), "");
          }
        }
      }
      parseHeaders();
      if (headers == null) ret = 400;
    }
    else if (cmd[0].equals("POST")) {
      ret = 501; // not implemented
    }
    else if (ver[0] == 1 && ver[1] >= 1) {
      if (cmd[0].equals("OPTIONS") ||
          cmd[0].equals("PUT") ||
          cmd[0].equals("DELETE") ||
          cmd[0].equals("TRACE") ||
          cmd[0].equals("CONNECT")) {
        ret = 501; // not implemented
      }
    }
    else {
      // meh not understand, bad request
      ret = 400;
    }
    if (ver[0] == 1 && ver[1] >= 1 && getHeader("Host") == null) {
      ret = 400;
    }
    return ret;
  }
  private void parseHeaders() throws IOException {
    String line;
    int idx;
    // that fscking rfc822 allows multiple lines, we don"t care now
    line = reader.readLine();
    while (!line.equals("")) {
      idx = line.indexOf(":");
      if (idx < 0) {
        headers = null;
        break;
      }
      else {
        headers.put(line.substring(0, idx).toLowerCase(), line.substring(idx+1).trim());
      }
      line = reader.readLine();
    }
  }
  public String getMethod() {
    return method;
  }
  public String getHeader(String key) {
    if (headers != null)
      return (String) headers.get(key.toLowerCase());
    else return null;
  }
  public Hashtable getHeaders() {
    return headers;
  }
  public String getRequestURL() {
    return url;
  }
  public String getParam(String key) {
    return (String) params.get(key);
  }
  public Hashtable getParams() {
    return params;
  }
  public String getVersion() {
    return ver[0] + "." + ver[1];
  }
  public int compareVersion(int major, int minor) {
    if (major < ver[0]) return -1;
    else if (major > ver[0]) return 1;
    else if (minor < ver[1]) return -1;
    else if (minor > ver[1]) return 1;
    else return 0;
  }
  public static String getHttpReply(int codevalue) {
    String key, ret;
    int i;
    ret = null;
    key = "" + codevalue;
    for (i=0; i<HttpReplies.length; i++) {
      if (HttpReplies[i][0].equals(key)) {
        ret = codevalue + " " + HttpReplies[i][1];
        break;
      }
    }
    return ret;
  }
  public static String getDateHeader() {
    SimpleDateFormat format;
    String ret;
    format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss", Locale.US);
    format.setTimeZone(TimeZone.getTimeZone("GMT"));
    ret = "Date: " + format.format(new Date()) + " GMT";
    return ret;
  }
}





Locating files by path or URL

 
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2006, Red Hat Middleware LLC, and individual contributors 
 * as indicated by the @author tags. 
 * See the copyright.txt in the distribution for a
 * full listing of individual contributors. 
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License, v. 2.1.
 * This program is distributed in the hope that it will be useful, but WITHOUT A 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License,
 * v.2.1 along with this distribution; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 * 
 * (C) 2005-2006,
 * @author JBoss Inc.
 */
/*
* Copyright (C) 1998, 1999, 2000, 2001,
*
* Arjuna Solutions Limited,
* Newcastle upon Tyne,
* Tyne and Wear,
* UK.
*
* $Id: FileLocator.java 2342 2006-03-30 13:06:17Z  $
*/

import java.io.File;
import java.net.URL;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
/**
 * The FileLocator class provides a common method for locating files.
 * If not passed an absolute filename (starting with the string "abs://"),
 * it searches for the file in the order:
 *   in the directory specified by the system property user.dir
 *   in the directory specified by the system property user.home
 *   in the directory specified by the system property java.home
 *   using the getResource() method
 *
 * @author Julian Coleman
 * @version $Id: FileLocator.java 2342 2006-03-30 13:06:17Z  $
 * @since JTS 3.0.
 */
public class FileLocator
{
   /**
    * Locate the specific file.
    * Return the (URL decoded) abolute pathname to the file or null.
    */
   public static String locateFile (String findFile) throws FileNotFoundException
   {
      URL url;
      String fullPathName;
      StringBuffer decodedPathName;
      int pos, len, start;
      if (findFile == null)
         throw new FileNotFoundException("locateFile: null file name");
      if (findFile.startsWith(absolutePath))
         return findFile.substring(absolutePath.length());
      if ((fullPathName = locateByProperty(findFile)) != null)
         return fullPathName;
      if ((url = locateByResource(findFile)) != null)
      {
  /*
   * The URL that we receive from getResource /might/ have " "
   * (space) characters converted to "%20" strings.  However,
   * it doesn"t have other URL encoding (e.g "+" characters are
   * kept intact), so we"ll just convert all "%20" strings to
   * " " characters and hope for the best.
   */
  fullPathName = url.getFile();
  pos = 0;
  len = fullPathName.length();
  start = 0;
  decodedPathName = new StringBuffer();
  while ((pos = fullPathName.indexOf(pct20, start)) != -1) {
      decodedPathName.append(fullPathName.substring(start, pos));
      decodedPathName.append(" ");
      start = pos + pct20len;
  }
  if (start < len)
      decodedPathName.append(fullPathName.substring(start, len));
  fullPathName=decodedPathName.toString();
  if (platformIsWindows())
      fullPathName = fullPathName.substring(1, fullPathName.length());
  return fullPathName;
      }
      throw new FileNotFoundException("locateFile: file not found: " + findFile);
   }
   /**
    * Locate the specific file.
    * Return the file name in URL form or null.
    */
   public static URL locateURL (String findFile) throws FileNotFoundException
   {
      URL url;
      String fullPathName;
      if (findFile == null)
         throw new FileNotFoundException("locateURL: null file name");
      try {
         if (findFile.startsWith(absolutePath))
         {
            return (new URL("file:/" + findFile.substring(absolutePath.length())));
         }
         if ((fullPathName = locateByProperty(findFile)) != null)
         {
            if(platformIsWindows())
               url = new URL("file:/" + fullPathName);
            else
               url = new URL("file:" + fullPathName);
            return url;
         }
         //TODO: TR: used for testing:  return new URL(findFile);
      }
      catch (MalformedURLException e)
      {
         System.err.println("locateURL: URL creation problem");
         throw new FileNotFoundException("locateURL: URL creation problem");
      }
      if ((url = locateByResource(findFile)) != null)
         return url;
      throw new FileNotFoundException("locateURL: file not found: " + findFile);
   }
   /**
    * Search for a file using the properties: user.dir, user.home, java.home
    * Returns absolute path name or null.
    */
   private static synchronized String locateByProperty(String findFile)
   {
      String fullPathName = null;
      String dir = null;
      File f = null;
      if (findFile == null)
         return null;
      try
      {
         // System.err.println("Searching in user.dir for: " + findFile);
         dir = System.getProperty("user.dir");
         if (dir != null) {
            fullPathName = dir + File.separatorChar + findFile;
            f = new File(fullPathName);
         }
         if (f != null && f.exists())
         {
            // System.err.println("Found in user.dir");
            return fullPathName;
         }
         dir = System.getProperty("user.home");
         if (dir != null) {
            fullPathName = dir + File.separatorChar + findFile;
            f = new File(fullPathName);
         }
         if (f != null && f.exists())
         {
            // System.err.println("Found in user.home");
            return fullPathName;
         }
         dir = System.getProperty("java.home");
         if (dir != null) {
            fullPathName = dir + File.separatorChar + findFile;
            f = new File(fullPathName);
         }
         if (f != null && f.exists())
         {
            // System.err.println("Found in java.home");
            return fullPathName;
         }
      }
      catch (Exception e)
      {
         return null;
      }
      return null;
   }
   /**
    * Search for a file using the properties: user.dir, user.home, java.home
    * Returns URL or null.
    */
   private static URL locateByResource(String findFile)
   {
      ClassLoader loader = Thread.currentThread().getContextClassLoader();
      URL url = loader.getResource(findFile);
      if (url == null)
      {
          url = FileLocator.class.getResource("/" + findFile);
      }
      // System.err.println("Search succeeded via getResource()");
      return url;
   }
   /*
   * Check the file separator to see if we"re on a Windows platform.
   *
   * @return  boolean True if the platform is Windows, false otherwise.
   */
   private static boolean platformIsWindows()
   {
      if(File.separatorChar == "\\")
      {
         return true;
      }
      return false;
   }
   private static final String absolutePath = "abs://";
   private static final String pct20 = "%20";
   private static final int pct20len = 3;
}





Read a GIF or CLASS from an URL save it locally

    
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;
public class Main {
  public static void main(String args[]) throws Exception {
    byte[] b = new byte[1];
    URL url = new URL("http://www.server.ru/a.gif");
    URLConnection urlConnection = url.openConnection();
    urlConnection.connect();
    DataInputStream di = new DataInputStream(urlConnection.getInputStream());
    FileOutputStream fo = new FileOutputStream("a.gif");
    while (-1 != di.read(b, 0, 1))
      fo.write(b, 0, 1);
    di.close();
    fo.close();
  }
}





Read / download webpage content

    
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.URL;
public class Main {
  public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.google.ru");
    BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
    BufferedWriter writer = new BufferedWriter(new FileWriter("data.html"));
    String line;
    while ((line = reader.readLine()) != null) {
      System.out.println(line);
      writer.write(line);
      writer.newLine();
    }
    reader.close();
    writer.close();
  }
}





Sending a POST Request Using a URL

    

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class Main {
  public static void main(String[] argv) throws Exception {
    String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
    data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
    URL url = new URL("http://server.ru:80/cgi");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
      System.out.println(line);
    }
    wr.close();
    rd.close();
  }
}





Sending a POST Request with Parameters From a Java Class

    
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
public class Main {
  public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.jexp.ru");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
    writer.write("value=1&anotherValue=1");
    writer.flush();
    String line;
    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    while ((line = reader.readLine()) != null) {
      System.out.println(line);
    }
    writer.close();
    reader.close();
  }
}





URLConnection.setRequestProperty

     
 
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
class MainClass {
  public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.x.ru");
    URLConnection urlc = url.openConnection();
    urlc.setRequestProperty("User-Agent", "Mozilla 5.0 (Windows; U; "
        + "Windows NT 5.1; en-US; rv:1.8.0.11) ");
    InputStream is = urlc.getInputStream();
    int c;
    while ((c = is.read()) != -1)
      System.out.print((char) c);
  }
}





Zip URLConnection

   
/**
 * The utillib library.
 * More information is available at http://www.jinchess.ru/.
 * Copyright (C) 2003 Alexander Maryanovsky.
 * All rights reserved.
 *
 * The utillib library is free software; you can redistribute
 * it and/or modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * The utillib library is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with utillib library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.net.URL;
import java.net.URLConnection;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;

/**
 * A <code>URLConnection</code> which loads its content from a specified zip
 * entry within a local file.
 */
public class ZipURLConnection extends URLConnection{

  /**
   * The zip file.
   */
  private final File file;

  /**
   * The name of the zip entry within the zip file.
   */
  private final String zipEntryName;

  /**
   * The <code>ZipFile</code> object for the zip file. Created when
   * <code>connect()</code> is called.
   */
  private ZipFile zipFile = null;

  /**
   * The <code>ZipEntry</code> object for the entry in the zip file. Created
   * when <code>connect()</code> is called.
   */
  private ZipEntry zipEntry = null;

  /**
   * Creates a new <code>ZipURLConnection</code> for the specified zip entry
   * within the specified <code>File</code>.
   */
  public ZipURLConnection(URL url, File file, String zipEntryName){
    super(url);
    this.file = file;
    this.zipEntryName = zipEntryName;
  }

  /**
   * Attempts to open the zip entry.
   */
  public void connect() throws IOException{
    this.zipFile = new ZipFile(file);
    this.zipEntry = zipFile.getEntry(zipEntryName);
    if (zipEntry == null)
      throw new IOException("Entry " + zipEntryName + " not found in file " + file);
    this.connected = true;
  }

  /**
   * Returns the <code>InputStream</code> that reads from this connection.
   */
  public InputStream getInputStream() throws IOException{
    if (!connected)
      connect();
    return zipFile.getInputStream(zipEntry);
  }

  /**
   * Returns the length of the uncompressed zip entry.
   */
  public int getContentLength(){
    if (!connected)
      return -1;
    return (int)zipEntry.getSize();
  }

}





Zip URLStream Handler

   
/**
 * The utillib library.
 * More information is available at http://www.jinchess.ru/.
 * Copyright (C) 2003 Alexander Maryanovsky.
 * All rights reserved.
 *
 * The utillib library is free software; you can redistribute
 * it and/or modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * The utillib library is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with utillib library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * <p>A URLStreamHandler which allows accessing specific zip entries within a
 * zip file. The format of the file part of the URL is
 * <code>[full path to the zip file]|[name of zip entry]</code>, but you are
 * encouraged to use the
 * <code>createURL(String fileName, String entryName)</code> method
 * instead of creating a URL manually. The host and port part of the URL should
 * be <code>null</code>. An application is responsible for setting a
 * <code>URLStreamHandlerFactory</code> that will return a ZipURLStreamHandler
 * when appropriate.
 */
public class ZipURLStreamHandler extends URLStreamHandler{

  
  /**
   * Returns a ZipURLConnection for the specified URL.
   */
  public URLConnection openConnection(URL url) throws IOException{
    String urlFile = url.getFile();
    int barIndex = urlFile.indexOf("|");
    if (barIndex == -1)
      throw new MalformedURLException("Missing "|"");
    String fileName = urlFile.substring(0, barIndex);
    String entryName = urlFile.substring(barIndex + 1);
    return new ZipURLConnection(url, new File(fileName), entryName);
  }

  /**
   * Parses the specified URL string.
   */
  protected void parseURL(URL url, String spec, int start, int limit){
    String urlFile = url.getFile();
    int barIndex = urlFile.indexOf("|");
    
    String fileName = barIndex == -1 ? urlFile : urlFile.substring(0, barIndex);
    String entryName = barIndex == -1 ? "" : urlFile.substring(barIndex + 1);
    
    int lastSlashIndex = entryName.lastIndexOf("/");
    String newEntryName = entryName.substring(0, lastSlashIndex + 1) + spec.substring(start, limit);
    
    setURL(url, url.getProtocol(), "", -1, fileName + "|" + newEntryName, null);
  }

  /**
   * Creates a <code>URL</code> that points to the specified entry within the
   * specified zip file. The <code>URL</code> will have "zip" as the protocol
   * name. To use the resulting <code>URL</code>, an application must set
   * a <code>URLStreamHandlerFactory</code> (via the <code>URL</code> class)
   * which will return a <code>ZipURLStreamHandler</code> for the "zip"
   * protocol. Returns <code>null</code> if the "zip: protocol is unrecognized.
   */
  public static URL createURL(String fileName, String entryName){
    return createURL("zip", fileName, entryName);
  }

  /**
   * Creates a <code>URL</code> that points to the specified entry within the
   * specified zip file and has the specified protocol name. To use the
   * resulting <code>URL</code>, an application must set a
   * <code>URLStreamHandlerFactory</code> (via the <code>URL</code> class)
   * which will return a <code>ZipURLStreamHandler</code> for the protocol name
   * given to this method. Returns <code>null</code> if the specified protocol
   * is unrecognized.
   */
  public static URL createURL(String protocol, String fileName, String entryName){
    try{
      return new URL(protocol, "", -1, fileName + "|" + entryName);
    } catch (MalformedURLException e){
        e.printStackTrace();
        return null;
      }
  }

}
/**
 * The utillib library.
 * More information is available at http://www.jinchess.ru/.
 * Copyright (C) 2003 Alexander Maryanovsky.
 * All rights reserved.
 *
 * The utillib library is free software; you can redistribute
 * it and/or modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * The utillib library is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with utillib library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/**
 * A <code>URLConnection</code> which loads its content from a specified zip
 * entry within a local file.
 */
 class ZipURLConnection extends URLConnection{

  /**
   * The zip file.
   */
  private final File file;

  /**
   * The name of the zip entry within the zip file.
   */
  private final String zipEntryName;

  /**
   * The <code>ZipFile</code> object for the zip file. Created when
   * <code>connect()</code> is called.
   */
  private ZipFile zipFile = null;

  /**
   * The <code>ZipEntry</code> object for the entry in the zip file. Created
   * when <code>connect()</code> is called.
   */
  private ZipEntry zipEntry = null;

  /**
   * Creates a new <code>ZipURLConnection</code> for the specified zip entry
   * within the specified <code>File</code>.
   */
  public ZipURLConnection(URL url, File file, String zipEntryName){
    super(url);
    this.file = file;
    this.zipEntryName = zipEntryName;
  }

  /**
   * Attempts to open the zip entry.
   */
  public void connect() throws IOException{
    this.zipFile = new ZipFile(file);
    this.zipEntry = zipFile.getEntry(zipEntryName);
    if (zipEntry == null)
      throw new IOException("Entry " + zipEntryName + " not found in file " + file);
    this.connected = true;
  }

  /**
   * Returns the <code>InputStream</code> that reads from this connection.
   */
  public InputStream getInputStream() throws IOException{
    if (!connected)
      connect();
    return zipFile.getInputStream(zipEntry);
  }

  /**
   * Returns the length of the uncompressed zip entry.
   */
  public int getContentLength(){
    if (!connected)
      return -1;
    return (int)zipEntry.getSize();
  }

}