Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 下载文件时不知道其大小_Java_File_Memory_Download - Fatal编程技术网

Java 下载文件时不知道其大小

Java 下载文件时不知道其大小,java,file,memory,download,Java,File,Memory,Download,我使用URLConnection、DataInputStream和FileOutputStream下载文件。我正在创建一个大字节[],大小与在线文件(getContentLength()相同)。问题是,当我尝试下载大型文件时,我遇到了一个OutOfMemoryError:Java堆空间,这是一种正常的行为 代码如下: URLConnection con; DataInputStream dis; FileOutputStream fos; byte[] fil

我使用URLConnection、DataInputStream和FileOutputStream下载文件。我正在创建一个大字节[],大小与在线文件(getContentLength()相同)。问题是,当我尝试下载大型文件时,我遇到了一个OutOfMemoryError:Java堆空间,这是一种正常的行为

代码如下:

    URLConnection con;
    DataInputStream dis;  
    FileOutputStream fos;
    byte[] fileData = null;

    URL url = new URL(from);
    con = url.openConnection();                  

    con.setUseCaches(false);
    con.setDefaultUseCaches(false);
    con.setRequestProperty("Cache-Control", "no-store,max-age=0,no-cache");
    con.setRequestProperty("Expires", "0");
    con.setRequestProperty("Pragma", "no-cache");
    con.setConnectTimeout(5000);
    con.setReadTimeout(30000);

    dis = new DataInputStream(con.getInputStream());

    int contentLength = con.getContentLength();

    //Taille connue
    if (contentLength != -1)
    {
        fileData = new byte[con.getContentLength()];

        for (int x = 0; x < fileData.length; x++) 
        {             
            fileData[x] = dis.readByte();

            if (listener != null)
            {
                listener.onFileProgressChanged(x, fileData.length);
            }
        }
    }
    //Taille inconnue
    else
    {
        System.out.println("Attention : taille du fichier inconnue !");
        if (undefinedListener != null)
        {
            undefinedListener.onUndefinedFile();
        }
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        while (true)
        {
            try
            {
                stream.write(dis.readByte());
            }
            catch (EOFException ex)
            {
                //Fin
                fileData = stream.toByteArray();
                stream.close();             
            }
        }
    }

    dis.close(); 

    //Ecriture
    fos = new FileOutputStream(file);
    fos.write(fileData);  
    fos.close();    
URLConnection;
数据输入流dis;
文件输出流;
字节[]fileData=null;
URL=新URL(来自);
con=url.openConnection();
con.setUseCaches(假);
con.setDefaultUseCaches(假);
con.setRequestProperty(“缓存控制”,“无存储,最大年龄=0,无缓存”);
con.setRequestProperty(“到期”、“0”);
con.setRequestProperty(“Pragma”,“无缓存”);
con.设置连接超时(5000);
con.设置读取超时(30000);
dis=新的DataInputStream(con.getInputStream());
int contentLength=con.getContentLength();
//泰勒康努埃
如果(contentLength!=-1)
{
fileData=新字节[con.getContentLength()];
对于(int x=0;x
我听说我应该把文件分成几块来避免它。我知道怎么做,很简单,但是。。。如果文件的ContentLength不能从服务器(getContentLength()=-1)变为红色,我该怎么做?如果我不知道文件的大小,我应该如何将它分割成块


谢谢

完全没有理由使用
DataInputStream
。使用:

我正在创建一个与在线文件大小相同的大字节[]

为什么??您不需要文件大小的字节数组。这只会浪费空间并增加延迟。只需读取和写入缓冲区:

while ((count = in.read(buffer)) > 0)
{
    out.write(buffer, 0, count);
}
这适用于大小大于零的任何字节[]缓冲区。我通常使用8192


使用URLConnection,您也不需要内容长度:只需如上所述读取EOS。

为什么要使用
DataInputStream
?显示您现在使用的代码。几乎可以肯定有一个简单的解决方案,它可能涉及Apache Commons
IOUtils
。我这样做了,它可以工作,谢谢:)问题是,当您中断下载(例如关闭窗口)时,您只剩下一半的下载文件=/如果下载尚未完全完成,我如何删除该文件?
while ((count = in.read(buffer)) > 0)
{
    out.write(buffer, 0, count);
}