java.net.URL.openStream()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(7.9k)|赞(0)|评价(0)|浏览(792)

本文整理了Java中java.net.URL.openStream()方法的一些代码示例,展示了URL.openStream()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。URL.openStream()方法的具体详情如下:
包路径:java.net.URL
类名称:URL
方法名:openStream

URL.openStream介绍

[英]Equivalent to openConnection().getInputStream(types).
[中]相当于openConnection()。getInputStream(类型)。

代码示例

代码示例来源:origin: stackoverflow.com

InputStream response = new URL(url).openStream();
// ...

代码示例来源:origin: jenkinsci/jenkins

public boolean hasLicensesXml() {
  try {
    new URL(baseResourceURL,"WEB-INF/licenses.xml").openStream().close();
    return true;
  } catch (IOException e) {
    return false;
  }
}

代码示例来源:origin: stackoverflow.com

import java.net.*;
import java.io.*;

URL whatismyip = new URL("http://checkip.amazonaws.com");
BufferedReader in = new BufferedReader(new InputStreamReader(
        whatismyip.openStream()));

String ip = in.readLine(); //you get the IP as a String
System.out.println(ip);

代码示例来源:origin: stackoverflow.com

public static void main(String[] args) throws Exception {
  String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
  String search = "stackoverflow";
  String charset = "UTF-8";

  URL url = new URL(google + URLEncoder.encode(search, charset));
  Reader reader = new InputStreamReader(url.openStream(), charset);
  GoogleResults results = new Gson().fromJson(reader, GoogleResults.class);

  // Show title and URL of 1st result.
  System.out.println(results.getResponseData().getResults().get(0).getTitle());
  System.out.println(results.getResponseData().getResults().get(0).getUrl());
}

代码示例来源:origin: btraceio/btrace

@Test
  @SuppressWarnings("DefaultCharset")
  public void testCompile() throws Exception {
    System.out.println("compile");
    URL script = CompilerTest.class.getResource("/traces/OnMethodTest.java");
    InputStream is = script.openStream();

    File f = new File(script.toURI());
    File base = f.getParentFile().getParentFile().getParentFile();

    String classpath = base.getAbsolutePath() + "/main" + ":" + base.getAbsolutePath() + "/test";

    BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = br.readLine()) != null) {
      sb.append(line).append('\n');
    }

    System.out.println(sb.toString());
    Map<String, byte[]> data = instance.compile("traces/OnMethodTest.java", sb.toString(), new PrintWriter(System.err), ".", classpath);

    Assert.assertNotNull(data);
  }
}

代码示例来源:origin: apache/ignite

/**
 * {@code keyStorePath} could be absolute path or path to classpath resource.
 *
 * @return File for {@code keyStorePath}.
 */
private InputStream keyStoreFile() throws IOException {
  File abs = new File(keyStorePath);
  if (abs.exists())
    return new FileInputStream(abs);
  URL clsPthRes = KeystoreEncryptionSpi.class.getClassLoader().getResource(keyStorePath);
  if (clsPthRes != null)
    return clsPthRes.openStream();
  return null;
}

代码示例来源:origin: stackoverflow.com

InputStream in = new URL( "http://jakarta.apache.org" ).openStream();
try {
 System.out.println( IOUtils.toString( in ) );
} finally {
 IOUtils.closeQuietly(in);
}

代码示例来源:origin: jenkinsci/jenkins

public String call() throws IOException {
    File f = new File(script);
    if(f.exists())
      return FileUtils.readFileToString(f);

    URL url;
    try {
      url = new URL(script);
    } catch (MalformedURLException e) {
      throw new AbortException("Unable to find a script "+script);
    }
    try (InputStream s = url.openStream()) {
      return IOUtils.toString(s);
    }
  }
}

代码示例来源:origin: redisson/redisson

public <T> T fromYAML(URL url, Class<T> configType) throws IOException {
  String content = resolveEnvParams(new InputStreamReader(url.openStream()));
  return yamlMapper.readValue(content, configType);
}

代码示例来源:origin: eclipse-vertx/vert.x

protected String getCommandFromManifest() {
 try {
  Enumeration<URL> resources = RunCommand.class.getClassLoader().getResources("META-INF/MANIFEST.MF");
  while (resources.hasMoreElements()) {
   InputStream stream = resources.nextElement().openStream();
   Manifest manifest = new Manifest(stream);
   Attributes attributes = manifest.getMainAttributes();
   String mainClass = attributes.getValue("Main-Class");
   if (main.getClass().getName().equals(mainClass)) {
    String command = attributes.getValue("Main-Command");
    if (command != null) {
     stream.close();
     return command;
    }
   }
   stream.close();
  }
 } catch (IOException e) {
  throw new IllegalStateException(e.getMessage());
 }
 return null;
}

