FTP文件下载
package com.example.Test;
import com.example.util.ftp.FTPConfig;
import com.example.util.ftp.FTPFileDownload;
import java.io.IOException;
public class DownloadTest {
public static void main(String[] args) throws IOException {
FTPFileDownload ftpFileDownload = new FTPFileDownload();
FTPConfig ftpConfig = new FTPConfig();
ftpConfig.setIpAddr("39.106.227.63");
ftpConfig.setUserName("jcsc");
ftpConfig.setPwd("202433@@qq.com");
ftpConfig.setRemotePath("/PM10/20191227/0900/");
ftpConfig.setFileName("NC_H08_20191227_0100_R21_FLDK.06001_06001.zip");
ftpConfig.setLocalPath("D:/");
Boolean flag = ftpFileDownload.downloadFile(ftpConfig);
if (flag){
System.out.println("下载成功");
}else{
System.out.println("下载失败");
}
}
}
package com.example.util.ftp;
public class FTPConfig {
private String ipAddr;
private Integer port;
private String userName;
private String pwd;
private String remotePath;
private String fileName;
private String localPath;
public String getLocalPath() {
return localPath;
}
public void setLocalPath(String localPath) {
this.localPath = localPath;
}
public String getIpAddr() {
return ipAddr;
}
public void setIpAddr(String ipAddr) {
this.ipAddr = ipAddr;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getRemotePath() {
return remotePath;
}
public void setRemotePath(String remotePath) {
this.remotePath = remotePath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
package com.example.util.ftp;
import com.example.util.ftp.FTPConfig;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
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 FTPFileDownload {
public Boolean downloadFile(FTPConfig ftpConfig){
Boolean success = false;
FTPClient ftp = new FTPClient();
try {
ftp.connect(ftpConfig.getIpAddr());
ftp.setControlEncoding("GBK");
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
conf.setServerLanguageCode("zh");
ftp.login(ftpConfig.getUserName(), ftpConfig.getPwd());
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(ftpConfig.getRemotePath());
FTPFile[] files = ftp.listFiles();
for (int i = 0; i < files.length; i++) {
FTPFile ff = files[i];
System.out.println(ff.getName());
System.out.println(ftpConfig.getFileName());
System.out.println(ff.getName().equals(ftpConfig.getFileName()));
if (ff.getName().equals(ftpConfig.getFileName())) {
File localfile = new File(ftpConfig.getLocalPath()+File.separator+ ff.getName());
OutputStream is = new FileOutputStream(localfile);
ftp.retrieveFile(new String(ff.getName().getBytes("GBK"), "ISO-8859-1"), is);
is.close();
}
}
ftp.logout();
success = true;
}catch (Exception e){
e.printStackTrace();
}finally {
if (ftp.isConnected()){
try{
ftp.disconnect();
}catch (IOException IOe){
}
}
}
return success;
}
}