java 如何在java中下载大型文件(大小> 50MB)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25045003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:12:41  来源:igfitidea点击:

How to download large sized Files (size > 50MB) in java

javadownloadp2

提问by Raguram

I'm downloading files from a remote location, and the download is complete for smaller sized files and in-complete for large sized files (>10 MB). Here is my code that i have used for downloading files from remote server .

我正在从远程位置下载文件,对于较小的文件下载已完成,对于大文件 (>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());
    }

采纳答案by bitkot

You can use apache commons IO library. It's easy. I have used it in many projects.

您可以使用apache commons IO 库。这简单。我在很多项目中都使用过它。

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());
}

回答by Rakesh Sharma

Firstly, I'd suggest you use:

首先,我建议你使用:

FileInputStream in = new FileInputStream(file);  

instead of:

代替:

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

(To avoid building up memory usage)

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

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();
    }
}

回答by Xabster

Please read BufferedInputStream's method .available() in the API.

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

It returns the number of available bytes already downloaded (ie. the number of bytes you can read out of the stream without accessing/waiting for the network).

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

You should create a fixed size byte array, fx. 2048 bytes, and use the read() methods until it returns -1.

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