java 连接服务器的方法 获得服务器下的文件列表和传输命令执行
方法1:apache jar包 commons-net-3.3.jar api http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html
不足doCommand方法只能够执行ftp命令(不包括ls 命令)
package cc.julong.cash.tools; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Vector; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; import org.apache.commons.net.ftp.FTPSClient; /** * FTP客户端,支持FTPS协议 * * 例: * FtpClient client = new FtpClient(true); // 功能:创建FTP客户端,参数:【是否开启ftps】 * client.connect("192.168.0.1", "root", "root", 990); // 功能:连接ftp服务器,参数:【连接地址】【用户名】【密码】【端口号】 * System.out.println(client.listFileInDir("/")); // 功能:列出主目录下文件列表,参数:【服务器路径】 * System.out.println(client.downloadFile("/123.txt","D:/123.txt")); //功能:下载文件,参数:【服务器路径】【本地路径】 * System.out.println(client.uploadFile("D:/py_work/test.py", "/test.py")); // 功能:上传文件,参数:【本地路径】【服务器路径】 * * @author ray * */ public class FtpClient{ private FTPClient client; public FtpClient(boolean ftps) { if (ftps) { try { client = new FTPSClient(true); } catch (Exception e) { e.printStackTrace(); } } else { client = new FTPClient(); } } public boolean changeDir(String remotePath) throws Exception {//路径 return client.changeWorkingDirectory(remotePath); } public boolean connect(String host, String login, String password, int port) throws Exception {//ftp 链接 client.connect(host, port); int reply = client.getReplyCode(); if (FTPReply.isPositiveCompletion(reply)) { if (client.login(login, password)) { client.enterLocalPassiveMode(); return true; } } disconnect(); return false; } public void disconnect() throws Exception { client.disconnect(); } public boolean downloadFileAfterCheck(String remotePath, String localFile) throws IOException { boolean rst; FileOutputStream out = null; try { File file = new File(localFile); if (!file.exists()) { out = new FileOutputStream(localFile); rst = client.retrieveFile(remotePath, out); } else { rst = true; } } finally { if (out != null) { out.close(); } } if (out != null) { out.close(); } return rst; } public boolean downloadFile(String remotePath, String localFile) throws IOException { boolean rst; FileOutputStream out = null; try { out = new FileOutputStream(localFile); rst = client.retrieveFile(remotePath, out); } finally { if (out != null) { out.close(); } } return rst; } public Vector<String> listFileInDir(String remoteDir) throws Exception { if (changeDir(remoteDir)) { FTPFile[] files = client.listFiles(); Vector<String> v = new Vector<String>(); for (FTPFile file : files) { if (!file.isDirectory()) { v.addElement(file.getName()); } } return v; } else { return null; } } public boolean uploadFile(String localFile, String remotePath) throws IOException { FileInputStream in = new FileInputStream(localFile); boolean rst; try { rst = client.storeFile(remotePath, in); } finally { in.close(); } return rst; } public Vector<String> listSubDirInDir(String remoteDir) throws Exception { if (changeDir(remoteDir)) { FTPFile[] files = client.listFiles(); Vector<String> v = new Vector<String>(); for (FTPFile file : files) { if (file.isDirectory()) { v.addElement(file.getName()); } } return v; } else { return null; } } public boolean createDirectory(String dirName) { try { return client.makeDirectory(dirName); } catch (IOException e) { e.printStackTrace(); } return false; } public boolean isARemoteDirectory(String path) { String cache = "/"; try { cache = client.printWorkingDirectory(); } catch (NullPointerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { boolean isDir = changeDir(path); changeDir(cache); return isDir; } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return false; } public String getWorkingDirectory() { try { return client.printWorkingDirectory(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 执行指定命令 * * @param commandAndParams * 要执行的命令以及命令的参数 * @return String[] 执行结果 */ public Vector<String> doCommand(String command,String params){ Vector<String> v = new Vector<String>(); try { boolean b = client.doCommand("help",""); System.out.println(b); String[] strs = client.doCommandAsStrings(command, params); System.out.println(command+ params); System.out.println(strs + "-----123"); System.out.println(client.listHelp()); strs = client.getReplyStrings(); for (String str : strs) { v.addElement(str); } } catch (IOException e) { e.printStackTrace(); } return v; } /** * 判断文件是否存在ftp目录上 * * @param pathAndName * ftp上的文件所在所在文件全路径 * @return */ public boolean exsitsFile(String pathAndName) throws Exception{ String fileName = pathAndName.substring(pathAndName.lastIndexOf("/") + 1); String path = pathAndName.substring(0, pathAndName.lastIndexOf("/") + 1); return this.listFileInDir(path).contains(fileName); } }
package cc.julong.cash.tools; import java.util.Vector; public class test { public static void main(String args[]){ String host = "10.64.8.11"; String login = "ogsdev"; String password = "ogsdev123"; int port = 21; FtpClient ftpClient = new FtpClient(false); System.out.println(ftpClient); try { ftpClient.connect(host, login, password, port); System.out.println(ftpClient); Vector<String> list = new Vector<String>(); list = ftpClient.listFileInDir("/ogsdev/tmp/wangzf"); System.out.println(list); for(int i = 0;i<list.size();i++){ System.out.println(list.get(i)); } Vector<String> list1 = new Vector<String>(); System.out.println(ftpClient.getWorkingDirectory()); list1 = ftpClient.doCommand("help", null); for(int i = 0;i<list1.size();i++){ System.out.println(list1.get(i)); } } catch (Exception e) { e.printStackTrace(); } finally{ if(ftpClient !=null){ try { ftpClient.disconnect(); } catch (Exception e) { e.printStackTrace(); } } } } }
方法2:javajar包 jsch jsch-0.1.51.jar 专为执行命令
具体查看博客 http://www.cnblogs.com/longyg/archive/2012/06/25/2556576.html
posted on 2015-01-15 10:36 weiguoyuan 阅读(978) 评论(0) 收藏 举报
浙公网安备 33010602011771号