从Dropbox下载整个(84M)文件后,它变为0字节

时间:2018-05-07 09:41:24

标签: java inputstream dropbox outputstream

我正在从dropbox下载一个zip文件。当它继续下载时,我测量文件大小,并使用下面的代码增加它的大小。它下载整个84M,完成下载后变为0字节。我实际上做错了什么?

public static void downloadDropBox(File file) {

    String url = "https://www.dropbox.com/sh/jx4b2wvqg8d4ze1/AAA0J3LztkRc6FJ5tKy4dUKha?dl=1";

      int bytesRead;
      byte[] bytesArray = new byte[1024];
      InputStream is = null;
      FileOutputStream outputStream = null;
      long progres = 0;

    try {
        URL fileUrl = new URL(url);
        HttpURLConnection connection = (HttpURLConnection)fileUrl.openConnection();
        connection.connect();
        is = connection.getInputStream();
        outputStream = new FileOutputStream(file);

        while ((bytesRead = is.read(bytesArray, 0, 1024)) != -1) {
            outputStream.write(bytesArray, 0, bytesRead);

            }
        }

    } catch (Exception e) {
      e.printStackTrace();
    }
    finally {
        if (is != null) {
            try {
                is.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (outputStream != null) {
            try {
                outputStream.flush();

                outputStream.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  }

在下载文件中:

enter image description here

完成下载文件后:

enter image description here

0 个答案:

没有答案