我正在尝试在 java 代码中下载 http 上的文件。我不断收到异常 403,我不确定具体原因。根据维基百科的文章,必须有一种方法可以得到准确的子状态错误。
有什么方法可以让我们在 Java 中得到子状态错误,以便我可以相应地修复它?
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)