[英]Unable to download a file using java
I'm trying to download javascript libraries from highcharts.com. 我正在尝试从highcharts.com下载javascript库。 If I hit the url manually I'm able to see the file. 如果我手动点击该网址,则可以看到该文件。 But if I try it with my program, it returns 403 error. 但是,如果我在程序中尝试使用它,则会返回403错误。 I'm able to download other js files but not highcharts. 我可以下载其他js文件,但不能下载highcharts。 Here is my code. 这是我的代码。
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();
}
Very strange error I'm facing. 我面临的非常奇怪的错误。 Let me know where I went wrong or they added a policy that we can't download it through program etc.., Thanks in anticipation. 让我知道我哪里出了错,或者他们添加了一个我们无法通过程序等下载的策略。,谢谢。
Added Exception trace for reference: 添加了异常跟踪以供参考:
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)
Use URLConnection and you should be able to access the requested web page from java by setting the user agent. 使用URLConnection,您应该能够通过设置用户代理从java访问请求的网页。 The following should work: 以下应该工作:
URL url = new URL("http://code.highcharts.com/highcharts.js");
URLConnection urlConn = url.openConnection();
urlConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.29 Safari/537.36");
InputStream is = urlConn.getInputStream();
//Do what you want to do with the InputStream
The error was the missing set of the User-Agent
. 错误是缺少User-Agent
集合。
You could also use HttpClient: http://hc.apache.org/ 您还可以使用HttpClient: http : //hc.apache.org/
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(result.toString().getBytes());
fos.close();
I was facing a similar problem and was not resolved by setting user-agent property of URLConnection. 我面临类似的问题,无法通过设置URLConnection的user-agent属性来解决。 But typecast to HttpURLConnection made it work. 但是对HttpURLConnection的类型转换使它可以工作。
Does not work. 不起作用。 Gives 403. 给出403。
URLConnection hc = url.openConnection();
hc.setRequestProperty("User-Agent", "curl/7.43.0");
FileUtils.copyURLToFile(url, temp);
Works 作品
HttpURLConnection hc = (HttpURLConnection) url.openConnection();
hc.setRequestProperty("User-Agent", "curl/7.43.0");
FileUtils.copyURLToFile(url, temp);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.