从FTP下载文件
pom
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
代码如下
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.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class FTP {
public static void main(String[] args) {
FTPClient ftp = new FTPClient();
int reply;
try {
ftp.connect("10.1.58.105", 21);//连接服务器
ftp.login("$账号$", "$密码$");//登陆
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();//连接不成功 断开
}else{
System.out.println("aaaaaaaaaaaaaaaaaaaaaaaa===================");
ftp.changeWorkingDirectory("/khzxsn/date_20190221/");//要下载的文件路径
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) { //在ftp找要下载的文件 名字为15113.txt
System.out.println("bb" + fs);
if (ff.getName().equals("15113.txt")) {
File localFile = new File("E:/FTP" + "/" + ff.getName());//下载到的地址
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);//下载
System.out.println("cccccccccccc" + ff.getName() + "15113131_343.dat");
is.close();//关闭流
}
}
ftp.logout();//断开连接
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
}
暂时只有下载