Java tutorial
/* Copyright 2013, 2014 joshua.tee@gmail.com This file is part of wX. wX is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. wX is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with wX. If not, see <http://www.gnu.org/licenses/>. */ package joshuatee.wx; import java.io.FileOutputStream; import java.io.InputStream; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; import android.content.Context; public class UtilityFTP { public static void GetNids(Context c, String url, String path) { try { FTPClient ftp = new FTPClient(); //String url = "tgftp.nws.noaa.gov"; //String user = "ftp"; //String pass = "anonymous"; ftp.connect(url); if (!ftp.login("ftp", "anonymous")) { ftp.logout(); } ftp.setFileType(FTP.BINARY_FILE_TYPE); int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); } ftp.enterLocalPassiveMode(); ftp.changeWorkingDirectory(path); //reply = ftp.getReplyCode(); //String fn = "sn.last"; FileOutputStream fos = c.openFileOutput("nids", Context.MODE_PRIVATE); ftp.retrieveFile("sn.last", fos); //reply = ftp.getReplyCode(); fos.close(); ftp.logout(); ftp.disconnect(); } catch (Exception ex) { ex.printStackTrace(); } } public static String[] GetNidsArr(Context c, String url, String path, String frame_cnt_str) { int frame_cnt = Integer.parseInt(frame_cnt_str); String[] nids_arr = new String[frame_cnt]; try { FTPClient ftp = new FTPClient(); //String user = "ftp"; //String pass = "anonymous"; ftp.connect(url); if (!ftp.login("ftp", "anonymous")) { ftp.logout(); } ftp.setFileType(FTP.BINARY_FILE_TYPE); int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); } ftp.enterLocalPassiveMode(); ftp.changeWorkingDirectory(path); //reply = ftp.getReplyCode(); FTPFile[] ftpFiles = ftp.listFiles(); //get newest .xml file name from ftp server java.util.Date lastMod = ftpFiles[0].getTimestamp().getTime(); FTPFile choice = ftpFiles[0]; for (FTPFile file : ftpFiles) { if (file.getTimestamp().getTime().after(lastMod) && !file.getName().equals("sn.last")) { choice = file; lastMod = file.getTimestamp().getTime(); } } int seq = Integer.parseInt(choice.getName().replace("sn.", "")); // was ALl int j = 0; int k = seq - frame_cnt + 1; for (j = 0; j < frame_cnt; j++) { // files range from 0000 to 0250, if num is negative add 251 int tmp_k = k; if (tmp_k < 0) tmp_k = tmp_k + 251; nids_arr[j] = "sn." + String.format("%4s", Integer.toString(tmp_k)).replace(' ', '0'); k++; } FileOutputStream fos; for (j = 0; j < frame_cnt; j++) { fos = c.openFileOutput(nids_arr[j], Context.MODE_PRIVATE); ftp.retrieveFile(nids_arr[j], fos); fos.close(); } ftp.logout(); ftp.disconnect(); } catch (Exception ex) { ex.printStackTrace(); } return nids_arr; } public static String[] GetL2Arr(Context c, String base_url, String frame_cnt) { int frame_cnt_int = Integer.parseInt(frame_cnt); InputStream is = null; String[] l2_arr = new String[frame_cnt_int]; /* String radar_index_html = Utility.GetStringFromURLSep(base_url + "dir.list"); radar_index_html = radar_index_html.replace("<br>"," "); // was replaceAll String[] tmp_arr = radar_index_html.split(" ");*/ String[] tmp_arr = Utility.GetStringFromURLSep(base_url + "dir.list").replace("<br>", " ").split(" "); int arr_length = tmp_arr.length; int additional_add = 0; int fn_size = 1; int fn_prev_size = 1; try { fn_size = Integer.parseInt(tmp_arr[tmp_arr.length - 2]); fn_prev_size = Integer.parseInt(tmp_arr[tmp_arr.length - 4]); } catch (Exception e) { } float ratio = (float) fn_size / (float) fn_prev_size; if (ratio < 0.75) additional_add = 1; for (int i = 0; i < frame_cnt_int; i++) { l2_arr[i] = tmp_arr[arr_length - (frame_cnt_int - i + additional_add) * 2 + 1]; is = Utility.getInputStreamFromURL(base_url + l2_arr[i]); UtilityIO.SaveInputStream(c, is, l2_arr[i]); } return l2_arr; } public static String IowaMesoL2(String rid1) { String fn = ""; String rid_prefix = "K"; if (rid1.equals("JUA")) rid_prefix = "T"; if (rid1.equals("HKI") || rid1.equals("HMO") || rid1.equals("HKM") || rid1.equals("HWA") || rid1.equals("APD") || rid1.equals("ACG") || rid1.equals("AIH") || rid1.equals("AHG") || rid1.equals("AKC") || rid1.equals("ABC") || rid1.equals("AEC") || rid1.equals("GUA")) rid_prefix = "P"; final String base_url = "http://mesonet-nexrad.agron.iastate.edu/level2/raw/" + rid_prefix + rid1 + "/"; final String[] tmp_arr = Utility.GetStringFromURLSep(base_url + "dir.list").replace("<br>", " ").split(" "); fn = tmp_arr[tmp_arr.length - 1]; int fn_size = 1; String fn_prev = tmp_arr[tmp_arr.length - 3]; int fn_prev_size = 1; try { fn_size = Integer.parseInt(tmp_arr[tmp_arr.length - 2]); fn_prev_size = Integer.parseInt(tmp_arr[tmp_arr.length - 4]); } catch (Exception e) { } float ratio = (float) fn_size / (float) fn_prev_size; if (ratio < 0.75) { fn = fn_prev; } return base_url + fn; } }