本文整理汇总了Java中java.net.URL.getContent方法的典型用法代码示例。如果您正苦于以下问题:Java URL.getContent方法的具体用法?Java URL.getContent怎么用?Java URL.getContent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.URL
的用法示例。
在下文中一共展示了URL.getContent方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getImage
import java.net.URL; //导入方法依赖的package包/类
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;
}
}
示例2: fetchContent
import java.net.URL; //导入方法依赖的package包/类
static String fetchContent(URL url) {
java.io.Reader reader = null;
try {
reader = new java.io.InputStreamReader((java.io.InputStream) url.getContent());
StringBuilder content = new StringBuilder();
char[] buf = new char[1024];
for (int n = reader.read(buf); n > -1; n = reader.read(buf))
content.append(buf, 0, n);
return content.toString();
} catch (Exception ex) {
throw new RuntimeException(ex);
} finally {
if (reader != null) try {
reader.close();
} catch (Throwable t) {
}
}
}
示例3: addVersionInfo
import java.net.URL; //导入方法依赖的package包/类
public void addVersionInfo() {
Properties props = new Properties();
// when running from webstart we are not allowed to open a file on the local file system, but we can
// get a the contents of a resource, which in this case is the echo'ed date stamp written by ant on the last build
ClassLoader cl = this.getClass().getClassLoader(); // get this class'es class loader
addLogInfo("\nLoading version info from resource " + AEViewerAboutDialog.VERSION_FILE);
URL versionURL = cl.getResource(AEViewerAboutDialog.VERSION_FILE); // get a URL to the time stamp file
addLogInfo("\nVersion URL=" + versionURL + "\n");
if (versionURL != null) {
try {
Object urlContents = versionURL.getContent();
BufferedReader in = null;
if (urlContents instanceof InputStream) {
props.load((InputStream) urlContents);
}
} catch (Exception e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
PrintWriter ps = new PrintWriter(baos);
props.list(ps);
ps.flush();
try {
addLogInfo("\n" + baos.toString("UTF-8"));
} catch (UnsupportedEncodingException ex) {
System.err.println("cannot encode version information in LoggingWindow.addVersionInfo: " + ex.toString());
}
} else {
props.setProperty("version", "missing file " + AEViewerAboutDialog.VERSION_FILE + " in jAER.jar");
}
}
示例4: doInBackground
import java.net.URL; //导入方法依赖的package包/类
@Override
protected ArrayList<Bitmap> doInBackground(URL... params) {
URL[] urls = new URL[] {params[0], params[1], params[2]};
ArrayList<Bitmap> bitmaps = new ArrayList<>();
for(URL url : urls) {
Bitmap bitmap = null;
if(url != null) {
try {
InputStream is = (InputStream) url.getContent();
bitmap = BitmapFactory.decodeStream(is);
} catch (IOException e) {
e.printStackTrace();
}
}
bitmaps.add(bitmap);
}
return bitmaps;
}
示例5: getClassMetadata
import java.net.URL; //导入方法依赖的package包/类
public static ClassMetadata getClassMetadata(String className) throws IOException {
String path = className.replace('.','/') + ".class";
URL url = Thread.currentThread().getContextClassLoader().getResource(path);
ClassReader cr = null;
if(url != null && "file".equals(url.getProtocol())){
try(InputStream in = (InputStream) url.getContent()) {
cr = new ClassReader(in);
}
}else {
URL realUrl = new URL(SourceCodeHelper.getJarLocationByPath(url));
try (ZipInputStream zip = new ZipInputStream(realUrl.openStream())) {
ZipEntry entry;
while ((entry = zip.getNextEntry()) != null) {
if (entry.getName().equalsIgnoreCase(path)) {
cr = new ClassReader(zip);
break;
}
}
}
}
if(cr != null) {
ClassMetadata metadata = new ClassMetadata();
cr.accept(new MetadataCollector(metadata), ClassReader.SKIP_FRAMES);
return metadata;
}else {
return null;
}
}
示例6: fetch
import java.net.URL; //导入方法依赖的package包/类
private InputStream fetch(String urlString) throws IOException {
URL url;
final MyHtmlHttpImageGetter imageGetter = imageGetterReference.get();
if (imageGetter == null) {
return null;
}
if (imageGetter.baseUri != null) {
url = imageGetter.baseUri.resolve(urlString).toURL();
} else {
url = URI.create(urlString).toURL();
}
return (InputStream) url.getContent();
}
示例7: fetch
import java.net.URL; //导入方法依赖的package包/类
private InputStream fetch(String urlString) throws IOException {
URL url;
final HtmlHttpImageGetter imageGetter = imageGetterReference.get();
if (imageGetter == null) {
return null;
}
if (imageGetter.baseUri != null) {
url = imageGetter.baseUri.resolve(urlString).toURL();
} else {
url = URI.create(urlString).toURL();
}
return (InputStream) url.getContent();
}
示例8: getAudioClip
import java.net.URL; //导入方法依赖的package包/类
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;
}
}
示例9: loadImage
import java.net.URL; //导入方法依赖的package包/类
/**
* 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;
}
示例10: loadLocalPDFFile
import java.net.URL; //导入方法依赖的package包/类
@When("the user load '$path' file with key '$key'")
public void loadLocalPDFFile(String path, String key) throws URISyntaxException {
URL pdfFile = FilePreconditionsSteps.class.getResource(path);
try {
Object content = pdfFile.getContent();
URI uri = pdfFile.toURI();
File file = new File(uri);
Thucydides.getCurrentSession().put(key, file);
} catch (IOException e) {
e.printStackTrace();
}
}
示例11: loadImage
import java.net.URL; //导入方法依赖的package包/类
/**
* 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;
}
示例12: testGetContent
import java.net.URL; //导入方法依赖的package包/类
@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");
}
}
示例13: main
import java.net.URL; //导入方法依赖的package包/类
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.");
}