1、FTP客户端连接与登录
/**
@ IP: FTP 服务器IP地址
@PORT: FTP 服务器端口
@USERNAME: FTP 服务器登录名称
@PASSWORD: FTP 服务器登录密码
*/
private static FTPClient getFtpClient(String IP, Integer PORT, String USERNAME, String PASSWORD){
FTPClient ftpClient = null;
try {
ftpClient = new FTPClient();
// 连接FTP服务器
ftpClient.connect(IP, PORT);
//ftp端口的打开和关闭
ftpClient.enterLocalPassiveMode();
// 登录ftp
ftpClient.login(USERNAME, PASSWORD);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE); //设置传输文件类型
int reply = ftpClient.getReplyCode();
log.debug("login ftp reply:" + reply);
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
}
}catch (Exception e) {
StringBuffer sb = new StringBuffer("登陆ftp服务器异常:" + e.toString());
sb.append("<br>");
sb.append("FTP地址:" + ftpConfig.getIp());
sb.append("<br>");
sb.append("FTP端口号:" + ftpConfig.getPort());
sb.append("<br>");
sb.append("FTP用户:" + ftpConfig.getUsername());
sb.append("<br>");
sb.append("FTP密码:" + ftpConfig.getPassword());
log.error(sb.toString(), e);
ftpClient = null;
}
return ftpClient;
}
2、下载文件
/**
@ IP: FTP 服务器IP地址
@PORT: FTP 服务器端口
@USERNAME: FTP 服务器登录名称
@PASSWORD: FTP 服务器登录密码
@PATH: 要上传至FTP的目标目录
@Local_Path: 要上传的文件所在路径
@filename: 要上传的文件名称
*/
public static String receive(String IP,Integer PORT,String USERNAME,String PASSWORD,String PATH, String Local_Path,String filename) {
boolean result = false; //接收结果
FileOutputStream out = null;
FTPClient ftpClient = null;
try {
//获得ftp客户端对象
ftpClient = getFtpClient(IP, PORT, USERNAME, PASSWORD);
if(ftpClient == null){
throw new RuntimeException("ftp init fail!");
}
//变更ftp目录
boolean b = ftpClient.changeWorkingDirectory(PATH);
if(b){
//获得本地的输出流
File localPath = new File(Local_Path);
if (!localPath.exists()) {
localPath.mkdirs();
}
out = new FileOutputStream(localPath + File.separator + filename);
result = ftpClient.retrieveFile(filename, out); //接收到本地
}else{
//找不到目录
throw new RuntimeException("找不到ftp目录:" + PATH);
}
return filename;
} catch (Exception e) {
throw new RuntimeException("接受文件异常,path=" + PATH + ",filename=" + filename, e);
}
finally{
if(out != null){
try {
out.close();
} catch (IOException e) {
log.error("关闭本地目录异常,path=" + Local_Path, e);
}
}
closeFtpClient(ftpClient);
}
}
3、发送文件
/**
@ IP: FTP 服务器IP地址
@PORT: FTP 服务器端口
@USERNAME: FTP 服务器登录名称
@PASSWORD: FTP 服务器登录密码
@PATH: 要上传至FTP的目标目录
@Local_Path: 要上传的文件所在路径
@filename: 要上传的文件名称
*/
public static boolean send(String IP,Integer PORT,String USERNAME,String PASSWORD,String PATH,String Local_Path,String filename){
boolean result = false; //发送结果
FileInputStream in = null;
FTPClient ftpClient = null;
try {
//获得ftp客户端对象
ftpClient = getFtpClient(IP, PORT, USERNAME, PASSWORD);
if(ftpClient == null){
throw new RuntimeException("ftp init fail!");
}
ftpClient.makeDirectory(PATH);
//变更ftp目录
boolean b = ftpClient.changeWorkingDirectory(PATH);
if(b){
//获得本地的流
in = new FileInputStream(Local_Path + File.separator + filename);
result = ftpClient.storeFile(new String(filename.getBytes(),"ISO-8859-1"), in);
}else{
//找不到目录
throw new RuntimeException("找不到ftp目录:" + PATH);
}
return result;
} catch (Exception e) {
throw new RuntimeException("发送文件异常,path=" + PATH + ",filename=" + filename, e);
}
finally{
closeFtpClient(ftpClient);
if(in != null){
try {
in.close();
} catch (IOException e) {
log.error("关闭本地目录异常,path=" + Local_Path, e);
}
}
}
}
4、FTP客户端关闭
private static void closeFtpClient(FTPClient ftpClient){
try {
if (ftpClient != null) {
ftpClient.disconnect();
}
} catch (Exception e) {
log.error("关闭fTP客户端异常", e);
}
}