public class URLDemo1 {
public static void main(String[] args) throws Exception {
//下载地址
URL url = new URL("https://localhost:8080/fanbin/screct.txt");
//连接到这个资源 HTTP
HttpsURLConnection urlConnection =(HttpsURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fs = new FileOutputStream("screct.txt");
byte[] bytes = new byte[1024];
int len;
while ((len=inputStream.read(bytes))!=-1){
fs.write(bytes,0,len);
}
fs.close();
inputStream.close();
urlConnection.disconnect();//断开连接
}
}
解决
将(HttpsURLConnection)变为(HttpURLConnection)
public class URLDemo1 {
public static void main(String[] args) throws Exception {
//下载地址
URL url = new URL("http://localhost:8080/fanbin/screct.txt");
//连接到这个资源 HTTP
HttpURLConnection urlConnection =(HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fs = new FileOutputStream("screct.txt");
byte[] bytes = new byte[1024];
int len;
while ((len=inputStream.read(bytes))!=-1){
fs.write(bytes,0,len);
}
fs.close();
inputStream.close();
urlConnection.disconnect();//断开连接
}
}