java - file content get reduced when transfering to ftp server -
i wrote java program recursively upload directory remote ftp server using apache.commons.net library.by program files got uploaded remote ftp server expected.but html,js,svg,ftl file content getting reduced.also jpeg,png,icon file sizes different original.i compare original directory , uploaded directory after uploading using meld software can figure out directory content got corrupted.i have tried both binary , ascii transfer mode.both 2 modes getting same problem.so can 1 suggest reason why happening.is issue ftp file transferring
import java.io.file; import java.io.fileinputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import org.apache.commons.io.filenameutils; import org.apache.commons.net.ftp.ftp; import org.apache.commons.net.ftp.ftpclient; public class ftpclient3 { public static boolean uploadsinglefile(ftpclient ftpclient, string localfilepath, string remotefilepath) throws ioexception { file localfile = new file(localfilepath); inputstream inputstream = new fileinputstream(localfile); string ext1 = filenameutils.getextension(localfilepath); try { if(ext1== "ajx" || ext1== "am" || ext1=="asa" || ext1=="properties" || ext1=="asc" || ext1=="asp" || ext1=="aspx" || ext1=="awk" || ext1=="bat" || ext1=="c" || ext1=="cdf" || ext1=="cf" || ext1=="cfg" || ext1=="cfm" || ext1=="cgi" || ext1=="cnf" || ext1=="conf" || ext1=="cpp" || ext1=="css" || ext1=="csv" || ext1=="ctl" || ext1=="dat" || ext1=="dhtml"|| ext1=="diz" || ext1=="file" || ext1=="forward" || ext1=="grp" || ext1=="h" || ext1=="hpp"|| ext1=="hqx" || ext1=="hta" || ext1=="htaccess" || ext1=="htc" || ext1=="htm" || ext1=="html" || ext1=="htpasswd" || ext1=="htt" || ext1=="htx"|| ext1=="in"|| ext1=="inc"|| ext1=="info"|| ext1=="ini"|| ext1=="ink"|| ext1=="java" || ext1=="js" || ext1=="jsp" || ext1=="log" || ext1=="logfile"|| ext1=="m3u"|| ext1=="m4a"|| ext1=="m4a"|| ext1=="mak"|| ext1=="map"|| ext1=="model"|| ext1=="msg"|| ext1=="nfo"|| ext1=="nsi" || ext1=="info" || ext1=="old" || ext1=="pas" || ext1=="patch" || ext1=="perl" || ext1=="php"|| ext1=="php2"|| ext1=="php3" || ext1=="php4" || ext1=="php5" || ext1=="php6" || ext1=="phtml" || ext1=="pix" || ext1=="pl" || ext1=="pm" || ext1=="po"|| ext1=="pwd"|| ext1=="py" || ext1=="qmail" || ext1=="rb" || ext1=="rbl" || ext1=="rbw" || ext1=="readme" || ext1=="reg" || ext1=="rss" || ext1=="ruby" || ext1=="session" || ext1=="setup" || ext1=="sh" || ext1=="shtm" || ext1=="shtml" || ext1=="sql" || ext1=="ssh" || ext1=="stm" || ext1=="style" || ext1=="svg" || ext1=="tcl" || ext1=="text" || ext1=="threads" || ext1=="tmpl" || ext1=="tpl" || ext1=="txt" || ext1=="ubb" || ext1=="vbs" || ext1=="xhtml" || ext1=="xml" || ext1=="xrc" || ext1=="xsl" || ext1=="rtf"){ ftpclient.setfiletype(ftp.ascii_file_type); ftpclient.setfiletransfermode(ftpclient.ascii_file_type); } else{ ftpclient.setfiletype(ftp.binary_file_type); ftpclient.setfiletransfermode(ftpclient.binary_file_type); } return ftpclient.storefile(remotefilepath, inputstream); } catch (exception e) { system.out.println("error: " + e.getmessage()); e.printstacktrace(); throw e; } { inputstream.close(); } } public static void uploaddirectory(ftpclient ftpclient, string remotedirpath, string localparentdir, string remoteparentdir) throws ioexception { system.out.println("listing directory: " + localparentdir); file localdir = new file(localparentdir); file[] subfiles = localdir.listfiles(); if (subfiles != null && subfiles.length > 0) { (file item : subfiles) { string remotefilepath = remotedirpath + "/" + remoteparentdir + "/" + item.getname(); if (remoteparentdir.equals("")) { remotefilepath = remotedirpath + "/" + item.getname(); } if (item.isfile()) { // upload file string localfilepath = item.getabsolutepath(); system.out.println("about upload file: " + localfilepath); //changing boolean uploaded = uploadsinglefile(ftpclient, localfilepath, remotefilepath); if (uploaded) { system.out.println("uploaded file to: " + remotefilepath); } else { system.out.println("could not upload file: " + localfilepath); } } else { // create directory on server boolean created = ftpclient.makedirectory(remotefilepath); if (created) { system.out.println("created directory: " + remotefilepath); } else { system.out.println("could not create directory: " + remotefilepath); } // upload sub directory string parent = remoteparentdir + "/" + item.getname(); if (remoteparentdir.equals("")) { parent = item.getname(); } localparentdir = item.getabsolutepath(); uploaddirectory(ftpclient, remotedirpath, localparentdir, parent); } } } } public static void main(string args[])throws exception{ string server = ""; int port = 21; string user = ""; string pass = ""; ftpclient ftpclient = new ftpclient(); try { // connect , login server ftpclient.connect(server, port); ftpclient.login(user, pass); // use local passive mode pass firewall ftpclient.enterlocalpassivemode(); system.out.println("connected"); string remotedirpath = "/dist"; string localdirpath ="/data/iot/ase-core"; ftpclient3.uploaddirectory(ftpclient, remotedirpath, localdirpath, ""); // log out , disconnect server ftpclient.logout(); ftpclient.disconnect(); system.out.println("disconnected"); } catch (ioexception ex) { ex.printstacktrace(); } }
}
wiki
Comments
Post a Comment