1. 程式人生 > >多執行緒下載時HTTP response code: 416 解決方案

多執行緒下載時HTTP response code: 416 解決方案

今天用java寫的多執行緒下載報錯,找了很久,發現是第一次請求伺服器時響應碼正常,開啟多執行緒部分的響應碼為416,響應碼對應的意思可以去這裡看看點選開啟連結。主要意思就是public void setRequestProperty (String field, String newValue)出現了錯誤,設定的下載位元組範圍和訪問檔案大小有出入,要麼就是上面確認各個執行緒的下載範圍出現問題,要麼就是該語句沒用對。將之前的每個執行緒下載位元組範圍都打印出來發現沒問題。後來檢視setRequestProperty相關資料發現是setRequestProperty("Range", "bytes:" + threadStart + "-" + threadEnd)這裡出錯了,bytes後面應該是=號而不是:

附上沒有完成斷點下載的多執行緒下載程式碼.

(我用的是某馬的74期的視訊學習的,而他的程式碼用的是:卻可以執行,有點鬱悶,,還有他的getFileName()方法中獲取/的索引後沒有+1就擷取檔名得到的是/feiq.exe, 那也是錯誤的,後面執行時會報錯拒絕訪問)

package com.lyle.study;

import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

public class UpDownload1 {

	public static String path = "http://192.168.56.1:8080/itheima74/feiq.exe";
	public static int threadCount = 3;

	public static void main(String[] args) {

		try {

			URL url = new URL(path);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("GET");
			connection.setConnectTimeout(1000 * 10);

			if (connection.getResponseCode() == 200) {
				long length = connection.getContentLength();

				RandomAccessFile randomAccessFile = new RandomAccessFile(new File(getFileName()),
						"rw");
				randomAccessFile.setLength(length);
				randomAccessFile.close();

				long threadSize = length / threadCount;

				for (int i = 0; i < threadCount; i++) {
					int threadId = i;
					long threadStart = threadId * threadSize;
					long threadEnd = (threadId == threadCount - 1 ? length - 1 : (threadId + 1)
							* threadSize - 1);

					new DoThreadOpen(threadStart, threadEnd).start();

				}

			}
		} catch (Exception e) {

			e.printStackTrace();
		}

	}

	public static class DoThreadOpen extends Thread {

		private long threadStart;
		private long threadEnd;

		public DoThreadOpen(long threadStart, long threadEnd) {
			this.threadEnd = threadEnd;
			this.threadStart = threadStart;
		}

		@Override
		public void run() {

			try {

				URL url = new URL(path);
				HttpURLConnection connection = (HttpURLConnection) url.openConnection();
				connection.setRequestMethod("GET");
				connection.setConnectTimeout(1000 * 10);

				connection.setRequestProperty("Range", "bytes=" + threadStart + "-" + threadEnd);

				int code = connection.getResponseCode();

				if (code == 206) {
					InputStream inputStream = connection.getInputStream();
					RandomAccessFile randomAccessFile = new RandomAccessFile(
							new File(getFileName()), "rw");
					randomAccessFile.seek(threadStart);

					byte[] buffer = new byte[1024];
					int len;
					while ((len = inputStream.read(buffer)) != -1) {
						randomAccessFile.write(buffer, 0, len);

					}
					
					inputStream.close();
					randomAccessFile.close();

				}

			} catch (Exception e) {

				e.printStackTrace();
			}

		}

	}

	private static String getFileName() {

		return path.substring(path.lastIndexOf("/") + 1);
	}

}