实现ftp单个文件上传

首先配置ftp.properties

ftpip=10.192.88.60
ftpuser=eftp
ftppassword=eftp
#localpath=/home/eftp

编写读ftp.properties文件类;

public class ReadFtpProperties{

private InputStream is;

private Properties properties;

public ReadFtpProperties() {

is = this.getClass().getResourceAsStream("ftp.properties");

 properties = new Properties();

try {

properties.load(is);

} catch (IOException e) {

System.out.println("配置文件不存在..");

e.printStackTrace();

} finally {

if (null != is) {

try { is.close();

} catch (IOException e) {

System.out.println("关闭流失败.."); e.printStackTrace();

}

}

}

}

public String getIp() {

// 获取ftp服务器的ip地址 return properties.getProperty("ftpIp"); }

public String getUser() {

// 获取ftp登录用户名 return properties.getProperty("ftpuser");

}

public String getPwd() {

// 获取ftp服务器的登录密码 return properties.getProperty("ftppassword");

}

public String getRemotePath() {

// 获取ftp服务器的存放文件的目录 return properties.getProperty("#localpath"); }

}

ftp文件下载类

public class FtpDownload {

/**
* FTP下载单个文件
*/
public static boolean testDownload(String remoteFile,String localFile) {
FTPClient ftpClient = new FTPClient();
FileOutputStream fos = null;
boolean bool;
boolean bool1;
try {
String ftpIP=IWapContext.getProperty("POSFtp.IP", "127.0.0.1");
String ftpUsername=ReadFtpProperties.getUser();
String ftpPassword=ReadFtpProperties.getPwd();
String remoteFile1=ReadFtpProperties.getRemotePath();
ftpClient.connect(ftpIP);
bool1=ftpClient.login(ftpUsername,ftpPassword);

String remoteFileName = remoteFile1+remoteFile;
fos = new FileOutputStream(localFile);

ftpClient.setBufferSize(1024);
//设置文件类型(二进制)
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
bool=ftpClient.retrieveFile(remoteFileName, fos);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
} finally {
IOUtils.closeQuietly(fos);
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
return bool&&bool1;
}
}

原文地址:https://www.cnblogs.com/2014330122wwh/p/8358239.html