Files are getting corrupted when download using ftp in java -
files getting corrupted when trying pull compressed files. here code have used. not able understand going wrong. format of files .zip , inside has xml files. after downloading remote server,xml files tags changed , looks corrupted.
public boolean pullconfirmationsftp(string host, string sftpusername, string sftppwd, string sftpport, string fromconfirmationdirectory, string archiveconfirmationdirectory, string todirectory) { try { // new ftp client ftpclient ftp = new ftpclient(); // try connect ftp.connect(host); // login server if (!ftp.login(sftpusername, sftppwd)) { ftp.logout(); log4j.error("authentication failed"); } int reply = ftp.getreplycode(); // ftpreply stores set of constants ftp reply codes. if (!ftpreply.ispositivecompletion(reply)) { ftp.disconnect(); } // enter passive mode ftp.enterlocalpassivemode(); // system name // system.out.println("remote system " + ftp.getsystemtype()); // change current directory ftp.changeworkingdirectory(fromconfirmationdirectory); system.out.println("current directory " + ftp.printworkingdirectory()); // list of filenames ftpfile[] ftpfiles = ftp.listfiles(); if (ftpfiles != null && ftpfiles.length > 0) { // loop thru files (ftpfile file : ftpfiles) { try{ if (!file.isfile()) { continue; } log4j.error("file " + file.getname()); // output stream outputstream output; output = new fileoutputstream(todirectory + file.getname()); // file remote system ftp.retrievefile(file.getname(), output); // close output stream output.close(); // delete file ftp.deletefile(file.getname()); } catch(exception e) { log4j.error("error in pushing file : ",e); } } } ftp.logout(); ftp.disconnect(); } catch (exception ex) { ex.printstacktrace(); log4j.error(ex); } return true; }
you didn't ftpclient use, i'm guessing apache commons. documentation ftpclient says:
the default settings ftpclient use ftp.ascii_file_type , ftp.non_print_text_format , ftp.stream_transfer_mode , , ftp.file_structure . file types directly supported ftp.ascii_file_type , ftp.binary_file_type
as zip-file binary file have add
ftp.setfiletype(ftp.binary_file_type);
Comments
Post a Comment