java ftp 插件_java操作FTP的一些工具方法

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;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值