ftp连接
package enterprise.celerity.ac.util;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import java.util.ArrayList;
import org.apache.commons.net.ftp.FTPClient;
public class Ftp implements FtpInterface{
@Override
public FTPClient ftp(String ip, String user, String password) {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(ip);
ftpClient.login(user, password);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (!ftpClient.isConnected()) {
ftpClient = null;
}
return ftpClient;
}
}
//实现方法
public OutCallPlanDataBean testupload(OutCallPlanDataBean dataBean){
String FTP_IP = "xx.xxx.xx.xxx";
int FTP_PORT = 21;//端口号
String FTP_USERNAME = "kak";//电脑用户名
String FTP_PASSWORD = "123456";//电脑开机密码
FtpClient ftpClient;
ftpClient = new FtpClient();
String remotePath="/niuxi";
String fname="催收.xls";
Ftp ftpInterface=new Ftp();
// 登录ftp,获取事件
FTPClient ftp1 = ftpInterface.ftp(FTP_IP, FTP_USERNAME, FTP_PASSWORD);//这里是ip,用户名,密码
InputStream is;
//时间正则表达式
try {
boolean aaa=ftp1.changeWorkingDirectory(new String(remotePath.getBytes(),"ISO-8859-1"));
//这里最大的坑就是这个remotePath地址了要访问具体哪个电脑要先开放那台电脑的ftp具体开放步骤百度下当你设置好具体电脑的ftp地址后这里的remotePath是你设置的ftp地址(E:/ftp
)中的文件夹名称我在ftp文件夹中建立了个niuxi文件夹
is=ftp1.retrieveFileStream(new String(fname.getBytes(),"ISO-8859-1"));
}