下载网站上的付费资源-java代码

package com.kuang.lesson04;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class UrlDown {
    public static void main(String[] args) throws Exception {
        //1.下载地址
        URL url = new URL ("https://m701.music.126.net/20210202184230/6e5fc76589c2b2d6b59bfd6b88dc6710/jdyyaac/obj/w5rDlsOJwrLDjj7CmsOj/5172545316/ac30/2edf/1dc4/4dceb13149a476e8745a5e8d74a8006e.m4a");//文件路径
        //2.连接到这个资源 HTTP
        HttpURLConnection urlConnection= (HttpURLConnection)url.openConnection ();

        InputStream inputStream = urlConnection.getInputStream ();

        FileOutputStream fos = new FileOutputStream ("e.m4a");//下载文件存储名

        byte[] buffer = new byte[1024];
        int len;
        while ((len=inputStream.read (buffer))!=-1){
            fos.write (buffer,0,len);
        }
        fos.close ();
        inputStream.close ();
        urlConnection.disconnect ();
    }
}
posted @ 2021-02-02 18:26  柠檬情  阅读(446)  评论(0编辑  收藏  举报