6

我目前有以下代码读取位于网络上的文件并将其写入手机上的文件:

InputStream inputStream = new URL(sourceFileWebAddress).openStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
FileOutputStream fileOutputStream = new FileOutputStream(targetFile);

int count;
byte buffer[] = new byte[1024];

while ((count = bufferedInputStream.read(buffer, 0, buffer.length)) != -1)
  fileOutputStream.write(buffer, 0, count);

有谁知道是否有可能(使用上面的设置或其他方式)确定在开始下载之前要读取的字节总数(以便在下载进行时向用户发布百分比进度)?

4

5 回答 5

8

使用getContentLength方法URLConnection

URL url = new URL(sourceFileWebAddress);
URLConnection connection = url.openConnection();
connection.connect();

int fileLenth = connection.getContentLength();

InputStream inputStream = url.openStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
于 2012-06-06T12:52:51.267 回答
3

使用URLConnnection试试这个

URLConnection connection = new URL(sourceFileWebAddress).openStream();
InputStream stream = connection.getInputStream();

System.out.println("total size: "+connection.getContentLength();//size

BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
FileOutputStream fileOutputStream = new FileOutputStream(targetFile);

int count;
byte buffer[] = new byte[1024];

while ((count = bufferedInputStream.read(buffer, 0, buffer.length)) != -1)
  fileOutputStream.write(buffer, 0, count);
于 2012-06-06T12:53:00.533 回答
3

要确定在开始下载之前要读取的字节总数,一种方法是仅通过发送HTTP HEAD请求来获取响应标头,如下所示:

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

public class SimpleHTTPRequest {

  public static void main(String[] args) {
      HttpURLConnection connection = null;
      URL serverAddress = null;

      try {
          String sourceFileWebAddress = "http://localhost:8080/mycontents";
          serverAddress = new URL(sourceFileWebAddress);
          //set up out communications stuff
          connection = null;

          //Set up the initial connection
          connection = (HttpURLConnection)serverAddress.openConnection();

          //HEAD request will make sure that the contents are not downloaded.
          connection.setRequestMethod("HEAD");  

          connection.connect();
          System.out.println("========"+connection.getContentLength());

      } catch (MalformedURLException e) {
          e.printStackTrace();
      } catch (ProtocolException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
      finally
      {
          //close the connection, set all objects to null
          connection.disconnect();

          connection = null;
      }
  }
}

这将打印要下载的内容的大小,而无需实际下载内容。

于 2012-06-06T13:33:59.313 回答
2

您可以向服务器发送请求以返回文件大小。在进度条中使用该文件大小并启动​​下载。因此,这是两个单独的请求,但您可以将它们捆绑在同一个 asyc 任务中。因此,您首先获取 fielszie,调用progressUpdate您设置进度条值的位置,然后通过定期调用来继续下载progressUpdate()以更改进度条的状态。

于 2012-06-06T12:52:18.673 回答
1

getContentLength的方法URLConnection应该给你要下载的文件的大小。从此,您可以随意绘制 ProgressBar,并在您的 fileOutputStream 处理新数据时更新它(在 onProgressUpdate 中,假设您在 AsyncTask 中执行此操作。您是,对吗?)。

如果服务器没有在 getContentLength 中为您提供值(在大多数情况下为-1,但至少检查它是否小于或等于零),只需让您的 ProgressBar 不确定。

于 2012-06-06T13:22:38.743 回答