java.net.URLConnection.getInputStream()方法的使用及代码示例

x33g5p2x  于2022-01-31 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(499)

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

URLConnection.getInputStream介绍

[英]Returns an InputStream for reading data from the resource pointed by this URLConnection. It throws an UnknownServiceException by default. This method must be overridden by its subclasses.
[中]返回一个InputStream,用于从此URLConnection指向的资源中读取数据。默认情况下,它会抛出UnknownServiceException。此方法必须由其子类重写。

代码示例

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

  1. URLConnection connection = new URL(url + "?" + query).openConnection();
  2. connection.setRequestProperty("Accept-Charset", charset);
  3. InputStream response = connection.getInputStream();
  4. // ...

代码示例来源:origin: apache/incubator-druid

  1. @Override
  2. protected InputStream openObjectStream(URI object) throws IOException
  3. {
  4. return object.toURL().openConnection().getInputStream();
  5. }

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

  1. import java.net.*;
  2. import java.io.*;
  3. public class URLConnectionReader {
  4. public static void main(String[] args) throws Exception {
  5. URL yahoo = new URL("http://www.yahoo.com/");
  6. URLConnection yc = yahoo.openConnection();
  7. BufferedReader in = new BufferedReader(
  8. new InputStreamReader(
  9. yc.getInputStream()));
  10. String inputLine;
  11. while ((inputLine = in.readLine()) != null)
  12. System.out.println(inputLine);
  13. in.close();
  14. }
  15. }

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

  1. /**
  2. * Opens the given URL and reads text content from it.
  3. * This method honors Content-type header.
  4. */
  5. protected BufferedReader open(URL url) throws IOException {
  6. // use HTTP content type to find out the charset.
  7. URLConnection con = ProxyConfiguration.open(url);
  8. if (con == null) { // TODO is this even permitted by URL.openConnection?
  9. throw new IOException(url.toExternalForm());
  10. }
  11. return new BufferedReader(
  12. new InputStreamReader(con.getInputStream(),getCharset(con)));
  13. }

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

  1. URL url = loader.getResource(resourceName);
  2. if (url != null) {
  3. URLConnection connection = url.openConnection();
  4. if (connection != null) {
  5. connection.setUseCaches(false);
  6. stream = connection.getInputStream();
  7. try {
  8. bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
  9. } finally {
  10. stream.close();

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

  1. SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
  2. URL url = new URL("https://gridserver:3049/cgi-bin/ls.py");
  3. HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
  4. conn.setSSLSocketFactory(sslsocketfactory);
  5. InputStream inputstream = conn.getInputStream();
  6. InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
  7. BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
  8. String string = null;
  9. while ((string = bufferedreader.readLine()) != null) {
  10. System.out.println("Received " + string);
  11. }

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

  1. URLConnection connection = new URL(url).openConnection();
  2. connection.setDoOutput(true); // Triggers POST.
  3. connection.setRequestProperty("Accept-Charset", charset);
  4. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
  5. try (OutputStream output = connection.getOutputStream()) {
  6. output.write(query.getBytes(charset));
  7. }
  8. InputStream response = connection.getInputStream();
  9. // ...

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

  1. /**
  2. * Opens the given URL and reads text content from it.
  3. * This method honors Content-type header.
  4. */
  5. protected BufferedReader open(URL url) throws IOException {
  6. // use HTTP content type to find out the charset.
  7. URLConnection con = ProxyConfiguration.open(url);
  8. if (con == null) { // TODO is this even permitted by URL.openConnection?
  9. throw new IOException(url.toExternalForm());
  10. }
  11. return new BufferedReader(
  12. new InputStreamReader(con.getInputStream(),getCharset(con)));
  13. }

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

  1. @Override
  2. public ResourceBundle newBundle(String baseName, Locale locale, String format,
  3. ClassLoader loader, boolean reload) throws IOException {
  4. // The below is a copy of the default implementation.
  5. final String bundleName = toBundleName(baseName, locale);
  6. final String resourceName = toResourceName(bundleName, "properties");
  7. final URL url = loader.getResource(resourceName);
  8. ResourceBundle resourceBundle = null;
  9. if (url != null) {
  10. final URLConnection connection = url.openConnection();
  11. if (connection != null) {
  12. connection.setUseCaches(!reload);
  13. try (Reader streamReader = new InputStreamReader(connection.getInputStream(),
  14. StandardCharsets.UTF_8.name())) {
  15. // Only this line is changed to make it read property files as UTF-8.
  16. resourceBundle = new PropertyResourceBundle(streamReader);
  17. }
  18. }
  19. }
  20. return resourceBundle;
  21. }

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

  1. public Document parse(final DocumentBuilder parser) throws SAXException, IOException {
  2. URLConnection uConn = url.openConnection();
  3. uConn.setUseCaches(false);
  4. InputStream stream = uConn.getInputStream();
  5. try {
  6. InputSource src = new InputSource(stream);
  7. src.setSystemId(url.toString());
  8. return parser.parse(src);
  9. } finally {
  10. stream.close();
  11. }
  12. }
  13. public String toString() {

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

  1. URLConnection connection = new URL("https://www.google.com/search?q=" + query).openConnection();
  2. connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
  3. connection.connect();
  4. BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));
  5. StringBuilder sb = new StringBuilder();
  6. String line;
  7. while ((line = r.readLine()) != null) {
  8. sb.append(line);
  9. }
  10. System.out.println(sb.toString());

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

  1. String name = c.getString(str_url);
  2. URL url_value = new URL(name);
  3. ImageView profile = (ImageView)v.findViewById(R.id.vdo_icon);
  4. if (profile != null) {
  5. Bitmap mIcon1 =
  6. BitmapFactory.decodeStream(url_value.openConnection().getInputStream());
  7. profile.setImageBitmap(mIcon1);
  8. }

代码示例来源:origin: spring-projects/spring-framework

  1. URL url = classLoader.getResource(resourceName);
  2. if (url != null) {
  3. URLConnection connection = url.openConnection();
  4. if (connection != null) {
  5. connection.setUseCaches(false);
  6. is = connection.getInputStream();
  7. String encoding = getDefaultEncoding();
  8. if (encoding != null) {
  9. try (InputStreamReader bundleReader = new InputStreamReader(inputStream, encoding)) {
  10. return loadBundle(bundleReader);

代码示例来源:origin: spring-projects/spring-framework

  1. /**
  2. * This implementation opens an InputStream for the given URL.
  3. * <p>It sets the {@code useCaches} flag to {@code false},
  4. * mainly to avoid jar file locking on Windows.
  5. * @see java.net.URL#openConnection()
  6. * @see java.net.URLConnection#setUseCaches(boolean)
  7. * @see java.net.URLConnection#getInputStream()
  8. */
  9. @Override
  10. public InputStream getInputStream() throws IOException {
  11. URLConnection con = this.url.openConnection();
  12. ResourceUtils.useCachesIfNecessary(con);
  13. try {
  14. return con.getInputStream();
  15. }
  16. catch (IOException ex) {
  17. // Close the HTTP connection (if applicable).
  18. if (con instanceof HttpURLConnection) {
  19. ((HttpURLConnection) con).disconnect();
  20. }
  21. throw ex;
  22. }
  23. }

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

  1. URL sandbox = new URL("https://api.sandbox.paypal.com/v1/oauth2/token");
  2. URLConnection yc = sandbox.openConnection();
  3. BufferedReader in = new BufferedReader(new InputStreamReader(
  4. yc.getInputStream()));
  5. String inputLine;
  6. while ((inputLine = in.readLine()) != null)
  7. System.out.println(inputLine);
  8. in.close();

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

  1. URL url = new URL("http://image10.bizrate-images.com/resize?sq=60&uid=2216744464");
  2. Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
  3. imageView.setImageBitmap(bmp);

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

  1. /**
  2. * Opens a resource of the specified name for reading. Controls caching,
  3. * that is important when the same jar is reloaded using custom classloader.
  4. */
  5. public static InputStream getResourceAsStream(String resourceName, ClassLoader callingClass, boolean useCache) throws IOException {
  6. URL url = getResourceUrl(resourceName, callingClass);
  7. if (url != null) {
  8. URLConnection urlConnection = url.openConnection();
  9. urlConnection.setUseCaches(useCache);
  10. return urlConnection.getInputStream();
  11. }
  12. return null;
  13. }

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

  1. String urlParameters = "param1=a&param2=b&param3=c";
  2. URL url = new URL("http://example.com/index.php");
  3. URLConnection conn = url.openConnection();
  4. conn.setDoOutput(true);
  5. OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
  6. writer.write(urlParameters);
  7. writer.flush();
  8. String line;
  9. BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  10. while ((line = reader.readLine()) != null) {
  11. System.out.println(line);
  12. }
  13. writer.close();
  14. reader.close();

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

  1. ImageView user_picture;
  2. userpicture=(ImageView)findViewById(R.id.userpicture);
  3. URL img_value = null;
  4. img_value = new URL("http://graph.facebook.com/"+id+"/picture?type=large");
  5. Bitmap mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
  6. userpicture.setImageBitmap(mIcon1);

代码示例来源:origin: spring-projects/spring-framework

  1. while (urls.hasMoreElements()) {
  2. URL url = urls.nextElement();
  3. URLConnection con = url.openConnection();
  4. ResourceUtils.useCachesIfNecessary(con);
  5. InputStream is = con.getInputStream();
  6. try {
  7. if (resourceName.endsWith(XML_FILE_EXTENSION)) {

相关文章

URLConnection类方法