简体   繁体   中英

Get automatically a CSV from external server using Spring MVC

I have a scheduled task (using cron) inside my Spring MVC application. Inside the programmed task I have to get a CSV from an external server in the following link:

http://www.aemet.es/es/eltiempo/observacion/ultimosdatos_6172O_datos-horarios.csv?k=and&l=6172O&datos=det&w=0&f=temperatura&x=h24

And once I get it I have to parse it. The problem comes when getting the file, as when I click on the previous link I can download it to my computer, but I don't know how to do that using Spring.... can you give me I hint??

UPDATE: I don't have any code yet, but I guess that must be something similar to the following code:

URL stockURL = new URL("http://example.com/stock.csv"); BufferedReader in = new BufferedReader(new InputStreamReader(stockUrl.openStream())); CSVReader reader = new CSVReader(in);

But the problem is that my URL is not exactly a .csv. Whe I put the URL in a browser it looks like it is a redirect.

Thank you very much indeed.

Thank you all for your comments. Even if the URL doesn't have a CSV extension, i tried the following code (in Java, not Spring) but it works!!

URL stockURL = new URL("http://www.aemet.es/es/eltiempo/observacion/ultimosdatos_6172O_datos-horarios.csv?k=and&l=6172O&datos=det&w=0&f=temperatura&x=h24");
    BufferedReader in = new BufferedReader(new InputStreamReader(stockURL.openStream()));
    //CSVReader reader = new CSVReader(in);

    String line;
    while((line = in.readLine()) != null){
        System.out.println(line);

    }

So,I guess that using Spring is going to be really very similar to get the file, so thank you very much everybody!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM