我正在尝试在java代码中下载http文件。我一直得到例外403,我不确定具体原因。根据{{3}},必须有一种方法来获得确切的子状态错误。
任何方式我们都可以在Java中获得substatus错误,以便我可以相应地修复它?
private void downloadFile()
{
try
{
URL url = new URL("http://hostname/folder/fileName");
URLConnection connection = url.openConnection();
InputStream stream = connection.getInputStream();
BufferedInputStream in = new BufferedInputStream(stream);
int length = 0;
byte[] data = new byte[1024];
while ((length = in.read(data)) != -1)
{
System.out.write(data);
}
in.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
java.io.IOException:
Server returned HTTP response code: 403 for URL: http://hostname/folder/fileName
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)