• <tfoot id='8OXGl'></tfoot>
    <i id='8OXGl'><tr id='8OXGl'><dt id='8OXGl'><q id='8OXGl'><span id='8OXGl'><b id='8OXGl'><form id='8OXGl'><ins id='8OXGl'></ins><ul id='8OXGl'></ul><sub id='8OXGl'></sub></form><legend id='8OXGl'></legend><bdo id='8OXGl'><pre id='8OXGl'><center id='8OXGl'></center></pre></bdo></b><th id='8OXGl'></th></span></q></dt></tr></i><div id='8OXGl'><tfoot id='8OXGl'></tfoot><dl id='8OXGl'><fieldset id='8OXGl'></fieldset></dl></div>

        • <bdo id='8OXGl'></bdo><ul id='8OXGl'></ul>

        <small id='8OXGl'></small><noframes id='8OXGl'>

        <legend id='8OXGl'><style id='8OXGl'><dir id='8OXGl'><q id='8OXGl'></q></dir></style></legend>

      1. Java URLConnection :我怎样才能找出网络文件的大小?

        Java URLConnection : how can I find out the size of a web file?(Java URLConnection :我怎样才能找出网络文件的大小?)
        <i id='WmP4z'><tr id='WmP4z'><dt id='WmP4z'><q id='WmP4z'><span id='WmP4z'><b id='WmP4z'><form id='WmP4z'><ins id='WmP4z'></ins><ul id='WmP4z'></ul><sub id='WmP4z'></sub></form><legend id='WmP4z'></legend><bdo id='WmP4z'><pre id='WmP4z'><center id='WmP4z'></center></pre></bdo></b><th id='WmP4z'></th></span></q></dt></tr></i><div id='WmP4z'><tfoot id='WmP4z'></tfoot><dl id='WmP4z'><fieldset id='WmP4z'></fieldset></dl></div>

            • <small id='WmP4z'></small><noframes id='WmP4z'>

            • <legend id='WmP4z'><style id='WmP4z'><dir id='WmP4z'><q id='WmP4z'></q></dir></style></legend>
                <tbody id='WmP4z'></tbody>
              <tfoot id='WmP4z'></tfoot>
                <bdo id='WmP4z'></bdo><ul id='WmP4z'></ul>
                  本文介绍了Java URLConnection :我怎样才能找出网络文件的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在为学校做一个项目,我正在实现一个可用于从网络下载文件的工具(带有限制选项).问题是,我将有一个 GUI,我将使用一个 JProgressBar 小部件,我想显示下载的当前进度.为此,我需要知道文件的大小.在下载文件之前如何获取文件的大小.

                  解决方案

                  应该任何 HTTP 响应都包含 Content-Length 标头,因此您可以查询 URLConnection 对象以获取此值.p>

                  //一旦打开连接列表值 = urlConnection.getHeaderFields().get("content-Length")if (values != null && !values.isEmpty()) {//getHeaderFields() 返回带有 key=(String) 标头的 Map//name, value = 该标头字段的字符串值列表.//这里只使用第一个值.String sLength = (String) values.get(0);如果(sLength!= null){//将长度解析为整数......}

                  服务器可能无法始终返回准确的 Content-Length,因此该值可能不准确,但至少在大多数情况下您会得到一些可用值.p>

                  更新: 或者,现在我更完整地查看了 URLConnection javadoc,您可以只使用 getContentLength() 方法.

                  I'm working on a project for school, and I'm implementing a tool which can be used to download files from the web ( with a throttling option ). The thing is, I'm gonna have a GUI for it, and I will be using a JProgressBar widget, which I would like to show the current progress of the download. For that I would need to know the size of the file. How do you get the size of the file prior to downloading the file.

                  解决方案

                  Any HTTP response is supposed to contain a Content-Length header, so you could query the URLConnection object for this value.

                  //once the connection has been opened
                  List values = urlConnection.getHeaderFields().get("content-Length")
                  if (values != null && !values.isEmpty()) {
                  
                      // getHeaderFields() returns a Map with key=(String) header 
                      // name, value = List of String values for that header field. 
                      // just use the first value here.
                      String sLength = (String) values.get(0);
                  
                      if (sLength != null) {
                         //parse the length into an integer...
                         ...
                      }
                  

                  It might not always be possible for a server to return an accurate Content-Length, so the value could be inaccurate, but at least you would get some usable value most of the time.

                  update: Or, now that I look at the URLConnection javadoc more completely, you could just use the getContentLength() method.

                  这篇关于Java URLConnection :我怎样才能找出网络文件的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                      • <bdo id='hA7we'></bdo><ul id='hA7we'></ul>
                        <tfoot id='hA7we'></tfoot>
                        • <legend id='hA7we'><style id='hA7we'><dir id='hA7we'><q id='hA7we'></q></dir></style></legend>

                            <tbody id='hA7we'></tbody>
                          1. <i id='hA7we'><tr id='hA7we'><dt id='hA7we'><q id='hA7we'><span id='hA7we'><b id='hA7we'><form id='hA7we'><ins id='hA7we'></ins><ul id='hA7we'></ul><sub id='hA7we'></sub></form><legend id='hA7we'></legend><bdo id='hA7we'><pre id='hA7we'><center id='hA7we'></center></pre></bdo></b><th id='hA7we'></th></span></q></dt></tr></i><div id='hA7we'><tfoot id='hA7we'></tfoot><dl id='hA7we'><fieldset id='hA7we'></fieldset></dl></div>

                            <small id='hA7we'></small><noframes id='hA7we'>