java 展示服务器文件列表_java 连接服务器的方法 获得服务器下的文件列表和传输命令执行...

packagecc.julong.cash.tools;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.util.Vector;importorg.apache.commons.net.ftp.FTPClient;importorg.apache.commons.net.ftp.FTPFile;importorg.apache.commons.net.ftp.FTPReply;importorg.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")); // 功能:上传文件,参数:【本地路径】【服务器路径】

*

*@authorray

**/

public classFtpClient{privateFTPClient client;public FtpClient(booleanftps) {if(ftps) {try{

client= new FTPSClient(true);

}catch(Exception e) {

e.printStackTrace();

}

}else{

client= newFTPClient();

}

}public boolean changeDir(String remotePath) throws Exception {//路径

returnclient.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() throwsException {

client.disconnect();

}public boolean downloadFileAfterCheck(String remotePath, String localFile) throwsIOException {booleanrst;

FileOutputStream out= null;try{

File file= newFile(localFile);if (!file.exists()) {

out= newFileOutputStream(localFile);

rst=client.retrieveFile(remotePath, out);

}else{

rst= true;

}

}finally{if (out != null) {

out.close();

}

}if (out != null) {

out.close();

}returnrst;

}public boolean downloadFile(String remotePath, String localFile) throwsIOException {booleanrst;

FileOutputStream out= null;try{

out= newFileOutputStream(localFile);

rst=client.retrieveFile(remotePath, out);

}finally{if (out != null) {

out.close();

}

}returnrst;

}public Vector listFileInDir(String remoteDir) throwsException {if(changeDir(remoteDir)) {

FTPFile[] files=client.listFiles();

Vector v = new Vector();for(FTPFile file : files) {if (!file.isDirectory()) {

v.addElement(file.getName());

}

}returnv;

}else{return null;

}

}public boolean uploadFile(String localFile, String remotePath) throwsIOException {

FileInputStream in= newFileInputStream(localFile);booleanrst;try{

rst=client.storeFile(remotePath, in);

}finally{

in.close();

}returnrst;

}public Vector listSubDirInDir(String remoteDir) throwsException {if(changeDir(remoteDir)) {

FTPFile[] files=client.listFiles();

Vector v = new Vector();for(FTPFile file : files) {if(file.isDirectory()) {

v.addElement(file.getName());

}

}returnv;

}else{return null;

}

}public booleancreateDirectory(String dirName) {try{returnclient.makeDirectory(dirName);

}catch(IOException e) {

e.printStackTrace();

}return false;

}public booleanisARemoteDirectory(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);returnisDir;

}catch(IOException e) {

e.printStackTrace();

}catch(Exception e) {

e.printStackTrace();

}return false;

}publicString getWorkingDirectory() {try{returnclient.printWorkingDirectory();

}catch(IOException e) {

e.printStackTrace();

}return null;

}/*** 执行指定命令

*

*@paramcommandAndParams

* 要执行的命令以及命令的参数

*@returnString[] 执行结果*/

public VectordoCommand(String command,String params){

Vector v = new Vector();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();

}returnv;

}/*** 判断文件是否存在ftp目录上

*

*@parampathAndName

* ftp上的文件所在所在文件全路径

*@return

*/

public boolean exsitsFile(String pathAndName) throwsException{

String fileName= pathAndName.substring(pathAndName.lastIndexOf("/") + 1);

String path= pathAndName.substring(0,

pathAndName.lastIndexOf("/") + 1);return this.listFileInDir(path).contains(fileName);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值