欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java 下载网络中在文件,java下载网络文件, URL url

来源: javaer 分享于  点击 44359 次 点评:8

java 下载网络中在文件,java下载网络文件, URL url


    URL url = new URL(Config.local + "/excel/" + name);                  HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();                  // 设置请求信息                  httpConnection.setRequestProperty("GET", "/down.zip HTTP/1.1");                  // 设置接受信息                  httpConnection.setRequestProperty("Accept", "image/gif,image/x-xbitmap,application/msword");                  // 设置连接信息                  httpConnection.setRequestProperty("Connection", "Keep-Alive");                  // 获得输入流                  InputStream input = httpConnection.getInputStream();                  // 创建随机文件                  flist.add(new File(name));                  RandomAccessFile oSavedFile = new RandomAccessFile(name, "rw");                  byte[] b = new byte[1024];                  int nRead;                  // 从输入流中读入字节流,然后写到文件中                  while ((nRead = input.read(b, 0, 1024)) > 0) {                      oSavedFile.write(b, 0, nRead);                  }                  input.close();                  oSavedFile.close();                  httpConnection.disconnect();  
相关栏目:

用户点评