Download the file to a remote server (rpm)

//downFileUrl 遠程文件下載地址
 public void downFile(String downFileUrl) throws Exception {

    try {
     String fileName = downFileUrl.substring(downFileUrl.lastIndexOf("/") + 1); //取文件名
      String savePath = "D:/wwwroot/"+ fileName;  //要另存的路徑+文件名作為路徑
      URL downUrl = new URL(downFileUrl);
     URLConnection conn = downUrl.openConnection();
     int dataSize = conn.getContentLength(); //取得要下載的數據的長度

      BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
      BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(savePath)));

      byte[] data = new byte[1024];
      int len = in.read(data);
      while (len != -1) {
        out.write(data, 0, len);
        len = in.read(data);
      }
      in.close();
      out.close();
    } catch (Exception e) {
      e.printStackTrace();
    }

  }

Reproduced in: https: //my.oschina.net/dddgggaaa/blog/204929

Guess you like

Origin blog.csdn.net/weixin_33859844/article/details/92046889