问题
Consider a code snippet (look at first line next to main System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
):
package com.zip;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class ParallelDownload {
public static class Download {
public static String FILEPATH = "https://server.com/myfile.zip"; //about 60Gb
public static String DESTINATION = "C:\\!deleteme\\file.zip";
// The number of threads to download
public static int THREADCOUNT = 20;
public static void main(String[] args) throws Exception {
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
URL url = new URL(FILEPATH);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("GET");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36");
conn.setRequestProperty("Connection", "keep-alive");
if (conn.getResponseCode() == 200) {
Long contentLength = Long.parseLong(conn.getHeaderFields().get("Content-Length").get(0));
long blocksize = contentLength / THREADCOUNT;
RandomAccessFile randomAccessFile = new RandomAccessFile(new File(Download.DESTINATION + Download.getFileName(Download.FILEPATH)), "rwd");
randomAccessFile.setLength(contentLength);
randomAccessFile.close();
conn.disconnect();
for (long i = 0; i <THREADCOUNT; i++) {
long startpos = i * blocksize;
long endpos = (i + 1) * blocksize - 1;
if (i == THREADCOUNT - 1) {
endpos = contentLength;
}
new Thread(new DownloadTask(i, startpos, endpos)).start();
}
} else {
System.out.println("Server error!");
}
}
public static String getFileName(String path) {
return path.substring(path.lastIndexOf("/") + 1, path.length());
}
}
// Multithreaded task
public static class DownloadTask implements Runnable {
public DownloadTask(long id, long startpos, long endpos) {
this.id = id;
this.startpos = startpos;
this.endpos = endpos;
}
long id;
long startpos;
long endpos;
@Override
public void run() {
try {
URL url = new URL(Download.FILEPATH);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(5 * 1000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Range", "bytes=" + startpos + "-" + endpos + "");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36");
conn.setRequestProperty("Connection", "keep-alive");
// HTTP Status-Code 206: Partial Content.
if (conn.getResponseCode() == 206) {
InputStream is = conn.getInputStream();
RandomAccessFile randomAccessFile = new RandomAccessFile(new File(Download.DESTINATION + Download.getFileName(Download.FILEPATH)), "rwd");
// Setting the file pointer at the beginning of the file offset is measured by the occurrence, a read or write operation in this position.
randomAccessFile.seek(startpos);
int len = 0;
byte[] buffer = new byte[1024*10];
while ((len = is.read(buffer)) != -1) {
randomAccessFile.write(buffer, 0, len);
}
is.close();
randomAccessFile.close();
conn.disconnect();
System.out.println("Thread "+ Thread.currentThread().getId() +": Download");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
About half of threads failed with javax.net.ssl.SSLException: SSL peer shut down incorrectly
.
How fix this?
来源:https://stackoverflow.com/questions/60153575/why-i-got-sslexception-even-when-i-set-https-protocols