Android 单HTTP链接多文件下载

public static void main(String[] args) {
		System.out.println(System.currentTimeMillis());
		for (int i = 1; i < 2; i++) {
			int nRead = 0;
			String j = "";
			if (i < 10)
				j = "00" + i;
			else if (i < 100)
				j = "0" + i;
			String sPath = sURL.replace("xxx", j);
			// String sName = sPath.substring(sURL.lastIndexOf("/") + 1, sPath.lastIndexOf("?"));
			try {
				URL url = new URL(sPath);
				// URL url = new URL(sPath + "&" + System.currentTimeMillis());
				HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
				httpConnection.setRequestProperty("User-Agent", "Internet Explorer");
				httpConnection.connect();
				InputStream input = httpConnection.getInputStream();
				byte[] b = new byte[1024];
				RandomAccessFile file = getRandomFile();
				// 读取网络文件,写入指定的文件中
				while ((nRead = input.read(b)) != -1) {
					String msg = new String(b, 0, nRead, "iso-8859-1");
					// System.out.println(msg);
					if (buffer.length() > 0) {
						msg = buffer.toString() + msg;
						buffer.delete(0, buffer.length());
					}
					int pos = msg.indexOf("<!--next-->");
					boolean next = pos != -1;
					int pos_harf = msg.indexOf("<");
					boolean harf = (msg.length() - pos_harf) < 11 && pos_harf != -1;
					if (!next && !harf) {
						file.write(msg.getBytes("iso-8859-1"));
					} else if (!next && harf) {
						buffer.append(msg);
						continue;
					} else if (next) {
						file.write(msg.substring(0, pos).getBytes());
						file.close();
						buffer.append(msg.substring(pos + 11));
						file = getRandomFile();
						continue;
					}
				}
				httpConnection.disconnect();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		System.out.println(System.currentTimeMillis());
	}

	static synchronized RandomAccessFile getRandomFile() {
		try {
			return new RandomAccessFile("D:\\Android\\LeClient\\bin\\" + System.currentTimeMillis() + ".jpg", "rw");

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值