【问题标题】:How to download large sized Files (size > 50MB) in java如何在java中下载大文件(大小> 50MB)
【发布时间】:2014-09-22 13:52:03
【问题描述】:

我正在从远程位置下载文件,对于较小的文件,下载已完成,对于大文件 (>10 MB),下载未完成。这是我用来从远程服务器下载文件的代码。

    File dstFile = null;
    // check the directory for existence.
    String dstFolder = LOCAL_FILE.substring(0,LOCAL_FILE.lastIndexOf(File.separator));
    if(!(dstFolder.endsWith(File.separator) || dstFolder.endsWith("/")))
        dstFolder += File.separator;

    // Creates the destination folder if doesn't not exists
    dstFile = new File(dstFolder);
    if (!dstFile.exists()) {
        dstFile.mkdirs();
    }
    try {
        URL url = new URL(URL_LOCATION);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
        connection.addRequestProperty("User-Agent", "Mozilla/4.76"); 
        //URLConnection connection = url.openConnection();
        BufferedInputStream stream = new BufferedInputStream(connection.getInputStream());
        int available = stream.available();
        byte b[]= new byte[available];
        stream.read(b);
        File file = new File(LOCAL_FILE);
        OutputStream out  = new FileOutputStream(file);
        out.write(b);
    } catch (Exception e) {
        System.err.println(e);
        VeBLogger.getInstance().log( e.getMessage());
    }

【问题讨论】:

  • 还有其他 API 可用于下载大文件吗?
  • 您的实际问题是什么或这段代码有什么问题?
  • 在您的FileOutputStream 上拨打flush() 然后close() 它。

标签: java download p2


【解决方案1】:

您可以使用apache commons IO library。 这很容易。我在很多项目中都使用过它。

File dstFile = null;
// check the directory for existence.
String dstFolder = LOCAL_FILE.substring(0,LOCAL_FILE.lastIndexOf(File.separator));
if(!(dstFolder.endsWith(File.separator) || dstFolder.endsWith("/")))
    dstFolder += File.separator;

// Creates the destination folder if doesn't not exists
dstFile = new File(dstFolder);
if (!dstFile.exists()) {
    dstFile.mkdirs();
}
try {
    URL url = new URL(URL_LOCATION);
    FileUtils.copyURLToFile(url, dstFile);
} catch (Exception e) {
    System.err.println(e);
    VeBLogger.getInstance().log( e.getMessage());
}

【讨论】:

  • Amit : 感谢您的回复。使用 FileUtils.copyURLToFile(url, dstFile) 时文件大小有任何限制,我们可以下载所有文件(无论其大小)吗?跨度>
  • 在调用 FileUtils.copyURLToFile(url, dstFile) 方法时是否需要注意关闭流?
  • 不用,看source code,而且大小没有限制
  • HiberKnight:嗨,这个 copyURLToFile() 是否从服务器复制目录中存在的文件列表?我的意思是,如果服务器中的目录有文件列表,并且我提供目录位置作为 URL 参数。那么它是否成立呢?
  • 我认为不可能从服务器下载所有文件。是否有任何 API 可以做到这一点?
【解决方案2】:

首先, 我建议你使用:

FileInputStream in = new FileInputStream(file);  

代替:

BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));

(为了避免增加内存使用)

try
{
    FileInputStream fileInputStream  = new FileInputStream(file);
    byte[] buf=new byte[8192];
    int bytesread = 0, bytesBuffered = 0;
    while( (bytesread = fileInputStream.read( buf )) > -1 ) {
        out.write( buf, 0, bytesread );
        bytesBuffered += bytesread;
        if (bytesBuffered > 1024 * 1024) { //flush after 1MB
            bytesBuffered = 0;
            out.flush();
        }
    }
}
finally {
    if (out != null) {
        out.flush();
    }
}

【讨论】:

  • 当你在这里实现一个缓冲区时,避免使用 BufferedInputStream 似乎很奇怪。你能详细说明为什么 BufferedInputStream 不合适吗?
  • 问题是询问如何从 URL 下载文件。 FileInputStream 不是您从 URL 读取/下载文件的方式。 FileInputStream 用于从文件系统中读取数据。
【解决方案3】:

请阅读 API 中 BufferedInputStream 的方法 .available()。

它返回已下载的可用字节数(即您可以在不访问/等待网络的情况下从流中读取的字节数)。

您应该创建一个固定大小的字节数组 fx。 2048 字节,并使用 read() 方法直到它返回 -1。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-20
    • 2017-03-10
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    • 2012-06-06
    相关资源
    最近更新 更多