• 下载服务器文件到本地


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

      

  • 相关阅读:
    liunx下忘记mysql的密码
    oracle add_month函数
    oracle的LAST_DAY()函数
    JAVA方法中参数到底是值传递还是引用传递
    list=null和list.size=0的区别
    对python 函数的一些理解
    python 文件流
    html canvas 简单体验
    angular js 指令
    初识 angular js
  • 原文地址:https://www.cnblogs.com/wanhua-wu/p/6377810.html
Copyright © 2020-2023  润新知