简体   繁体   中英

Download big file is too slow

I am encountering with a scenario like this.

My project receives a download request from perl

void downloadRequest(FileItemIterator items,
                             HttpServletResponse response) throws Exception
{ 
log.info("Start downloadRequest.......");
OutputStream os = response.getOutputStream();
File file = new File("D:\\clip.mp4");
            FileInputStream fileIn = new FileInputStream(file);
            //while ((datablock = dataOutputStreamServiceImpl.readBlock()) != null)
            byte[] outputByte = new byte[ONE_MEGABYE];
            while (fileIn.read(outputByte) != -1)
            {

                System.out.println("--------" + (i = i + 1) + "--------");
                System.out.println(new Date());
                //dataContent = datablock.getContent();
                System.out.println("Start write " + new Date());
                os.write(outputByte);
                System.out.println("End write " + new Date());
                //System.out.println("----------------------");
            }
            os.close();
        }
    }

I try to read and write blocks of 1MB from the file. However, it takes too long for downloading the whole file. ( my case is 20mins for file of 100MB)

I try to sysout and I saw a result like this:

The first few blocks can read, write data realy fast:

--------1--------
Mon Dec 07 16:24:20 ICT 2015
Start write Mon Dec 07 16:24:20 ICT 2015
End write Mon Dec 07 16:24:21 ICT 2015
--------2--------
Mon Dec 07 16:24:21 ICT 2015
Start write Mon Dec 07 16:24:21 ICT 2015
End write Mon Dec 07 16:24:21 ICT 2015
--------3--------
Mon Dec 07 16:24:21 ICT 2015
Start write Mon Dec 07 16:24:21 ICT 2015
End write Mon Dec 07 16:24:21 ICT 2015

But the next block is slower than the previous

--------72--------
Mon Dec 07 16:29:22 ICT 2015
Start write Mon Dec 07 16:29:22 ICT 2015
End write Mon Dec 07 16:29:29 ICT 2015
--------73--------
Mon Dec 07 16:29:29 ICT 2015
Start write Mon Dec 07 16:29:29 ICT 2015
End write Mon Dec 07 16:29:37 ICT 2015

--------124--------
Mon Dec 07 16:38:22 ICT 2015
Start write Mon Dec 07 16:38:22 ICT 2015
End write Mon Dec 07 16:38:35 ICT 2015
--------125--------
Mon Dec 07 16:38:35 ICT 2015
Start write Mon Dec 07 16:38:35 ICT 2015
End write Mon Dec 07 16:38:48 ICT 2015

I realy cannot understand how the outputStream write, why it take such a long time like that? or I made some mistakes?

Sorry for my bad english. I realy need your support. Thank in advance!

There is no guaranty that the read(byte[] b) method will read b.length number of bytes from the file which mean that your code could be sending more bytes then the file actually has.

For example, if you're processing a 10 MB file and the read(byte[] b) always reads b.length/2 from the file, you will be sending 20 MB.

To address that, you could do something like this.

 byte[] outputByte = new byte[ONE_MEGABYE];
 int r = -1;
 while ((r = fileIn.read(outputByte)) != -1){            
      os.write(outputByte,0,r);            
 }

This will ensure that you'll be sending only as much bytes as there were read from the file.

Beside this issue, the speed can be influenced by a lot of other factors like the internet speed or the other program's implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM