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();
    }

    }

  • 相关阅读:
    Java 必看的 Spring 知识汇总
    java与C#的基础语法区别--持续更新
    MQTT协议-MQTT协议解析(MQTT数据包结构)
    MQTT协议-MQTT协议简介及协议原理
    RabbitMQ与AMQP
    JMS、AMQP和MQTT主要特性
    socket 极值数量
    redis.conf详细说明
    Redis 模糊匹配 SearchKeys
    Parallel
  • 原文地址:https://www.cnblogs.com/the-wang/p/7123390.html
Copyright © 2011-2022 走看看