idea下载网络图片,音乐到本地

下载图片:

public class URLTest {
    public static void main(String[] args) throws Exception {
        //要复制图片的地址
        URL url = new URL("http://pics4.baidu.com/feed/500fd9f9d72a60596fe549051190049c023bba67.jpeg?token=db66af7efb7371437559b66676578fe3");
//      //连接到这个资源
        HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
        //获取输入流
        InputStream inputStream = urlConnection.getInputStream();
        //写到哪个文件的输出流
        FileOutputStream fos = new FileOutputStream("picture.jpg");

        byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer))!= -1){
            fos.write(buffer,0,len);
        }

    }

下载音乐:

public class URLTest {
    public static void main(String[] args) throws Exception {
        //要复制音乐的地址
        URL url = new URL("http://ss.sycdn.kuwo.cn/d8a9b1edb2718f66ca6a923b5975ba98/60c22e79/resource/n3/99/15/3712425278.mp3");
//      //连接到这个资源
        HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
        //获取输入流
        InputStream inputStream = urlConnection.getInputStream();
        //写到哪个文件的输出流
        FileOutputStream fos = new FileOutputStream("无羁.mp3");

        byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer))!= -1){
            fos.write(buffer,0,len);
        }

    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值