java - Ftp Downloaded file size is 0 KB -
i'm trying download files in ftp path local system. downloaded files in 0 kb. problem. please me find bug. (files of type *.zip, *.pdf, *.xml)
thanks in advance.
package connectftp; import java.io.fileoutputstream; import java.io.ioexception; import java.util.calendar; import java.util.logging.level; import java.util.logging.logger; 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; public class ftpdownloader { ftpclient ftp = null; public ftpdownloader(string host, string user, string pwd) throws exception { ftp = new ftpclient(); int reply; ftp.connect(host); reply = ftp.getreplycode(); if (!ftpreply.ispositivecompletion(reply)) { ftp.disconnect(); throw new exception("exception in connecting ftp server"); } ftp.login(user, pwd); ftp.setfiletype(ftp.binary_file_type); ftp.enterlocalpassivemode(); } public void downloadfile(string remotefilepath, string localfilepath) { try (fileoutputstream fos = new fileoutputstream(localfilepath)) { this.ftp.retrievefile(remotefilepath, fos); } catch (ioexception ioe) { ioe.printstacktrace(); } } public void disconnect() { if (this.ftp.isconnected()) { try { this.ftp.logout(); this.ftp.disconnect(); } catch (ioexception ioe) { } } } public static void main(string[] args) { try { ftpdownloader ftpdownloader = new ftpdownloader("192.168.3.1", "adminuser", "password"); ftpdownloader.listfolders(); system.out.println("file downloaded successfully"); ftpdownloader.disconnect(); } catch (exception e) { e.printstacktrace(); } } public void listfolders() { try { string ftpbasepath = "dev"; ftpfile[] filelist = ftp.listdirectories("/" + ftpbasepath); (ftpfile file : filelist) { listfiles(file.getname()); } } catch (ioexception ex) { logger.getlogger(ftpdownloader.class.getname()).log(level.severe, null, ex); } } public void listfiles(string path) { try { ftpfile[] filelist = ftp.listfiles(path); (ftpfile file : filelist) { string currentfilename = file.getname(); string localpath = "e:\\ftpfiles\\"+currentfilename; try (fileoutputstream fos = new fileoutputstream(localpath)) { this.ftp.retrievefile(currentfilename, fos); } catch (ioexception ioe) { ioe.printstacktrace(); } } } catch (ioexception ioe) { logger.getlogger(ftpdownloader.class.getname()).log(level.severe, null, ioe); } } }
here solution question. have modified code little changes. given code files in given ftp path , stores in local directory folder wise structure.
the output structure example:
e:\ftpfiles\2013\november\11222013\*.*
*.* files stored folder structure in ftp
operating system : windows 2008 server (64 bit)
however work in windows ftp servers , move file local system or given path.
thanks.
package connectftp; import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.file; import java.io.fileoutputstream; import java.io.filereader; import java.io.filewriter; import java.io.outputstream; import java.text.simpledateformat; import java.util.date; import org.apache.commons.net.ftp.ftpclient; import org.apache.commons.net.ftp.ftpfile; import org.apache.commons.net.ftp.ftpreply; class uftp { public static void main(string[] args) { uftp unftp = new uftp(); unftp.unfile(); } private void unfile() { try { //current date, month, year starts date mmddyyyy = new date(); string today = new simpledateformat("mmddyyyy").format(mmddyyyy); string cyear = today.substring(4, 8); string month[] = {"january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"}; string cmonths = today.substring(0, 2); int cmonthi = integer.parseint(cmonths); cmonths = month[cmonthi - 1]; system.out.println("month : " + cmonths); system.out.println("year : " + cyear); //current date, month, year ends ftpclient ftp = new ftpclient(); ftp.connect("192.168.3.5"); if (!ftp.login("dev", "password")) { ftp.logout(); } int reply = ftp.getreplycode(); if (!ftpreply.ispositivecompletion(reply)) { ftp.disconnect(); } ftp.enterlocalpassivemode(); string ftpbasepath = "devpath"; //ftp base directory string localpath = "e:\\ftpfiles\\"; //create dir tree //create year dir file newdir = new file(localpath + cyear); if (!newdir.exists()) { system.out.println("creating directory: " + today); boolean result = newdir.mkdir(); if (result) { system.out.println("year dir created"); } } localpath = localpath + cyear + "\\"; //create month dir newdir = new file(localpath + cmonths); if (!newdir.exists()) { system.out.println("creating directory: " + today); boolean result = newdir.mkdir(); if (result) { system.out.println("month dir created"); } } localpath = localpath + cmonths + "\\"; //create date dir newdir = new file(localpath + today); if (!newdir.exists()) { system.out.println("creating directory: " + today); boolean result = newdir.mkdir(); if (result) { system.out.println("today dir created"); } } //creates today's dir //destinationbasepath = "e:\\ftpfiles\\" + today + "\\"; localpath = localpath + today + "\\"; ftpfile[] filelist = ftp.listdirectories("/" + ftpbasepath); (ftpfile directory : filelist) { system.out.println(directory.getname()); string currentdir = directory.getname(); ftpfile[] ftpfiles = ftp.listfiles("/" + ftpbasepath + "/" + currentdir + "/"); if (ftpfiles != null && ftpfiles.length > 0) { (ftpfile file : ftpfiles) { if (!file.isfile()) { continue; } system.out.println("file " + file.getname()); outputstream output; //create dir starts newdir = new file(localpath + currentdir); // if directory not exist, create if (!newdir.exists()) { system.out.println("creating directory: " + currentdir); boolean result = newdir.mkdir(); if (result) { system.out.println("dir created"); } } //create dir ends output = new fileoutputstream(localpath + currentdir + "\\" + file.getname()); ftp.retrievefile("/" + ftpbasepath + "/" + currentdir + "/" + file.getname(), output); output.flush(); output.close(); } } } ftp.logout(); ftp.disconnect(); } catch (exception ex) { ex.printstacktrace(); } } }
Comments
Post a Comment