无法使用java下载文件

时间:2014-10-04 10:14:40

标签: java exception highcharts download

我试图从highcharts.com下载javascript库。如果我手动点击网址,我就能看到该文件。但是,如果我尝试使用我的程序,它将返回403错误。我可以下载其他js文件但不能下载高级图表。这是我的代码。

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 个答案:

答案 0 :(得分: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的缺失集。

答案 1 :(得分: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();

答案 2 :(得分:1)

我遇到了类似的问题,并且没有通过设置URLConnection的user-agent属性来解决。 但是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);