下面列出了java.net.URL#getContent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public synchronized Image getImage(URL url) {
Object o = imageCache.get(url);
if (o != null) {
return (Image)o;
}
try {
o = url.getContent();
if (o == null) {
return null;
}
if (o instanceof Image) {
imageCache.put(url, o);
return (Image) o;
}
// Otherwise it must be an ImageProducer.
Image img = target.createImage((java.awt.image.ImageProducer)o);
imageCache.put(url, img);
return img;
} catch (Exception ex) {
return null;
}
}
public synchronized Image getImage(URL url) {
Object o = imageCache.get(url);
if (o != null) {
return (Image)o;
}
try {
o = url.getContent();
if (o == null) {
return null;
}
if (o instanceof Image) {
imageCache.put(url, o);
return (Image) o;
}
// Otherwise it must be an ImageProducer.
Image img = target.createImage((java.awt.image.ImageProducer)o);
imageCache.put(url, img);
return img;
} catch (Exception ex) {
return null;
}
}
/**
* Preview the image when it is a local image file.
*
* @param uri
* Image absolute path without "file:///"
*/
private ImageStatus preview( String uri )
{
hasUriImagePreviewed=true;
try
{
URL url = new URL( uri );
// there's no need to enable the ok button when processing
url.getContent( );
if ( previewCanvas.loadImage( url ) != null )
{
emptyExceptionText();
}
return ImageStatus.IMAGE_CAN_DISPLAY;
}
catch (MalformedURLException malEx){
showMessage(Messages.getString( "MarkerIconDialog.Exception.InvalidURL" ));//$NON-NLS-1$
return ImageStatus.IMAGE_URL_INVALID;
}
catch ( Exception ex )
{
showMessage(Messages.getString("MarkerIconDialog.Exception.ImageNotAvailable")); //$NON-NLS-1$
return ImageStatus.IMAGE_CANNOT_DISPLAY;
}
}
@Override
public File loadDataFromNetwork() throws IOException {
int bytesRead;
URL url = new URL(source);
File destFile = new File(destinationFolder, Uri.parse(source).getLastPathSegment());
FileOutputStream fileOutput = new FileOutputStream(destFile);
InputStream inputStream = (InputStream) url.getContent();
byte[] buffer = new byte[MAX_BUFFER_SIZE];
while ((bytesRead = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bytesRead);
}
fileOutput.close();
return destFile;
}
public AudioClip getAudioClip(URL url) {
// We don't currently support audio clips in the Beans.instantiate
// applet context, unless by some luck there exists a URL content
// class that can generate an AudioClip from the audio URL.
try {
return (AudioClip) url.getContent();
} catch (Exception ex) {
return null;
}
}
public AudioClip getAudioClip(URL url) {
// We don't currently support audio clips in the Beans.instantiate
// applet context, unless by some luck there exists a URL content
// class that can generate an AudioClip from the audio URL.
try {
return (AudioClip) url.getContent();
} catch (Exception ex) {
return null;
}
}
/**
* This is a utility method to help in loading icon images.
* It takes the name of a resource file associated with the
* current object's class file and loads an image object
* from that file. Typically images will be GIFs.
* <p>
* @param resourceName A pathname relative to the directory
* holding the class file of the current class. For example,
* "wombat.gif".
* @return an image object. May be null if the load failed.
*/
public Image loadImage(final String resourceName) {
try {
final URL url = getClass().getResource(resourceName);
if (url != null) {
final ImageProducer ip = (ImageProducer) url.getContent();
if (ip != null) {
return Toolkit.getDefaultToolkit().createImage(ip);
}
}
} catch (final Exception ignored) {
}
return null;
}
public AudioClip getAudioClip(URL url) {
// We don't currently support audio clips in the Beans.instantiate
// applet context, unless by some luck there exists a URL content
// class that can generate an AudioClip from the audio URL.
try {
return (AudioClip) url.getContent();
} catch (Exception ex) {
return null;
}
}
/**
* This is a utility method to help in loading icon images.
* It takes the name of a resource file associated with the
* current object's class file and loads an image object
* from that file. Typically images will be GIFs.
* <p>
* @param resourceName A pathname relative to the directory
* holding the class file of the current class. For example,
* "wombat.gif".
* @return an image object. May be null if the load failed.
*/
public Image loadImage(final String resourceName) {
try {
final URL url = getClass().getResource(resourceName);
if (url != null) {
final ImageProducer ip = (ImageProducer) url.getContent();
if (ip != null) {
return Toolkit.getDefaultToolkit().createImage(ip);
}
}
} catch (final Exception ignored) {
}
return null;
}
@Test(dataProvider = "urls")
public void testGetContent(String urlString, boolean exists) throws Exception {
URL url = new URL(urlString);
try {
Object obj = url.getContent();
assertTrue(obj != null);
if (!exists) fail("IOException expected");
} catch (IOException ioe) {
if (exists) fail("IOException not expected");
}
}
/**
* This is a utility method to help in loading icon images.
* It takes the name of a resource file associated with the
* current object's class file and loads an image object
* from that file. Typically images will be GIFs.
* <p>
* @param resourceName A pathname relative to the directory
* holding the class file of the current class. For example,
* "wombat.gif".
* @return an image object. May be null if the load failed.
*/
public Image loadImage(final String resourceName) {
try {
final URL url = getClass().getResource(resourceName);
if (url != null) {
final ImageProducer ip = (ImageProducer) url.getContent();
if (ip != null) {
return Toolkit.getDefaultToolkit().createImage(ip);
}
}
} catch (final Exception ignored) {
}
return null;
}
public AudioClip getAudioClip(URL url) {
// We don't currently support audio clips in the Beans.instantiate
// applet context, unless by some luck there exists a URL content
// class that can generate an AudioClip from the audio URL.
try {
return (AudioClip) url.getContent();
} catch (Exception ex) {
return null;
}
}
public AudioClip getAudioClip(URL url) {
// We don't currently support audio clips in the Beans.instantiate
// applet context, unless by some luck there exists a URL content
// class that can generate an AudioClip from the audio URL.
try {
return (AudioClip) url.getContent();
} catch (Exception ex) {
return null;
}
}
public AudioClip getAudioClip(URL url) {
// We don't currently support audio clips in the Beans.instantiate
// applet context, unless by some luck there exists a URL content
// class that can generate an AudioClip from the audio URL.
try {
return (AudioClip) url.getContent();
} catch (Exception ex) {
return null;
}
}
/**
* This is a utility method to help in loading icon images.
* It takes the name of a resource file associated with the
* current object's class file and loads an image object
* from that file. Typically images will be GIFs.
* <p>
* @param resourceName A pathname relative to the directory
* holding the class file of the current class. For example,
* "wombat.gif".
* @return an image object. May be null if the load failed.
*/
public Image loadImage(final String resourceName) {
try {
final URL url = getClass().getResource(resourceName);
if (url != null) {
final ImageProducer ip = (ImageProducer) url.getContent();
if (ip != null) {
return Toolkit.getDefaultToolkit().createImage(ip);
}
}
} catch (final Exception ignored) {
}
return null;
}
/**
* This is a utility method to help in loading icon images. It takes the
* name of a resource file associated with the current object's class file
* and loads an image object from that file. Typically images will be GIFs.
*
* @param resourceName A pathname relative to the directory holding the
* class file of the current class. For example, "wombat.gif".
* @return an image object or null if the resource is not found or the
* resource could not be loaded as an Image
*/
public Image loadImage(final String resourceName) {
try {
final URL url = getClass().getResource(resourceName);
if (url != null) {
final ImageProducer ip = (ImageProducer) url.getContent();
if (ip != null) {
return Toolkit.getDefaultToolkit().createImage(ip);
}
}
} catch (final Exception ignored) {
}
return null;
}
/**
* This is a utility method to help in loading icon images.
* It takes the name of a resource file associated with the
* current object's class file and loads an image object
* from that file. Typically images will be GIFs.
* <p>
* @param resourceName A pathname relative to the directory
* holding the class file of the current class. For example,
* "wombat.gif".
* @return an image object. May be null if the load failed.
*/
public Image loadImage(final String resourceName) {
try {
final URL url = getClass().getResource(resourceName);
if (url != null) {
final ImageProducer ip = (ImageProducer) url.getContent();
if (ip != null) {
return Toolkit.getDefaultToolkit().createImage(ip);
}
}
} catch (final Exception ignored) {
}
return null;
}
public AudioClip getAudioClip(URL url) {
// We don't currently support audio clips in the Beans.instantiate
// applet context, unless by some luck there exists a URL content
// class that can generate an AudioClip from the audio URL.
try {
return (AudioClip) url.getContent();
} catch (Exception ex) {
return null;
}
}
/**
* This is a utility method to help in loading icon images.
* It takes the name of a resource file associated with the
* current object's class file and loads an image object
* from that file. Typically images will be GIFs.
* <p>
* @param resourceName A pathname relative to the directory
* holding the class file of the current class. For example,
* "wombat.gif".
* @return an image object. May be null if the load failed.
*/
public Image loadImage(final String resourceName) {
try {
final URL url = getClass().getResource(resourceName);
if (url != null) {
final ImageProducer ip = (ImageProducer) url.getContent();
if (ip != null) {
return Toolkit.getDefaultToolkit().createImage(ip);
}
}
} catch (final Exception ignored) {
}
return null;
}
public static void main(String args[]) throws Exception {
URL url = ClassLoader.getSystemResource("foo/bar");
InputStream is = (InputStream) url.getContent();
if (is == null)
throw new Exception("Failed to get content.");
}