zoukankan      html  css  js  c++  java
  • URL 下载

    package URL;

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.URL;
    import java.net.URLConnection;

    import org.omg.CORBA.portable.InputStream;

    public class URLDemo {

    public static void main(String[] args) throws IOException {
    Download.download("http://img03.sogoucdn.com/app/a/100520020/ab84b70f33697910f6c92d22a673bb0e","my.jpg", "F:\");

    }

    }
    class Download{
    public static void download(String urlstring,String filename,String savepath) throws IOException{
    //创建URL定位网络资源
    URL url =new URL(urlstring);

    // URLConnection con = url.openConnection();
    // java.io.InputStream is = con.getInputStream();

    //得到URL字节输入流
    java.io.InputStream is = url.openStream();
    byte[] buff = new byte[1024];
    int len = 0;
    File file = new File(savepath);
    if(!file.exists()){
    //创建指定文件夹
    file.mkdirs();
    }
    //传送完整文件路径及文件名给文件输出流用以创建文件
    OutputStream os = new FileOutputStream(file.getAbsolutePath()+"\"
    +filename);
    //为文件写入内容
    while((len=is.read(buff))!=-1){
    os.write(buff,0,len);

    }
    os.close();
    is.close();
    }

    }

  • 相关阅读:
    模块jieba库的使用
    模块wordcloud库的使用
    爬取哔哩哔哩网站弹幕
    爬虫讲解
    基本统计值计算
    数据类型及元素元组
    pyinstall的安装及使用
    面向对象介绍
    re模块
    logging模块
  • 原文地址:https://www.cnblogs.com/the-wang/p/7123390.html
Copyright © 2011-2022 走看看