代码示例来源:origin: weibocom/motan

/**
 * Parses the JSON input file containing the list of features.
 */
public static List<Feature> parseFeatures(URL file) throws IOException {
 InputStream input = file.openStream();
 try {
  Reader reader = new InputStreamReader(input);
  try {
   FeatureDatabase.Builder database = FeatureDatabase.newBuilder();
   JsonFormat.parser().merge(reader, database);
   return database.getFeatureList();
  } finally {
   reader.close();
  }
 } finally {
  input.close();
 }
}

代码示例来源:origin: Swagger2Markup/swagger2markup

/**
 * Create a reader from specified {@code source}.<br>
 * Returned reader should be explicitly closed after use.
 *
 * @param uri source URI
 * @return reader
 * @throws IOException if the connection cannot be opened
 */
public static Reader uriReader(URI uri) throws IOException {
  return new BufferedReader(new InputStreamReader(uri.toURL().openStream(), StandardCharsets.UTF_8));
}

代码示例来源:origin: stackoverflow.com

InputStream input = new URL("http://example.com/foo.json").openStream();
Reader reader = new InputStreamReader(input, "UTF-8");
Data data = new Gson().fromJson(reader, Data.class);
// ...

代码示例来源:origin: stackoverflow.com

URL url = new URL("http://stackoverflow.com");

try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
  for (String line; (line = reader.readLine()) != null;) {
    System.out.println(line);
  }
}

代码示例来源:origin: stackoverflow.com

URL url = new URL ("file://some/path/anImage.png");
InputStream input = url.openStream();
try {
  //The sdcard directory e.g. '/sdcard' can be used directly, or 
  //more safely abstracted with getExternalStorageDirectory()
  File storagePath = Environment.getExternalStorageDirectory();
  OutputStream output = new FileOutputStream (new File(storagePath,"myImage.png"));
  try {
    byte[] buffer = new byte[aReasonableSize];
    int bytesRead = 0;
    while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
      output.write(buffer, 0, bytesRead);
    }
  } finally {
    output.close();
  }
} finally {
  input.close();
}

代码示例来源:origin: apache/avro

private Idl(URL input, Idl parent) throws IOException {
 this(input.openStream(), "UTF-8");
 this.inputDir = "file".equals(input.getProtocol())
  ? new File(input.getPath()).getParentFile()
  : parent.inputDir;
 this.resourceLoader = parent.resourceLoader;
}

代码示例来源:origin: stackoverflow.com

String url = "http://stackoverflow.com/questions/3152138";
Document document = new Tidy().parseDOM(new URL(url).openStream(), null);
XPath xpath = XPathFactory.newInstance().newXPath();

Node question = (Node) xpath.compile("//*[@id='question']//*[contains(@class,'post-text')]//p[1]").evaluate(document, XPathConstants.NODE);
System.out.println("Question: " + question.getFirstChild().getNodeValue());

NodeList answerers = (NodeList) xpath.compile("//*[@id='answers']//*[contains(@class,'user-details')]//a[1]").evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < answerers.getLength(); i++) {
  System.out.println("Answerer: " + answerers.item(i).getFirstChild().getNodeValue());
}

代码示例来源:origin: cmusphinx/sphinx4

public KaldiTextParser(String path)
  throws IOException, MalformedURLException
{
  // TODO: rewrite with StreamTokenizer, see ExtendedStreamTokenizer.
  File modelFile = new File(path, "final.mdl");
  InputStream modelStream = new URL(modelFile.getPath()).openStream();
  File treeFile = new File(path, "tree");
  InputStream treeStream = new URL(treeFile.getPath()).openStream();
  InputStream stream = new SequenceInputStream(modelStream, treeStream);
  scanner = new Scanner(stream);
}

代码示例来源:origin: Netflix/eureka

private static Manifest loadManifest(String jarUrl) throws Exception {
  InputStream is = new URL(jarUrl + "!/META-INF/MANIFEST.MF").openStream();
  try {
    return new Manifest(is);
  } finally {
    is.close();
  }
}

代码示例来源:origin: dropwizard/dropwizard

@Override
  public InputStream open(String path) throws IOException {
    return new URL(path).openStream();
  }
}

相关文章