下面列出了java.net.URLConnection#getURL ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns one of several object types (this set may change in future
* versions):
* 1) instance of Thread:
* Invoke the thread to launch an external viewer.
* 2) instance of InputStream:
* Bring up the "Save to disk" dialog page to allow the content
* to be saved to disk.
* 3) instance of InputStreamImageSource:
* Load the image into HotJava in an image viewer page.
* 4) instance of String:
* Go to a new page with the string as the plain text content
* of that page.
*/
public Object getContent(URLConnection uc) {
try {
InputStream is = uc.getInputStream();
StringBuffer sb = new StringBuffer();
int c;
sb.append("[Content of " + uc.getURL() + "]\n\n");
sb.append("[This opening message brought to you by your plain/text\n");
sb.append("content handler. To remove this content handler, delete the\n");
sb.append("COM.foo.content.text directory from your class path and\n");
sb.append("the java.content.handler.pkgs property from your HotJava\n");
sb.append("properties file.]\n");
sb.append("----------------------------------------------------------------\n\n");
// Read the characters from the source, accumulate them into the string buffer.
// (Not the most efficient, but simplest for this example.)
while ((c = is.read()) >= 0) {
sb.append((char)c);
}
// Tidy up
is.close();
// Return the resulting string to our client (we're case 4 above)
return sb.toString();
} catch (IOException e) {
// For any exception, just return an indication of what went wrong.
return "Problem reading document: " + uc.getURL();
}
}
/**
* Returns the URL.
*
* @return null if the class file could not be obtained.
*/
@Override
public URL find(String classname) {
try {
URLConnection con = openClassfile0(classname);
InputStream is = con.getInputStream();
if (is != null) {
is.close();
return con.getURL();
}
}
catch (IOException e) {}
return null;
}
/**
* Returns one of several object types (this set may change in future
* versions):
* 1) instance of Thread:
* Invoke the thread to launch an external viewer.
* 2) instance of InputStream:
* Bring up the "Save to disk" dialog page to allow the content
* to be saved to disk.
* 3) instance of InputStreamImageSource:
* Load the image into HotJava in an image viewer page.
* 4) instance of String:
* Go to a new page with the string as the plain text content
* of that page.
*/
public Object getContent(URLConnection uc) {
try {
InputStream is = uc.getInputStream();
StringBuffer sb = new StringBuffer();
int c;
sb.append("[Content of " + uc.getURL() + "]\n\n");
sb.append("[This opening message brought to you by your plain/text\n");
sb.append("content handler. To remove this content handler, delete the\n");
sb.append("COM.foo.content.text directory from your class path and\n");
sb.append("the java.content.handler.pkgs property from your HotJava\n");
sb.append("properties file.]\n");
sb.append("----------------------------------------------------------------\n\n");
// Read the characters from the source, accumulate them into the string buffer.
// (Not the most efficient, but simplest for this example.)
while ((c = is.read()) >= 0) {
sb.append((char)c);
}
// Tidy up
is.close();
// Return the resulting string to our client (we're case 4 above)
return sb.toString();
} catch (IOException e) {
// For any exception, just return an indication of what went wrong.
return "Problem reading document: " + uc.getURL();
}
}
/**
* Returns one of several object types (this set may change in future
* versions):
* 1) instance of Thread:
* Invoke the thread to launch an external viewer.
* 2) instance of InputStream:
* Bring up the "Save to disk" dialog page to allow the content
* to be saved to disk.
* 3) instance of InputStreamImageSource:
* Load the image into HotJava in an image viewer page.
* 4) instance of String:
* Go to a new page with the string as the plain text content
* of that page.
*/
public Object getContent(URLConnection uc) {
try {
InputStream is = uc.getInputStream();
StringBuffer sb = new StringBuffer();
int c;
sb.append("[Content of " + uc.getURL() + "]\n\n");
sb.append("[This opening message brought to you by your plain/text\n");
sb.append("content handler. To remove this content handler, delete the\n");
sb.append("COM.foo.content.text directory from your class path and\n");
sb.append("the java.content.handler.pkgs property from your HotJava\n");
sb.append("properties file.]\n");
sb.append("----------------------------------------------------------------\n\n");
// Read the characters from the source, accumulate them into the string buffer.
// (Not the most efficient, but simplest for this example.)
while ((c = is.read()) >= 0) {
sb.append((char)c);
}
// Tidy up
is.close();
// Return the resulting string to our client (we're case 4 above)
return sb.toString();
} catch (IOException e) {
// For any exception, just return an indication of what went wrong.
return "Problem reading document: " + uc.getURL();
}
}
/**
* Prior to opening a URLConnection, calling this method will set all unexpired cookies that match the path or subpaths for thi
* underlying URL The connection MUST NOT have been opened method or an IOException will be thrown.
*
* @param conn a java.net.URLConnection - must NOT be open, or IOException will be thrown
* @throws java.io.IOException Thrown if conn has already been opened.
*/
public void setCookies(URLConnection conn) throws IOException {
// let's determine the domain and path to retrieve the appropriate cookies
URL url = conn.getURL();
String domain = getDomainFromHost(url.getHost());
String path = url.getPath();
Map domainStore = (Map) store.get(domain);
if (domainStore == null)
return;
StringBuffer cookieStringBuffer = new StringBuffer();
Iterator cookieNames = domainStore.keySet().iterator();
while (cookieNames.hasNext()) {
String cookieName = (String) cookieNames.next();
Map cookie = (Map) domainStore.get(cookieName);
// check cookie to ensure path matches and cookie is not expired
// if all is cool, add cookie to header string
if (comparePaths((String) cookie.get(PATH), path) && isNotExpired((String) cookie.get(EXPIRES))) {
cookieStringBuffer.append(cookieName);
cookieStringBuffer.append("=");
cookieStringBuffer.append((String) cookie.get(cookieName));
if (cookieNames.hasNext())
cookieStringBuffer.append(SET_COOKIE_SEPARATOR);
}
}
try {
conn.setRequestProperty(COOKIE, cookieStringBuffer.toString());
} catch (java.lang.IllegalStateException ise) {
IOException ioe = new IOException("Illegal State! Cookies cannot be set on a URLConnection that is already connected. " + "Only call setCookies(java.net.URLConnection) AFTER calling java.net.URLConnection.connect().");
throw ioe;
}
}
/**
* Prior to opening a URLConnection, calling this method will set all unexpired cookies that match the path or subpaths for thi
* underlying URL The connection MUST NOT have been opened method or an IOException will be thrown.
*
* @param conn a java.net.URLConnection - must NOT be open, or IOException will be thrown
* @throws java.io.IOException Thrown if conn has already been opened.
*/
public void setCookies(URLConnection conn) throws IOException {
// let's determine the domain and path to retrieve the appropriate cookies
URL url = conn.getURL();
String domain = getDomainFromHost(url.getHost());
String path = url.getPath();
Map domainStore = (Map) store.get(domain);
if (domainStore == null)
return;
StringBuffer cookieStringBuffer = new StringBuffer();
Iterator cookieNames = domainStore.keySet().iterator();
while (cookieNames.hasNext()) {
String cookieName = (String) cookieNames.next();
Map cookie = (Map) domainStore.get(cookieName);
// check cookie to ensure path matches and cookie is not expired
// if all is cool, add cookie to header string
if (comparePaths((String) cookie.get(PATH), path) && isNotExpired((String) cookie.get(EXPIRES))) {
cookieStringBuffer.append(cookieName);
cookieStringBuffer.append("=");
cookieStringBuffer.append((String) cookie.get(cookieName));
if (cookieNames.hasNext())
cookieStringBuffer.append(SET_COOKIE_SEPARATOR);
}
}
try {
conn.setRequestProperty(COOKIE, cookieStringBuffer.toString());
} catch (java.lang.IllegalStateException ise) {
IOException ioe = new IOException("Illegal State! Cookies cannot be set on a URLConnection that is already connected. " + "Only call setCookies(java.net.URLConnection) AFTER calling java.net.URLConnection.connect().");
throw ioe;
}
}
/**
* Returns one of several object types (this set may change in future
* versions):
* 1) instance of Thread:
* Invoke the thread to launch an external viewer.
* 2) instance of InputStream:
* Bring up the "Save to disk" dialog page to allow the content
* to be saved to disk.
* 3) instance of InputStreamImageSource:
* Load the image into HotJava in an image viewer page.
* 4) instance of String:
* Go to a new page with the string as the plain text content
* of that page.
*/
public Object getContent(URLConnection uc) {
try {
InputStream is = uc.getInputStream();
StringBuffer sb = new StringBuffer();
int c;
sb.append("[Content of " + uc.getURL() + "]\n\n");
sb.append("[This opening message brought to you by your plain/text\n");
sb.append("content handler. To remove this content handler, delete the\n");
sb.append("COM.foo.content.text directory from your class path and\n");
sb.append("the java.content.handler.pkgs property from your HotJava\n");
sb.append("properties file.]\n");
sb.append("----------------------------------------------------------------\n\n");
// Read the characters from the source, accumulate them into the string buffer.
// (Not the most efficient, but simplest for this example.)
while ((c = is.read()) >= 0) {
sb.append((char)c);
}
// Tidy up
is.close();
// Return the resulting string to our client (we're case 4 above)
return sb.toString();
} catch (IOException e) {
// For any exception, just return an indication of what went wrong.
return "Problem reading document: " + uc.getURL();
}
}
@Override
public long getLastModified() throws IOException {
URLConnection con = getResourceURLConnection(name, classLoader);
if (con == null) {
return -1;
}
try {
long lastModified = con.getLastModified();
resolvedURL = con.getURL();
LOGGER.debug("{} last modified: {}", name, lastModified);
return lastModified;
} finally {
closeURLConnection(con);
}
}
public URL getURL(String name, ClassLoader classLoader) throws IOException {
URLConnection con = getResourceURLConnection(name, classLoader);
if (con == null) {
return null;
}
try {
return con.getURL();
} finally {
closeURLConnection(con);
}
}
/**
* Returns one of several object types (this set may change in future
* versions):
* 1) instance of Thread:
* Invoke the thread to launch an external viewer.
* 2) instance of InputStream:
* Bring up the "Save to disk" dialog page to allow the content
* to be saved to disk.
* 3) instance of InputStreamImageSource:
* Load the image into HotJava in an image viewer page.
* 4) instance of String:
* Go to a new page with the string as the plain text content
* of that page.
*/
public Object getContent(URLConnection uc) {
try {
InputStream is = uc.getInputStream();
StringBuffer sb = new StringBuffer();
int c;
sb.append("[Content of " + uc.getURL() + "]\n\n");
sb.append("[This opening message brought to you by your plain/text\n");
sb.append("content handler. To remove this content handler, delete the\n");
sb.append("COM.foo.content.text directory from your class path and\n");
sb.append("the java.content.handler.pkgs property from your HotJava\n");
sb.append("properties file.]\n");
sb.append("----------------------------------------------------------------\n\n");
// Read the characters from the source, accumulate them into the string buffer.
// (Not the most efficient, but simplest for this example.)
while ((c = is.read()) >= 0) {
sb.append((char)c);
}
// Tidy up
is.close();
// Return the resulting string to our client (we're case 4 above)
return sb.toString();
} catch (IOException e) {
// For any exception, just return an indication of what went wrong.
return "Problem reading document: " + uc.getURL();
}
}
/**
* Returns one of several object types (this set may change in future
* versions):
* 1) instance of Thread:
* Invoke the thread to launch an external viewer.
* 2) instance of InputStream:
* Bring up the "Save to disk" dialog page to allow the content
* to be saved to disk.
* 3) instance of InputStreamImageSource:
* Load the image into HotJava in an image viewer page.
* 4) instance of String:
* Go to a new page with the string as the plain text content
* of that page.
*/
public Object getContent(URLConnection uc) {
try {
InputStream is = uc.getInputStream();
StringBuffer sb = new StringBuffer();
int c;
sb.append("[Content of " + uc.getURL() + "]\n\n");
sb.append("[This opening message brought to you by your plain/text\n");
sb.append("content handler. To remove this content handler, delete the\n");
sb.append("COM.foo.content.text directory from your class path and\n");
sb.append("the java.content.handler.pkgs property from your HotJava\n");
sb.append("properties file.]\n");
sb.append("----------------------------------------------------------------\n\n");
// Read the characters from the source, accumulate them into the string buffer.
// (Not the most efficient, but simplest for this example.)
while ((c = is.read()) >= 0) {
sb.append((char)c);
}
// Tidy up
is.close();
// Return the resulting string to our client (we're case 4 above)
return sb.toString();
} catch (IOException e) {
// For any exception, just return an indication of what went wrong.
return "Problem reading document: " + uc.getURL();
}
}
/**
* Returns one of several object types (this set may change in future
* versions):
* 1) instance of Thread:
* Invoke the thread to launch an external viewer.
* 2) instance of InputStream:
* Bring up the "Save to disk" dialog page to allow the content
* to be saved to disk.
* 3) instance of InputStreamImageSource:
* Load the image into HotJava in an image viewer page.
* 4) instance of String:
* Go to a new page with the string as the plain text content
* of that page.
*/
public Object getContent(URLConnection uc) {
try {
InputStream is = uc.getInputStream();
StringBuffer sb = new StringBuffer();
int c;
sb.append("[Content of " + uc.getURL() + "]\n\n");
sb.append("[This opening message brought to you by your plain/text\n");
sb.append("content handler. To remove this content handler, delete the\n");
sb.append("COM.foo.content.text directory from your class path and\n");
sb.append("the java.content.handler.pkgs property from your HotJava\n");
sb.append("properties file.]\n");
sb.append("----------------------------------------------------------------\n\n");
// Read the characters from the source, accumulate them into the string buffer.
// (Not the most efficient, but simplest for this example.)
while ((c = is.read()) >= 0) {
sb.append((char)c);
}
// Tidy up
is.close();
// Return the resulting string to our client (we're case 4 above)
return sb.toString();
} catch (IOException e) {
// For any exception, just return an indication of what went wrong.
return "Problem reading document: " + uc.getURL();
}
}
/**
* Returns one of several object types (this set may change in future
* versions):
* 1) instance of Thread:
* Invoke the thread to launch an external viewer.
* 2) instance of InputStream:
* Bring up the "Save to disk" dialog page to allow the content
* to be saved to disk.
* 3) instance of InputStreamImageSource:
* Load the image into HotJava in an image viewer page.
* 4) instance of String:
* Go to a new page with the string as the plain text content
* of that page.
*/
public Object getContent(URLConnection uc) {
try {
InputStream is = uc.getInputStream();
StringBuffer sb = new StringBuffer();
int c;
sb.append("[Content of " + uc.getURL() + "]\n\n");
sb.append("[This opening message brought to you by your plain/text\n");
sb.append("content handler. To remove this content handler, delete the\n");
sb.append("COM.foo.content.text directory from your class path and\n");
sb.append("the java.content.handler.pkgs property from your HotJava\n");
sb.append("properties file.]\n");
sb.append("----------------------------------------------------------------\n\n");
// Read the characters from the source, accumulate them into the string buffer.
// (Not the most efficient, but simplest for this example.)
while ((c = is.read()) >= 0) {
sb.append((char)c);
}
// Tidy up
is.close();
// Return the resulting string to our client (we're case 4 above)
return sb.toString();
} catch (IOException e) {
// For any exception, just return an indication of what went wrong.
return "Problem reading document: " + uc.getURL();
}
}
public URLImageSource(URLConnection uc) {
this(uc.getURL(), uc);
}
@Override
public R handleStream (final URLConnection connection, final InputStream is, final int contentLength) throws IOException {
this.url = connection.getURL();
this.contentLength = contentLength;
return this.delagate.handleStream(connection, is, contentLength);
}
public URLImageSource(URLConnection uc) {
this(uc.getURL(), uc);
}
public URLImageSource(URLConnection uc) {
this(uc.getURL(), uc);
}
public URLImageSource(URLConnection uc) {
this(uc.getURL(), uc);
}
public URLImageSource(URLConnection uc) {
this(uc.getURL(), uc);
}
public URLImageSource(URLConnection uc) {
this(uc.getURL(), uc);
}