Url file not found, but url works in browser

I am using Apache commons FileUtils to download a csv file and save it:

FileUtils.copyURLToFile(new URL("http://www.google.com/finance/historical?q=AAL&startdate=May+14%2C+2017&output=csv"), new File(toFilePrefix + "sadfsd.csv"), 10000, 10000);

      

I am getting the following error:

java.io.FileNotFoundException: http://www.google.com/finance/historical?q=AAL&startdate=May+14%2C+2017&output=csv

When I run the url in the browser, it works and the file is loaded.

If I change the url to get it on google it works:

http://www.google.com/finance/historical?q=GOOG&startdate=May+14%2C+2017&output=csv

Whole Stacktrace:

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
    Mon May 29 19:28:02 PDT 2017 WARN: Establishing SSL connection without server identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
    Mysql Connection has been established!
    MySQL JDBC Driver has been loaded ...
    java.io.FileNotFoundException: http://www.google.com/finance/historical?q=AAL&startdate=Mar+14+2017&output=csv
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1836)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
        at org.apache.commons.io.FileUtils.copyURLToFile(FileUtils.java:1506)
        at com.mycompany.myapp.AppFiles.downloadFiles(AppFiles.java:61)
        at com.mycompany.myapp.Main.main(Main.java:8)
    ------------------------------------------------------------------------
    BUILD SUCCESS
    ------------------------------------------------------------------------
    Total time: 0.980s
    Finished at: Mon May 29 19:28:03 PDT 2017
    Final Memory: 5M/245M
    ------------------------------------------------------------------------

      

I also tried downloading this file using the accepted answer from this question (using Java NIO) and the same error appears: How do I download and save a file from the internet using Java? ...

+3


source to share





All Articles