Downloading files with Java

前端 未结 2 1775
青春惊慌失措
青春惊慌失措 2021-02-04 01:11

I\'ve written a bit of code for downloading an episode of a webcast I do. It gets the URL of the episode and gets the place to save it. However, it only downloads up to 16MB and

相关标签:
2条回答
  • 2021-02-04 01:29

    here's another solution :

    import java.io.*;
    import java.net.*;
    public class DownloadFile
    {
    
        public static void main(String args[]) throws IOException
        {
    
            java.io.BufferedInputStream in = new java.io.BufferedInputStream(new java.net.URL(episode.getUrl()).openStream());
            java.io.FileOutputStream fos = new java.io.FileOutputStream(episode.getLocalSave());
            java.io.BufferedOutputStream bout = new BufferedOutputStream(fos);
            byte data[] = new byte[1024];
            int read;
            while((read = in.read(data,0,1024))>=0)
            {
                bout.write(data, 0, read);
            }
            bout.close();
            in.close();
        }
    }
    
    0 讨论(0)
  • 2021-02-04 01:39

    A quick look at the documentation of transferFrom:

    public abstract long transferFrom(ReadableByteChannel channel, long position, long count)
    

    WELL.

    The value 1<<24 for the count (from the original question) equals 16M

    I guess that's the answer to your question :-)

    0 讨论(0)
提交回复
热议问题