Tuesday, 15 July 2014

java - Coldfusion 11 Apache Commons 3.0.1 FTPClient RetrieveFileStream() returns null -



java - Coldfusion 11 Apache Commons 3.0.1 FTPClient RetrieveFileStream() returns null -

i'm creating class utilize in coldfusion replace cfftp tag can ftp on ssl. have custom tag interacts class, using either ftpclient or ftpsclient. connecting, logging in, putting file, changing directories, listing files work, cannot file life of me. have tried both retrievefile() , retrievefilestream methods , neither works. below getfile method implemented retrievefilestream() , inputstream null, no matter what. file there , permissions good. have no thought @ point. can connect , file through wsftp without problem, think it's in implementation. help appreciated!

public void getfile(string localfilename, string remotefilename, string transfermode) { seek { int transferfiletype = 0; existsfile(remotefilename); if (getreturnvalue() != "yes" || replycode == 550) { throw new ioexception("file " + remotefilename + " not exist"); } else { boolean transfercomplete = false; file downloadfile = new file(localfilename); outputstream output = new bufferedoutputstream(new fileoutputstream(downloadfile)); inputstream input; byte[] bytesarray = new byte[4096]; int bytesread = -1; if (!downloadfile.canwrite()) { setsucceeded(false); output.close(); throw new ioexception("cannot write file " + localfilename); } if (!isconnected()) { setsucceeded(false); output.close(); throw new ioexception("connection closed server."); } if (getsecure()) { if (transfermode.touppercase() == "binary") { ftps.setfiletype(ftps.binary_file_type); } else { ftps.setfiletype(ftps.ascii_file_type); } ftps.enterlocalpassivemode(); ftps.setremoteverificationenabled(false); seek { input = ftps.retrievefilestream(remotefilename); setreplycode(true); if (input == null || replycode == 550) { setsucceeded(false); output.close(); throw new ioexception("cannot read file " + remotefilename); } else { while ((bytesread = input.read(bytesarray)) != -1) { output.write(bytesarray, 0, bytesread); output.flush(); } input.close(); output.close(); transfercomplete = ftps.completependingcommand(); setreplycode(true); } } grab (ioexception e) { processerror(e); } } else { if (transfermode.touppercase() == "binary") { ftp.setfiletype(ftps.binary_file_type); } else { ftp.setfiletype(ftps.ascii_file_type); } ftp.enterlocalpassivemode(); ftp.setremoteverificationenabled(false); seek { input = ftp.retrievefilestream(remotefilename); setreplycode(true); if (input == null || replycode == 550) { setsucceeded(false); output.close(); throw new ioexception("cannot read file " + remotefilename); } else { while ((bytesread = input.read(bytesarray)) != -1) { output.write(bytesarray, 0, bytesread); output.flush(); } input.close(); output.close(); transfercomplete = ftp.completependingcommand(); setreplycode(true); } } grab (ioexception e) { processerror(e); } } //setreturnvalue("bytes read: " + integer.tostring(bytesread)); setsucceeded(transfercomplete); setreplycode(true); } } grab (ioexception e) { processerror(e); } }

here's open connection method:

public void open (string server_in, int port_in, int timeout_in, string username_in, string password_in, boolean implicit_in, boolean secure_in) { seek { ftpclientconfig conf = new ftpclientconfig(ftpclientconfig.syst_unix); if (secure_in) { setsecure(true); ftps = new ftpsclient("ssl", implicit_in); // create client object ftps.configure(conf); // set scheme type string[] protocolversions = {"sslv3"}; ftps.setenabledprotocols(protocolversions); // enable sslv3 protocol ftps.setautodetectutf8(true); // enable auto observe ftps.connect(server_in, port_in); // connect setreplycode(true); // server response ftps.setconnecttimeout(timeout_in); if (!ftpreply.ispositivecompletion(replycode)) { ftps.disconnect(); throw new exception("ftp server refused connection."); } ftps.login(username_in, password_in); setreplycode(true); // server response ftps.execpbsz(0); // set protection buffer 0 ftps.execprot("p"); // private protocol ftps.enterlocalpassivemode(); } else { setsecure(false); ftp = new ftpclient(); // create client object ftp.configure(conf); // set scheme type ftp.connect(server_in, port_in); setreplycode(true); // server response ftp.setautodetectutf8(true); // enable auto observe ftp.setconnecttimeout(timeout_in); if (!ftpreply.ispositivecompletion(replycode)) { ftp.disconnect(); throw new exception("ftp server refused connection."); } ftp.login(username_in, password_in); setreplycode(true); // server response ftp.enterlocalpassivemode(); } setsucceeded(true); } grab (exception e) { processerror(e); } }

ok, figured out. if @ getfile() method above you'll see first check create sure file exists existsfile(remotefilename). well, method using see if file exists to open inputstream retrievefilestream() within seek block , throw error if failed. never called completependingcommand() after retrievefilestream() in existsfile(), trying open stream file failed. phew!

java ftp-client

No comments:

Post a Comment