调用HTTP下载文件401错误
public static boolean httpDownload(String httpUrl, String saveFile) {
// int bytesum = 0;
int byteread = 0;
URL url = null;
try {
url = new URL(httpUrl);
java.net.Authenticator.setDefault(new java.net.Authenticator(){
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("wcadmin", "wcadmin".toCharArray());
}
});
} catch (MalformedURLException e1) {
e1.printStackTrace();
return false;
}
try {
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
FileOutputStream fs = new FileOutputStream(saveFile);
byte[] buffer = new byte[1204];
while ((byteread = inStream.read(buffer)) != -1) {
// bytesum += byteread;
// System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
原文作者:tiantangqiu
原文地址: https://blog.csdn.net/tiantangqiu/article/details/84905702
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/tiantangqiu/article/details/84905702
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
相关文章