// // ftp_manipulate.bsh // // Sep/03/2015 // // -------------------------------------------------------------- source ("/var/www/data_base/common/bsh_common/text_manipulate.bsh"); import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; // -------------------------------------------------------------- String ftp_get_proc (String hostname,String user,String passwd,String path_file) { FTPClient ftpclient = new FTPClient(); StringBuffer buf = new StringBuffer(); try { ftpclient.connect (hostname); int reply = ftpclient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { System.err.println("connect fail"); System.exit(1); } if (ftpclient.login(user,passwd) == false) { System.err.println("login fail"); System.exit(2); } InputStream ff = ftpclient.retrieveFileStream (path_file); BufferedReader reader = new BufferedReader(new InputStreamReader(ff, "UTF-8")); String str; while ((str = reader.readLine()) != null) { buf.append(str); buf.append("\n"); } ftpclient.disconnect (); } catch (Exception ee) { ee.printStackTrace (); } return (buf.toString ()); } // -------------------------------------------------------------- ftp_put_proc (String hostname,String user,String passwd, String path_file,String str_in) { String file_src = "/tmp/tmp_042917.txt"; file_write_proc (file_src,str_in); FTPClient fp = new FTPClient(); FileInputStream is = null; try { fp.connect (hostname); if (!FTPReply.isPositiveCompletion(fp.getReplyCode())) { System.out.println("connection failed"); System.exit(1); } if (fp.login(user, passwd) == false) { System.out.println("login failed"); System.exit(1); } is = new FileInputStream(file_src); fp.storeFile(path_file, is); is.close(); System.out.print("FTP PUT COMPLETED\t"); System.out.print(file_src + "\t"); System.out.println(path_file); } catch (Exception e) { e.printStackTrace(); } finally { fp.disconnect(); is.close(); } File fileA = new File (file_src); fileA.delete(); } // --------------------------------------------------------------