Java中 URL实现断点下载
发布时间:2020-12-14 19:57:23 所属栏目:Java 来源:网络整理
导读:复制代码 代码如下: URL ur = new URL("http://localhost:8080/first/he.txt"); HttpURLConnection conn = (HttpURLConnection) ur.openConnection();//URL.openConnection() -- return URLCommection(直接子类HttpURLConnection) conn.setRequestProperty("
复制代码 代码如下: URL ur = new URL("http://localhost:8080/first/he.txt"); HttpURLConnection conn = (HttpURLConnection) ur.openConnection();//URL.openConnection() -- >return URLCommection(直接子类HttpURLConnection) conn.setRequestProperty("Range","bytes=5-");//设置请求参数属性,设置下载从第5个字节开始下载; InputStream in = conn.getInputStream(); int len = 0; byte[] buf = new byte[1024]; FileOutputStream out = new FileOutputStream("d:a.txt"); while ((len = in.read(buf)) > 0) { out.write(buf,len); } in.close(); out.close(); 您可能感兴趣的文章:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |