在下面的代码中,运行hConn.disconnect();
。我以为responseCode
得-1。我该如何纠正? setRequestMethod("GET")
是否必要?
public static void downloadFile(String fileURL, String saveDirectory)
throws IOException {
URL u = new URL(fileURL);
HttpURLConnection hConn = (HttpURLConnection) u.openConnection();
int responseCode = hConn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
//some code here
//some codes here
} else {
System.out.println("No file to download. Server replied HTTP code: " + responseCode);
}
hConn.disconnect();
}