How to use Java to download a mp3 file online?

后端 未结 3 826
时光说笑
时光说笑 2021-02-06 12:11

I used the following method to download an mp3 file at : http://online1.tingclass.com/lesson/shi0529/43/32.mp3

But I got the following error :

java.io.FileNotFou

3条回答
  •  借酒劲吻你
    2021-02-06 13:01

    FileInputStream is used to access local files only. If you want to access the content of an URL you can setup an URLConnection or use something like this:

    URL myUrl = new URL("http://online1.tingclass.com/lesson/shi0529/43/32.mp3");
    InputStream myUrlStream = myUrl.openStream();
    ReadableByteChannel myUrlChannel = Channels.newChannel(myUrlStream);
    
    FileChannel destinationChannel=new FileOutputStream(To_File).getChannel();
    destinationChannel.transferFrom(myUrlChannel, 0, sizeOf32MP3);
    

    Or more simply just make a BufferedInputStream from myUrlStream and cycle the read/write operation until EOF is found on myUrlStream.

    Cheers, Andrea

提交回复
热议问题