java http下载文件流

 /**

    * 通过HTTP 协议获取文件流

    * @param urlPath http协议的url

    * @param fileNamePath 文件路径

    * @return true:写入成功 

    */

   public  boolean saveFileToHttp(String urlPath, String fileNamePath) {

    try {

      URL url = new URL(urlPath);

      HttpURLConnection connection = (HttpURLConnection) url.openConnection();

      DataInputStream in = new DataInputStream(connection.getInputStream());

      DataOutputStream out = new DataOutputStream(new FileOutputStream(fileNamePath));

      byte[] buffer = new byte[4096];

      int count = 0;

      while ((count = in.read(buffer)) > 0) {

        out.write(buffer, 0, count);

      }

      out.close();

      in.close();

      return true;

    }

    catch (Exception e) {

      return false;

    }

  }

你可能感兴趣的:(java)