我正在尝试从https URL下载文件。我可以使用以下代码从家用PC下载文件。但是,我无法从公司计算机上下载它。必需的cacerts
已正确导入Java。我也可以从浏览器下载文件。我查看了其他线程的数量,但无法找出解决该问题的适当方法。这是我的Java代码
try
{
url = new URL(url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK)
inputStream = httpURLConnection.getInputStream();
else
inputStream = httpURLConnection.getErrorStream();
}
catch (Exception ioe)
{
ioe.printStackTrace();
}
这是我得到的例外。我总是在inputstream
遇到例外。请记住,此代码在我的家用PC上可以100%工作。
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.read(InputRecord.java:503)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:975)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:933)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:735)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:678)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:706)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1593)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:352)
任何线索都会有很大帮助。
答案 0 :(得分:1)
对我来说很好:
System.setProperty("http.proxyHost", "host");
System.setProperty("http.proxyPort", "port");
try {
URL u = new URL("some url");
HttpURLConnection huc = (HttpURLConnection) u.openConnection();
InputStream is = null;
if (huc.getResponseCode() == HttpURLConnection.HTTP_OK)
is = huc.getInputStream();
else
is = huc.getErrorStream();
} catch (Exception e) {
e.printStackTrace();
}