HomeBlogBooksProjectsArchiveAboutlogo

A Curious Animal

Born to be curious, born to be animal!

Downloading files from AEMET FTP server with Java and Apache Commons Net

06 March, 2011
- 8 min read

Some time ago the AEMET, the Spanish government meteorological agency, release many of its data publicly: weather radar, weather stations, lightnings, ... The way they do is simple and effective: public the contents on a FTP server and update on regular intervals of time. Talking with a colleague we decide to create (on our very few free time) a simple web page that allows people to see the current values of the weather stations. A map of Spain with some dots representing weather stations and a graph with showing data from the selected station. Update: This service was stopped in 2012 by Spanish government 😔 File localFolderObservacions = new File(localFolder, "observaciones_diezminutales"); localFolderObservacions.mkdir(); } // Connect to server ftpclient.connect(server); ftpclient.setFileTransferMode(FTPClient.BINARYFILETYPE); // Loggin if (!ftpclient.login("anonymous", null)) { logger.severe("Can't log into FTP"); return; } // Change directory if (!ftpclient.changeWorkingDirectory(folder)) { logger.log(Level.SEVERE, "Can''t change to folder ''{0}''.", folder); return; } // Change to day directory String remoteDayFolder = Utils.getStringFromDate(this.date) + "_diezminutales"; if (!ftpclient.changeWorkingDirectory(remoteDayFolder)) { logger.log(Level.SEVERE, "Can''t change to day folder ''{0}''.", remoteDayFolder); return; } } fos = new FileOutputStream(localfile); ftpclient.retrieveFile(ftpfile.getName(), fos); logger.log(Level.INFO, "Downloaded finished at ''{0}'' , size:''{1} ''bytes , timestamp: ''{2}''.", new Object[]{Utils.getCurrentFormattedDate(), ftpfile.getSize(), ftpfile.getTimestamp().getTime()}); // Uncompress file String targetName = localfile.getName().replaceAll(".gz", ""); File targetlocalfile = new File(localfile.getParentFile(), targetName); if (Utils.uncompressGzFile(localfile, targetlocalfile)) { // // TODO - Here you can handle the file. } else { // If there is any error uncompressing file then remove files to // ensure it will be downloaded again. localfile.delete(); targetlocalfile.delete(); }