zoukankan      html  css  js  c++  java
  • java 从网络Url中下载文件

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    /**
     * Created by liwj on 2017/4/14.
     */
    public class Download {
    
        public static void downloadFileFromUrl(String fileUrl,String fileName,String savePath) throws Exception {
            //获取连接
            URL url=new URL(fileUrl);
            HttpURLConnection connection=(HttpURLConnection)url.openConnection();
            connection.setConnectTimeout(3*1000);
            //设置请求头
            connection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36");
            //获取输入流
            InputStream in=connection.getInputStream();
    
            File saveDir=new File(savePath);
            if(!saveDir.exists()){
                saveDir.mkdirs();
            }
            File file=new File(savePath+fileName);
    
            OutputStream out=new FileOutputStream(file);
    
            byte[] bytes=new byte[1024];
            int len=0;
            while((len=in.read(bytes))!=-1){
                out.write(bytes,0,len);
            }
            out.close();
            in.close();
        }
    
        public static void main(String[] args){
            try {
                downloadFileFromUrl("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png","baidu.png","E:\");
            }catch (Exception e){
    
            }
        }
    }
  • 相关阅读:
    【Java】Java网络编程
    (4.47)sql server 中的 values 构造临时表
    阿里时序数据库 telegraf+influxdb+grafana for sqlserver input plugin
    全景互动制作工具
    湖南师范大学的案例
    git观点
    js-cookie对cookie的操作
    hsf的意义在于什么
    Prettier看这一篇就行了
    关于微前端的观点
  • 原文地址:https://www.cnblogs.com/zuferj115/p/6707531.html
Copyright © 2011-2022 走看看