当前位置: 代码迷 >> Java相关 >> java中io流下载文件实例
  详细解决方案

java中io流下载文件实例

热度:329   发布时间:2013-06-17 16:59:08.0
java中io流下载文件实例
  public static void saveUrlAs(String Url, File fileName){
  //此方法只能用HTTP协议
  //保存文件到本地http://www.cdtarena.com/qrs
  //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.cdtarena.com
搜索更多相关的解决方案: 文件下载  下载文件  public  null  java  

----------------解决方案--------------------------------------------------------
  相关解决方案