我正尝试从highcharts.com下载JavaScript库。如果我手动打开网址,我可以看到该文件。但如果我用我的程序尝试它,它会返回403错误。我可以下载其他js文件,但不能下载。这是我的代码。无法使用java下载文件
try {
String fileName = "highchartsjs"; //The file that will be saved on your computer
URL link = new URL("http://code.highcharts.com/highcharts.js"); //The file that you want to download
//Code to download
InputStream in = new BufferedInputStream(link.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1!=(n=in.read(buf)))
{
out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(response);
fos.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
我正面临着非常奇怪的错误。让我知道我错了什么地方,或者他们添加了一个政策,我们不能通过程序等下载。,谢谢你的期待。
参考添加异常跟踪:
java.io.IOException: Server returned HTTP response code: 403 for URL: http://code.highcharts.com/highcharts.js
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at com.visualbi.readjson.SaveMap.downloadFiles(SaveMap.java:52)
at com.visualbi.Boot.main(Boot.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.simontuffs.onejar.Boot.run(Boot.java:340)
at com.simontuffs.onejar.Boot.main(Boot.java:166)
@Vignesh戈帕拉克里希南我只是改变了用户代理值 – Alboz 2014-10-04 11:01:51