java - apache ftpclient retrievefile() downloading corrupted file -
i using org.apache.commons.net.ftp.ftpclient downloading files ftp server.
it downloads files ftp server except files names test.xml test.txt west.xml etc., files these names getting downloaded no data inside file. retrievefile() method returning boolean false these files.
i tried download same file renaming name manually on ftp server. worked other names.
please let me know how solve problem.
edit- adding sample code
public static boolean downloadftpdir(string localdir, ftpclient ftpclient) { outputstream output = null; try { ftpclient.setfiletype(ftp.binary_file_type); /* * use passive mode default because of behind * firewalls these days. */ ftpclient.enterlocalpassivemode(); ftpfile[] filelist = ftpclient.listfiles(); (ftpfile ftpfile : filelist) { if(ftpfile.isdirectory()){ string tempdir = localdir + file.separatorchar+ftpfile.getname(); try { file temp = new file(tempdir); temp.mkdirs(); } catch (exception e) { system.out.println("could not create local directory "+tempdir); return false; } if(ftpclient.changeworkingdirectory(ftpfile.getname())) { downloadftpdir(tempdir, ftpclient); } else { system.out.println("could change directory "+ftpfile.getname()+" on ftp server"); return false; } } else { output = new fileoutputstream(localdir + file.separatorchar + ftpfile.getname()); if (!ftpclient.retrievefile(ftpfile.getname(), output)) { system.out.println("unable download file ftp server : " + ftpfile.getname()); file tmp = null; try { output.close(); /*tmp = new file(localdir + file.separatorchar + ftpfile.getname()); tmp.delete(); logger.info("deleted corrupt downloaded file : " + tmp.getabsolutepath());*/ } catch (ftpconnectionclosedexception e) { system.out.println("connection ftp server closed "); return false; } catch (exception e1) { system.out.println("unable delete corrupt file local directory : " + tmp.getabsolutepath()); return false; } } output.close(); system.out.println("ftp file download successful : " + ftpfile.getname()); } } } catch (ftpconnectionclosedexception e) { e.printstacktrace(); return false; } catch (filenotfoundexception e1) { e1.printstacktrace(); return false; } catch (ioexception e2) { e2.printstacktrace(); return false; } catch (exception e3) { try { output.close(); } catch (exception e4) { e3.printstacktrace(); } return false; } finally{ try { if(output!=null) output.close(); } catch (exception e5) { e5.printstacktrace(); } } return true; } public static void main(string[] args) { ftpclient ftpclient = new ftpclient(); try { ftpclient.connect("ftp ip", 21); system.out.println("connecting"); if(ftpreply.ispositivecompletion(ftpclient.getreply())) { system.out.println("connected"); if(!ftpclient.login("ftp username", "ftp password")) { system.out.println("could not login"); ftpclient.disconnect(); return; } system.out.println("logged in"); string remotepath = "ftp directory path"; stringtokenizer st = new stringtokenizer(remotepath, "/"); string dir = null; boolean status = false; while (st.hasmoretokens()) { dir = st.nexttoken(); status = ftpclient.changeworkingdirectory(dir); if (!status) { system.out.println("ftp client not able change current directory " + dir); return ; } } system.out.println("connected"); downloadftpdir("local path", ftpclient); } else { ftpclient.disconnect(); } } catch (socketexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } }
Comments
Post a Comment