java - Uploading blank file when FTP path has more than one subdirectory -
below code uploads file on ftp server
public class uploadfile { static resourcebundle rsbundle = resourcebundle.getbundle("com.mindcraft.resources.resources"); public void upload(string host,string username,string pwd,string inputfile,string uploadpath,string zip_filename) { //string zip_file_name= rsbundle.getstring("zip_filename"); ftpclient ftp=new ftpclient(); try { int reply; ftp.connect(host); ftp.login(username, pwd); reply = ftp.getreplycode(); system.out.println("reply1" + reply); if(!ftpreply.ispositivecompletion(reply)) { ftp.disconnect(); } system.out.println("ftp server connected."); ftp.setfiletype(ftp.binary_file_type); inputstream input= new fileinputstream(inputfile); system.out.println("directory.." + ftp.printworkingdirectory()); string dirtree=uploadpath; boolean direxists = true; string[] directories = dirtree.split("/"); (string dir : directories ) { if (!dir.isempty() ) { if (direxists) { direxists = ftp.changeworkingdirectory(dir); ftp.storefile(dirtree+zip_filename,input); system.out.println("1.."); } if (!direxists) { system.out.println("dir tree" + ftp.printworkingdirectory()); if (!ftp.makedirectory(dir)) { throw new ioexception("unable create remote directory '" + dir + "'. error='" + ftp.getreplystring()+"'"); } if (!ftp.changeworkingdirectory(dir)) { throw new ioexception("unable change newly created remote directory '" + dir + "'. error='" + ftp.getreplystring()+"'"); } system.out.println("dir tree" + ftp.printworkingdirectory()); ftp.storefile(dirtree+zip_filename,input); } } } system.out.println( ftp.getreplystring() ); input.close(); ftp.logout(); } catch(exception e) { system.out.println("err"+ e); e.printstacktrace(); } { if(ftp.isconnected()) { try { ftp.disconnect(); } catch(exception ioe) { } } } } }
it works fine when upload path has 1 folder eg. /folder1/
but uploads blank file of byte 0 when there subfolder or more 1 directory eg. /folder1/folder2/
what can issue??
ftp.storefile(dirtree+zip_filename,input);
should called after for
creating subdirectories, , going correct directory.
btw have helped introducing function makeandgotodirectory.
Comments
Post a Comment