zoukankan      html  css  js  c++  java
  • 下载服务器文件到本地

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    import java.net.URLConnection;
     
    public class DownLoad {   
    
      public static void downloadFile(URL theURL, String filePath) throws IOException {  
    
         File dirFile = new File(filePath);
            if(!dirFile.exists()){//文件路径不存在时,自动创建目录
              dirFile.mkdir();
            }
    
          //从服务器上获取图片并保存
          URLConnection  connection = theURL.openConnection();
          InputStream in = connection.getInputStream();  
          FileOutputStream os = new FileOutputStream(filePath+"\123.png"); 
    
          byte[] buffer = new byte[4 * 1024];  
          int read;  
          while ((read = in.read(buffer)) > 0) {  
              os.write(buffer, 0, read);  
               }  
            os.close();  
            in.close();
          }   
    
          public static void main(String[] args) {   
            String urlPath = "http://1.1.9.59:8089/image/123.png";   
            String filePath = "d:\excel";   
            URL url = new URL(urlPath);   
              try {   
                  downloadFile(url,filePath);   
               } catch (IOException e) {   
                e.printStackTrace();   
             }   
          }   
     
    }
    

      

  • 相关阅读:
    Spark源码分析之-scheduler模块
    YARN
    java.lang.NoClassDefFoundError 怎么解决
    rdd
    Apache Spark探秘:三种分布式部署方式比较
    Sqrt函数的实现方法
    golang 自旋锁的实现
    支付宝往余额宝转钱怎么保证一致性
    mysql 面试题
    TCP 进阶
  • 原文地址:https://www.cnblogs.com/wanhua-wu/p/6377810.html
Copyright © 2011-2022 走看看