Java实现跨域文件下载(下载远程文件)

来源:互联网 发布:土豆pc客户端mac版 编辑:程序博客网 时间:2024/06/05 13:35

啥也不说,光贴代码,你懂的。

 

 public static void main(String[] args) throws Exception{
  String url = "http://www.baidu.com/img/baidu_jgylogo1.gif";
  URL u = new URL(url);
  InputStream is = u.openStream();
  OutputStream os = new FileOutputStream("D://test.jpg");
  int buff = 0;
  while((buff = is.read()) != -1){
   os.write(buff);
  }
  os.close();
  is.close();
 }

 

当然首先你得保证那个地址中的文件可以正常访问。