| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 711 人关注过本帖
标题:java中io流下载文件实例
只看楼主 加入收藏
shitainong
Rank: 1
等 级:新手上路
帖 子:70
专家分:0
注 册:2012-7-6
结帖率:0
收藏
 问题点数:0 回复次数:0 
java中io流下载文件实例
  public static void saveUrlAs(String Url, File fileName){
  //此方法只能用HTTP协议
  //保存文件到本地http://www.
  //Url是文件下载地址,fileName 为一个全名(路径+文件名)文件
  URL url;
  DataOutputStream out = null;
  DataInputStream in = null;
  try {
  url = new URL(Url);
  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  in = new DataInputStream(connection.getInputStream());
  out = new DataOutputStream(new FileOutputStream(fileName));
  byte[] buffer = new byte[4096];
  int count = 0;
  while ((count = in.read(buffer)) > 0) {
  out.write(buffer, 0, count);
  }
  }catch (Exception e) {
  e.printStackTrace();
  }finally{
  try {
  if(out != null){
  out.close();
  }
  if(in != null){
  in.close();
  }
  } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }
  }
  }
成都电脑培训找成都IT培训机构成都达内是高端C++培训、php培训等10年品牌。成都c++培训咨询:http://www.
搜索更多相关主题的帖子: 文件下载 下载文件 public null java 
2013-06-17 16:59
快速回复:java中io流下载文件实例
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019420 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved