2014-10-04 73 views
1

我正尝试从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) 

回答

3

使用URLConnection的,你应该能够通过设置用户代理从Java访问请求的网页。以下应该工作:

 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 

错误是User-Agent的缺失集。

+0

@Vignesh戈帕拉克里希南我只是改变了用户代理值 – Alboz 2014-10-04 11:01:51

3

您还可以使用的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(); 
+0

+1感谢@JamesB。我尝试了一种解决方法并完成了它。 – 2014-10-06 10:52:57

1

我正面临着类似的问题,并没有通过设定的URLConnection的用户代理属性进行解析。 但是对HttpURLConnection的类型转换使它工作。

不起作用。给403

URLConnection hc = url.openConnection(); 
hc.setRequestProperty("User-Agent", "curl/7.43.0"); 
FileUtils.copyURLToFile(url, temp); 

作品

HttpURLConnection hc = (HttpURLConnection) url.openConnection(); 
hc.setRequestProperty("User-Agent", "curl/7.43.0"); 
FileUtils.copyURLToFile(url, temp);