importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.net.SocketException;importjava.util.Properties;importorg.apache.commons.lang.StringUtils;importorg.apache.commons.net.ftp.FTP;importorg.apache.commons.net.ftp.FTPClient;importorg.apache.commons.net.ftp.FTPFile;importorg.apache.commons.net.ftp.FTPReply;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;public class FTPHandlerImpl implementsFTPHandler {private static final Logger LOGGER= LoggerFactory.getLogger(FTPHandlerImpl.class);private static String url="";private static int port=21;private static String username=null;private static String password=null;private static String remotePath=null;private FTPClient ftpClient = null;static{
Properties configP;try{
configP= PropertiesUtils.getWebLoProperties("configure");
url= configP.getProperty("downFileService.ftp.url");
username= configP.getProperty("downFileService.ftp.username");
password= configP.getProperty("downFileService.ftp.password");
remotePath= configP.getProperty("downFileService.ftp.remotePath");//localPath = configP.getProperty("downFileService.ftp.localPath");
} catch(Exception e) {
e.printStackTrace();
}
}publicFTPHandlerImpl() {
FTPClient ftpClient= newFTPClient();this.ftpClient=ftpClient;
}
@Overridepublic boolean connect() throwsSocketException, IOException {boolean success=true;
ftpClient.connect(url, port);boolean login =ftpClient.login(username, password);int replyCode =ftpClient.getReplyCode();if (!FTPReply.isPositiveCompletion(replyCode)) {
success=false;
}return success&&login;
}
@Overridepublic booleanuploadFile(String _remotePath,String storeFilename, InputStream is) {boolean success=false;try{//切换到path指定的目录
if(StringUtils.isBlank(_remotePath)){
_remotePath=remotePath;
}
createOrChangeDir(_remotePath);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
success=ftpClient.storeFile(storeFilename, is);
is.close();
}catch(IOException e) {
e.printStackTrace();
}
closeClient(ftpClient);returnsuccess;
}
@Overridepublic boolean downFile(String _remotePath,String filename, OutputStream os) throwsIOException {//解决图片上传失真//解决图片下载失真//切换到path指定的目录
if(StringUtils.isBlank(_remotePath)){
_remotePath=remotePath;
}
createOrChangeDir(_remotePath);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
FTPFile[] ftpFiles=ftpClient.listFiles();boolean success=false;for(FTPFile file : ftpFiles) {if(file.getName().equals(filename)) {
filename=file.getName();
success=ftpClient.retrieveFile(filename, os);break;
}
}
closeClient(ftpClient);returnsuccess;
}
@Overridepublic boolean deleteFiles(String _remotePath,String fileName) throwsIOException {//切换到path指定的目录
if(StringUtils.isBlank(_remotePath)){
_remotePath=remotePath;
}
ftpClient.changeWorkingDirectory(_remotePath);
FTPFile[] ftpFiles=ftpClient.listFiles();boolean isDelete = false;for(FTPFile file : ftpFiles) {if(fileName.equalsIgnoreCase(file.getName())) {
ftpClient.deleteFile(file.getName());
isDelete= true;
}
}
closeClient(ftpClient);returnisDelete;
}
@Overridepublic voidcloseClient(FTPClient ftpClient) {try{if (ftpClient != null) {
ftpClient.logout();
}
}catch(Exception e) {
e.printStackTrace();
}finally{if (ftpClient != null &&ftpClient.isConnected()) {try{
ftpClient.disconnect();
}catch(IOException ioe) {
ioe.printStackTrace();
}
}
}
}/*** 创建目录(有则切换目录,没有则创建目录)
*@paramdir
*@return
*/
private booleancreateOrChangeDir(String dir){if(StringUtils.isBlank(dir))return true;
String d;try{//目录编码,解决中文路径问题
d = new String(dir.toString().getBytes("UTF-8"),"iso-8859-1");//尝试切入目录
if(ftpClient.changeWorkingDirectory(d))return true;
dir= StringUtils.removeStart(dir, "/");
dir= StringUtils.removeEnd(dir, "/");
String[] arr= dir.split("/");
StringBuffer sbfDir=newStringBuffer();//循环生成子目录
for(String s : arr){
sbfDir.append("/");
sbfDir.append(s);//目录编码,解决中文路径问题
d = new String(sbfDir.toString().getBytes("UTF-8"),"iso-8859-1");//尝试切入目录
if(ftpClient.changeWorkingDirectory(d))continue;if(!ftpClient.makeDirectory(d)){
LOGGER.info("[失败]ftp创建目录:"+sbfDir.toString());return false;
}
LOGGER.info("[成功]创建ftp目录:"+sbfDir.toString());
}//将目录切换至指定路径
returnftpClient.changeWorkingDirectory(d);
}catch(Exception e) {
e.printStackTrace();return false;
}
}
}