日期:2014-05-20  浏览次数:21098 次

调用url.openConnection().getInputStream()下载文件时报java.io.IOException
本帖最后由 ppmmwozuiai 于 2013-09-12 16:36:07 编辑
错误信息前两行是:
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();
}
}


不太明白为什么第五行会报错。。
URL IOException

------解决方案--------------------
Server returned HTTP response code: 401
只认识404 不认识 401了啊
------解决方案--------------------
对的。应该设置身份认证/