1、在服务器上面新建一个ftp服务器,并且支持FTP文件协议
2.测试代码:
package com.ding.test;
import com.ding.util.FTPClientUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import java.io.File;
import java.io.IOException;
/**
* Created by ding on 1/16/16.
*/
public class FtpTest {
public static void main(String []agrs){
FTPClient ftp = new FTPClient();
try {
ftp.connect("192.168.1.110",21);
ftp.login("root","123456");
int reply= ftp.getReplyCode();
System.out.println(" reply =="+reply);
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
// FTPFile[] files = ftp.listNames();
String[] names = ftp.listNames();
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]);
}
}catch (IOException e){
e.printStackTrace();
}
// System.out.println(FTPClientUtils.openConnectFTPService());
// FTPClinetUtils.changeWorkingDirectory("/home/ubuntu/uftp");
// FTPClinetUtils.ListNamesAll();
// FTPClinetUtils.makeDirectory("adsfdas");
// FTPClinetUtils.deleteFile("/home/ubuntu/uftp/4a36acaf2edda3ccf1bb668601e93901213f923d.jpg");
// FTPClinetUtils.deleteDirectory("/home/ubuntu/uftp/testtest/dadaaad/dadaaad");
//下载文件
// boolean flag= FTPClinetUtils.downloadFile("2015082709590657592.bin","/home/ding/test.bin");
//上传文件
// String path = "/home/ubuntu/uftp/testtest/dadaaad/dadaaad/dadaaad/dadaaad";
// File file = new File("/home/ding/test.bin");
// boolean flag= FTPClientUtils.uploadFile(file,"test.bin",path);
// System.out.println(flag);
// String[] str = path.split("/");
// System.out.println(str.length);
// for (int i=0;i<str.length;i++) {
// System.out.println(" 第" + i + "元素,String=/"+ str[i]);
// }
// FTPClinetUtils.makeDirectory(path);
// FTPClientUtils.closeConnection();
}
}
连接成功后,会返回FTP存放目录的文件及文件夹