在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

java 下载网络中在文件

2016-12-20 13:16| 发布者: zhangjf| 查看: 454| 评论: 0

摘要: URL url = new URL(Config.local + /excel/ + name); HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(); // 设置请求信息 ...
  1. URL url = new URL(Config.local + "/excel/" + name);
  2. HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
  3. // 设置请求信息
  4. httpConnection.setRequestProperty("GET", "/down.zip HTTP/1.1");
  5. // 设置接受信息
  6. httpConnection.setRequestProperty("Accept", "image/gif,image/x-xbitmap,application/msword");
  7. // 设置连接信息
  8. httpConnection.setRequestProperty("Connection", "Keep-Alive");
  9. // 获得输入流
  10. InputStream input = httpConnection.getInputStream();
  11. // 创建随机文件
  12. flist.add(new File(name));
  13. RandomAccessFile oSavedFile = new RandomAccessFile(name, "rw");
  14. byte[] b = new byte[1024];
  15. int nRead;
  16. // 从输入流中读入字节流,然后写到文件中
  17. while ((nRead = input.read(b, 0, 1024)) > 0) {
  18. oSavedFile.write(b, 0, nRead);
  19. }
  20. input.close();
  21. oSavedFile.close();
  22. httpConnection.disconnect();
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 16:44

Copyright 2015-2025 djqfx

返回顶部