调用url.openConnection().getInputStream()下载文件时报java.io.IOException

ppmmwozuiai 2013-09-12 04:34:20
错误信息前两行是:
java.io.IOException: Server returned HTTP response code: 401 for URL: http://localhost/jsp/downloadtmp/126806114/6.zip
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)

代码:

			try {
URL url = new URL(BASE_URL + requestNumber + "/" + fileName);
File outFile = new File(path + File.separator + fileName);
os = new FileOutputStream(outFile);
is = url.openConnection().getInputStream();
byte[] buff = new byte[1024];
while (true) {
int readed = is.read(buff);
System.out.println("--------------" + buff.length);
if (readed == -1) {
break;
}
byte[] temp = new byte[readed];
System.arraycopy(buff, 0, temp, 0, readed);
os.write(temp);
}

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


不太明白为什么第五行会报错。。
...全文
563 3 打赏 收藏 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
tony4geek 2013-09-12
  • 打赏
  • 举报
回复
对的。应该设置身份认证/
ppmmwozuiai 2013-09-12
  • 打赏
  • 举报
回复
我明白了。网站有身份验证。在调用之前必须现经过认证才行。
RAY_MAO 2013-09-12
  • 打赏
  • 举报
回复
Server returned HTTP response code: 401 只认识404 不认识 401了啊

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