FTPService工具类

package com.vcredit.ddcash.server.commons.net;

import com.vcredit.ddcash.server.commons.model.FtpParam;
import org.apache.commons.net.ftp.*;
import org.apache.commons.net.ftp.parser.MLSxEntryParser;
import org.apache.commons.net.io.*;
import org.apache.commons.net.io.SocketOutputStream;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.io.*;
import java.net.*;
import java.nio.charset.Charset;
import java.util.ArrayList;

/**
* Created by guanshuli on 2017/2/7.
*/
@Component
public class FTPService {
private static final Logger logger = Logger.getLogger(FTPService.class);
@Value("${identity.disk.save.path}")
private String identityPath;
@Value("${identity.ftp.userName}")
private String userName;
@Value("${identity.ftp.password}")
private String password;
@Value("${identity.ftp.server}")
private String hostname;
@Value("${identity.ftp.port}")
private int port;
public String __systemName;
public boolean __autodetectEncoding = false;
public int __dataConnectionMode;
public int __fileType;
public boolean __remoteVerificationEnabled;
public int __controlKeepAliveReplyTimeout = 1000;
public long __controlKeepAliveTimeout;
public int __fileFormat;
public int __bufferSize;
public CopyStreamListener __copyStreamListener;
public int receiveBufferSize = -1;
public int sendBufferSize = -1;

public boolean connect(FTPClient ftpClient) throws IOException {
if (!ftpClient.isConnected()) {
ftpClient.connect(hostname, port);
}
ftpClient.setConnectTimeout(50000);
return ftpClient.login(userName, password);
}

public void connect(FTPClient ftpClient,String hostname, int port) throws IOException {
ftpClient.connect(InetAddress.getByName(hostname), port);
}


public void connect(FTPClient ftpClient,String hostname, int port, String userName, String password) throws IOException {
ftpClient.setConnectTimeout(50000);
ftpClient.connect(hostname, port);
ftpClient.login(userName, password);
}

static String __parsePathname(String reply) {
String param = reply.substring(4);
if (param.startsWith("\"")) {
StringBuilder sb = new StringBuilder();
boolean quoteSeen = false;

for (int i = 1; i < param.length(); ++i) {
char ch = param.charAt(i);
if (ch == 34) {
if (quoteSeen) {
sb.append(ch);
quoteSeen = false;
} else {
quoteSeen = true;
}
} else {
if (quoteSeen) {
return sb.toString();
}

sb.append(ch);
}
}

if (quoteSeen) {
return sb.toString();
}
}

return param;
}


public void closeFtpClient(FTPClient ftpClient) throws IOException {
if (ftpClient.isConnected()) {
ftpClient.disconnect();
}
}

public boolean exists(FTPClient ftpClient,String fileName) throws IOException {
return ftpClient.exists(fileName);
}

public boolean uploadImg(FTPClient ftpClient,String diskSavePath, FtpParam param, InputStream inputStream) throws IOException {
return ftpClient.uploadImg(diskSavePath, param, inputStream);
}

public boolean rename(FTPClient ftpClient,String from, String to) throws IOException {
return ftpClient.rename(from, to);
}


public boolean deleteFile(FTPClient ftpClient,String file) throws IOException {
return ftpClient.deleteFile(file);
}

public FTPFile[] listFiles(FTPClient ftpClient,String rootPath) throws IOException {
return ftpClient.listFiles(rootPath);
}

public boolean isFile(FTPClient ftpClient,String file) throws IOException {
return ftpClient.isFile(file);
}

public boolean setFileType(FTPClient ftpClient,int fileType) throws IOException {
return ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
}

public boolean setFileTransferMode(FTPClient ftpClient,int fileType) throws IOException {
return ftpClient.setFileTransferMode(fileType);
}

public void setControlEncoding(FTPClient ftpClient,String charset) throws IOException {
ftpClient.setControlEncoding(charset);
}

public InputStream retrieveFileStream(FTPClient ftpClient,String fileName) throws IOException {
return ftpClient.retrieveFileStream(fileName);
}

public boolean retrieveFileStream(FTPClient ftpClient,String remote, OutputStream local) throws IOException {
return ftpClient.retrieveFile(remote, local);
}

public void removeDirectory(FTPClient ftpClient,String parentDir,
String currentDir) throws IOException {
ftpClient.removeDirectory(parentDir, currentDir);
}

public long mdtm(FTPClient ftpClient,String fileName) throws IOException {
return ftpClient.mdtm(fileName);
}
}

posted @ 2017-03-06 09:40  ding9587  阅读(299)  评论(0编辑  收藏  举报