java根据URL地址下载文件

public static void main(String[] args) throws IOException {
  String s = "
http://mp3.9ku.com/file2/418/417077.mp3";
  // String s = "
http://bbs.besgold.com/url/vdisk/11654115.mp3";

  System.out.println(s);
  InputStream in = new URL(s).openConnection().getInputStream();
  OutputStream ou = new FileOutputStream("d:\\11.mp3");
  try {
   int c = 0;
   int k = 0;
   float m = 0;
   int bread = 0;
   byte[] byt = new byte[2048];
   while ((bread = in.read(byt, 0, 2048)) != -1) {
    ou.write(byt, 0, bread);
    c = c + bread;
    k = c / 1024;
    m = k / 1024;
    System.out.println("己下载: " + k + "k  " + m + "M");
   }
   in.close();
   ou.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

你可能感兴趣的:(URL,java下载,java根据URL地址下载文件,java文件下载)