package th.co.muangthai.endrprint.util; import java.io.*; import java.net.URL; import java.text.ParseException; import java.util.Date; /** * Created by Matthaus on 8/24/15. */ public class DownloadFile { public static void download(String url, String fileName) throws IOException { // String fileName = "MTRAMEDAS2_MTRAM-WF.log.2015-04-29"; //The file that will be saved on your computer URL link = new URL(url); //The file that you want to download fileName = "C:\\Users\\Matthaus\\Downloads\\log\\" + fileName; //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(); //End download code System.out.println("Downloaded : " + fileName); } public static void main(String[] args) throws IOException, ParseException { // String[] appName = new String[]{"MTRAM", "MTRAM-WF", "MTRAMPh3"}; // String[] hostName = new String[]{"MTRAMEDAS1", "MTRAMEDAS2"}; String[] appName = new String[]{"MTRAM", "MTRAM-WF", "MTRAM_PROD"}; String[] hostName = new String[]{"uatappzone09", "uatappzone10"}; // String fileName = "MTRAMEDAS2_MTRAM-WF.log.2015-04-29"; //The file that will be saved on your computer // URL link = new URL("http://10.1.2.26:7103/MTRAMPh3/mtramlog.jsp?file=/LOGGING/MTRAM/Web/MTRAMEDAS2_MTRAM-WF.log.2015-04-29"); //The file that you want to download // fileName = "C:\\Users\\Matthaus\\Downloads\\log\\" + fileName; Date currentDate = DateUtil.toDate("28/04/2015"); while (!currentDate.equals(DateUtil.toDate("24/08/2015"))) { for (String host : hostName) { for (String app : appName) { // String url = "http://10.1.2.26:7103/MTRAMPh3/mtramlog.jsp?file=/LOGGING/MTRAM/Web/"; String url = "http://10.1.2.38:7113/MTRAM/mtramlog.jsp?file=/LOGGING/MTRAM/Web/"; String fileName = host + "_" + app + ".log." + DateUtil.toFormatString(currentDate, "yyyy-MM-dd"); url += fileName; try{ DownloadFile.download(url, fileName); } catch (IOException e) { System.out.println("file not found : " + fileName); } } } currentDate = DateUtil.plusOneDay(currentDate); } } }