---
title: "实现http续传下载的方式"
author: "absolute"
date: "2011-09-14T15:22:41Z"
tags: []
categories: ["移动开发"]
slug: "http"
draft: false
lastmod: "2011-09-14T15:22:41Z"
id: 1171757
url: "https://absolute.iteye.com/blog/1171757"
---

```
public void download() throws Exception {
	URL url = new URL("http://localhost/down.zip");
	HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();

	httpConnection.setRequestProperty("User-Agent", "Firefox");
	// range:-1000(like 1-1000)
	// range:1000-2000
	httpConnection.setRequestProperty("Range", "bytes=1024000-5689013");
	InputStream input = httpConnection.getInputStream();

	RandomAccessFile file = new RandomAccessFile("c:\\down.zip", "rw");
	long pos = 1024000;
	file.seek(pos);

	byte[] buff = new byte[1024];
	int read;
	while ((read = input.read(buff, 0, 1024)) > 0) {
		file.write(buff, 0, read);
	}
}
```