隨便找篇ppt來當下載的來源
如果你是該作者覺得這樣很不妥歡迎跟我聯繫xd
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
public class Test {
public static void main(String []args) throws InterruptedException, NoSuchAlgorithmException, IOException, KeyManagementException {
downloadFile("https://c.nknu.edu.tw/affair/UploadFile/Files/Files/envedu_1117163234.ppt");
}
private final static String CMD = "rundll32 url.dll,FileProtocolHandler %s";
public static void openFile(String path) throws IOException, InterruptedException {
if (new File(path).exists() == false) throw new IOException("No File");
Process process = Runtime.getRuntime().exec(String.format(CMD, path));
process.waitFor();
}
private final static String DOWNLOAD_FOLDER = "C:/Users/C.Y.Fang/Downloads/%s";
public static void downloadFile(String url) throws IOException, NoSuchAlgorithmException, InterruptedException, KeyManagementException {
String[] array = url.split("/");
String fileName = array[array.length - 1];
array = null;
System.out.println(fileName);
boolean isHttps = url.toUpperCase().contains("HTTPS") ? true : false;
if (isHttps) {
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
}
URL u = new URL(url);
ReadableByteChannel readableByteChannel = Channels.newChannel(u.openStream());
String path = String.format(DOWNLOAD_FOLDER, fileName);
FileOutputStream fileOutputStream = new FileOutputStream(path);
fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
fileOutputStream.flush();
fileOutputStream.close();
fileOutputStream = null;
readableByteChannel.close();
readableByteChannel = null;
openFile(path);
}
static TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
}
執行結果:
參考資料:
https://docs.microsoft.com/zh-tw/windows/desktop/api/shellapi/nf-shellapi-shellexecutea
https://confluence.atlassian.com/stashkb/could-not-generate-dh-keypair-on-ssl-715129360.html