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

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

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

URLConnection.getContentLength介绍

[英]Returns the content length in bytes specified by the response header field content-length or -1 if this field is not set.
[中]返回响应头字段content length指定的内容长度(字节),如果未设置此字段,则返回-1。

代码示例

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

  1. HttpURLConnection connection = null;
  2. try {
  3. URL url = new URL(sUrl[0]);
  4. connection = (HttpURLConnection) url.openConnection();
  5. connection.connect();
  6. int fileLength = connection.getContentLength();
  7. input = connection.getInputStream();
  8. output = new FileOutputStream("/sdcard/file_name.extension");
  9. while ((count = input.read(data)) != -1) {

代码示例来源:origin: wiztools/rest-client

  1. private void init() throws IOException {
  2. if(is == null && length == 0) {
  3. URLConnection con = url.openConnection();
  4. is = con.getInputStream();
  5. length = con.getContentLength();
  6. }
  7. }

代码示例来源:origin: perwendel/spark

  1. @Override
  2. public long contentLength() throws IOException {
  3. URL url = getURL();
  4. if (ResourceUtils.isFileURL(url)) {
  5. // Proceed with file system resolution...
  6. return getFile().length();
  7. } else {
  8. // Try a URL connection content-length header...
  9. URLConnection con = url.openConnection();
  10. customizeConnection(con);
  11. return con.getContentLength();
  12. }
  13. }

代码示例来源:origin: bytedeco/javacpp

  1. String s = System.getProperty("org.bytedeco.javacpp.cachedir.nosubdir", "false").toLowerCase();
  2. boolean noSubdir = s.equals("true") || s.equals("t") || s.equals("");
  3. URLConnection urlConnection = resourceURL.openConnection();
  4. if (urlConnection instanceof JarURLConnection) {
  5. JarFile jarFile = ((JarURLConnection)urlConnection).getJarFile();
  6. size = urlConnection.getContentLength();
  7. timestamp = urlConnection.getLastModified();
  8. if (!noSubdir) {
  9. logger.debug("Locking " + cacheDir + " to create symbolic link");
  10. lockChannel = new FileOutputStream(lockFile).getChannel();
  11. lock = lockChannel.lock();
  12. if ((!file.exists() || !Files.isSymbolicLink(path) || !Files.readSymbolicLink(path).equals(targetPath))
  13. logger.debug("Locking " + cacheDir + " to create symbolic link");
  14. lockChannel = new FileOutputStream(lockFile).getChannel();
  15. lock = lockChannel.lock();
  16. if ((!file.exists() || !Files.isSymbolicLink(path) || !Files.readSymbolicLink(path).equals(urlPath))
  17. logger.debug("Locking " + cacheDir + " before extracting");
  18. lockChannel = new FileOutputStream(lockFile).getChannel();
  19. lock = lockChannel.lock();

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

  1. if (sampleURL != null) {
  2. try {
  3. URL u = new URL(sampleURL);
  4. writer.println("Checking access to " + u);
  5. URLConnection c = u.openConnection();
  6. writer.println("Content type: " + c.getContentType());
  7. writer.println("Content length: " + c.getContentLength());
  8. } catch (Throwable e) {
  9. e.printStackTrace(writer);

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

  1. ResultReceiver receiver = (ResultReceiver) intent.getParcelableExtra("receiver");
  2. try {
  3. URL url = new URL(urlToDownload);
  4. URLConnection connection = url.openConnection();
  5. connection.connect();
  6. int fileLength = connection.getContentLength();
  7. InputStream input = new BufferedInputStream(connection.getInputStream());
  8. OutputStream output = new FileOutputStream("/sdcard/BarcodeScanner-debug.apk");
  9. while ((count = input.read(data)) != -1) {
  10. total += count;

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

  1. private int tryGetFileSize(URL url) {
  2. HttpURLConnection conn = null;
  3. try {
  4. conn = (HttpURLConnection) url.openConnection();
  5. conn.setRequestMethod("HEAD");
  6. conn.getInputStream();
  7. return conn.getContentLength();
  8. } catch (IOException e) {
  9. return -1;
  10. } finally {
  11. conn.disconnect();
  12. }
  13. }

代码示例来源:origin: deeplearning4j/nd4j

  1. public long contentLength() throws IOException {
  2. URL url = this.getURL();
  3. if (ResourceUtils.isFileURL(url)) {
  4. return this.getFile().length();
  5. } else {
  6. URLConnection con = url.openConnection();
  7. ResourceUtils.useCachesIfNecessary(con);
  8. if (con instanceof HttpURLConnection) {
  9. ((HttpURLConnection) con).setRequestMethod("HEAD");
  10. }
  11. return (long) con.getContentLength();
  12. }
  13. }

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

  1. URL url = new URL("http://server.com/file.mp3");
  2. URLConnection urlConnection = url.openConnection();
  3. urlConnection.connect();
  4. int file_size = urlConnection.getContentLength();

代码示例来源:origin: chewiebug/GCViewer

  1. private FileInformation readFileInformation(URL url) {
  2. FileInformation fileInformation = new FileInformation();
  3. URLConnection urlConnection;
  4. try {
  5. if (url.getProtocol().startsWith("http")) {
  6. urlConnection = url.openConnection();
  7. ((HttpURLConnection) urlConnection).setRequestMethod("HEAD");
  8. try (InputStream inputStream = urlConnection.getInputStream()) {
  9. fileInformation.length = urlConnection.getContentLength();
  10. fileInformation.lastModified = urlConnection.getLastModified();
  11. }
  12. }
  13. else {
  14. if (url.getProtocol().startsWith("file")) {
  15. File file = new File(url.getFile());
  16. if (file.exists()) {
  17. fileInformation = new FileInformation(file);
  18. }
  19. }
  20. }
  21. }
  22. catch (IOException e) {
  23. if (LOG.isLoggable(Level.WARNING))
  24. LOG.log(Level.WARNING, "Failed to obtain age and length of URL " + url, e);
  25. }
  26. return fileInformation;
  27. }

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

  1. final URLConnection connection = this.url.openConnection();
  2. if (connection.getContentLength() >= 0) {
  3. return true;

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

  1. public static boolean hasInternetAccess(Context context) {
  2. if (isNetworkAvailable(context)) {
  3. try {
  4. HttpURLConnection urlc = (HttpURLConnection)
  5. (new URL("http://clients3.google.com/generate_204")
  6. .openConnection());
  7. urlc.setRequestProperty("User-Agent", "Android");
  8. urlc.setRequestProperty("Connection", "close");
  9. urlc.setConnectTimeout(1500);
  10. urlc.connect();
  11. return (urlc.getResponseCode() == 204 &&
  12. urlc.getContentLength() == 0);
  13. } catch (IOException e) {
  14. Log.e(TAG, "Error checking internet connection", e);
  15. }
  16. } else {
  17. Log.d(TAG, "No network available!");
  18. }
  19. return false;
  20. }

代码示例来源:origin: org.apache.logging.log4j/log4j-core

  1. @Override
  2. protected Class<?> findClass(final String name) throws ClassNotFoundException {
  3. final String path = name.replace('.', '/').concat(".class");
  4. final URL resource = super.getResource(path);
  5. if (resource == null) {
  6. throw new ClassNotFoundException(name);
  7. }
  8. try {
  9. final URLConnection uc = resource.openConnection();
  10. final int len = uc.getContentLength();
  11. final InputStream in = new BufferedInputStream(uc.getInputStream());
  12. final byte[] bytecode = new byte[len];
  13. try {
  14. IOUtils.readFully(in, bytecode);
  15. } finally {
  16. Closer.closeSilently(in);
  17. }
  18. return defineClass(name, bytecode, 0, bytecode.length);
  19. } catch (final IOException e) {
  20. Throwables.rethrow(e);
  21. return null; // unreachable
  22. }
  23. }
  24. }

代码示例来源:origin: micronaut-projects/micronaut-core

  1. @Override
  2. public long contentLength() throws IOException {
  3. URL url = getURL();
  4. if (ResourceUtils.isFileURL(url)) {
  5. // Proceed with file system resolution...
  6. return getFile().length();
  7. }
  8. // Try a URL connection content-length header...
  9. URLConnection con = url.openConnection();
  10. useCachesIfNecessary(con);
  11. if (con instanceof HttpURLConnection) {
  12. ((HttpURLConnection) con).setRequestMethod("HEAD");
  13. }
  14. return con.getContentLength();
  15. }

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

  1. URL url = new URL("http", server, port,
  2. "/" + classname.replace('.', '/') + ".class");
  3. URLConnection con = url.openConnection();
  4. con.connect();
  5. int size = con.getContentLength();
  6. InputStream s = con.getInputStream();
  7. if (size <= 0)
  8. b = readStream(s);
  9. int len = 0;
  10. do {
  11. int n = s.read(b, len, size - len);
  12. if (n < 0) {
  13. s.close();

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

  1. private int getFileSize(URL url) {
  2. HttpURLConnection conn = null;
  3. try {
  4. conn = (HttpURLConnection) url.openConnection();
  5. conn.setRequestMethod("HEAD");
  6. conn.getInputStream();
  7. return conn.getContentLength();
  8. } catch (IOException e) {
  9. return -1;
  10. } finally {
  11. conn.disconnect();
  12. }
  13. }

代码示例来源:origin: perwendel/spark

  1. } else {
  2. URLConnection con = url.openConnection();
  3. customizeConnection(con);
  4. HttpURLConnection httpCon =
  5. if (con.getContentLength() >= 0) {
  6. return true;

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

  1. String path = "/sdcard/YourApp.apk";
  2. try {
  3. URL url = new URL(sUrl[0]);
  4. URLConnection connection = url.openConnection();
  5. connection.connect();
  6. int fileLength = connection.getContentLength();
  7. OutputStream output = new FileOutputStream(path);
  8. while ((count = input.read(data)) != -1) {
  9. total += count;
  10. publishProgress((int) (total * 100 / fileLength));

代码示例来源:origin: com.google.gwt/gwt-servlet

  1. /**
  2. * Helper method to read the contentLength of a given URL, automatically
  3. * closing the InputStream that is opened as a side effect.
  4. */
  5. private int getContentLength(URL url) throws IOException {
  6. URLConnection conn = url.openConnection();
  7. try {
  8. return conn.getContentLength();
  9. } finally {
  10. Utility.close(conn.getInputStream());
  11. }
  12. }

代码示例来源:origin: deeplearning4j/nd4j

  1. return this.getFile().exists();
  2. } else {
  3. URLConnection con = ex.openConnection();
  4. ResourceUtils.useCachesIfNecessary(con);
  5. HttpURLConnection httpCon = con instanceof HttpURLConnection ? (HttpURLConnection) con : null;
  6. if (con.getContentLength() >= 0) {
  7. return true;
  8. } else if (httpCon != null) {

相关文章

URLConnection类方法