本文整理汇总了Java中org.apache.commons.net.ftp.FTP类的典型用法代码示例。如果您正苦于以下问题:Java FTP类的具体用法?Java FTP怎么用?Java FTP使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
FTP类属于org.apache.commons.net.ftp包,在下文中一共展示了FTP类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getFTPClient
import org.apache.commons.net.ftp.FTP; //导入依赖的package包/类
public FTPClient getFTPClient(Uri uri) throws SocketException, IOException, AuthenticationException{
NetworkCredentialsDatabase database = NetworkCredentialsDatabase.getInstance();
Credential cred = database.getCredential(uri.toString());
if(cred==null){
cred = new Credential("anonymous","", buildKeyFromUri(uri).toString(), true);
}
FTPClient ftpclient = ftpClients.get(cred);
if (ftpclient!=null && ftpclient.isConnected()){
return ftpclient;
}
// Not previous session found, open a new one
Log.d(TAG, "create new ftp session for "+uri);
FTPClient ftp = getNewFTPClient(uri,FTP.BINARY_FILE_TYPE);
if(ftp==null)
return null;
Uri key = buildKeyFromUri(uri);
Log.d(TAG, "new ftp session created with key "+key);
ftpClients.put(cred, ftp);
return ftp;
}
示例2: getFTPSClient
import org.apache.commons.net.ftp.FTP; //导入依赖的package包/类
public FTPClient getFTPSClient(Uri uri) throws SocketException, IOException, AuthenticationException{
NetworkCredentialsDatabase database = NetworkCredentialsDatabase.getInstance();
Credential cred = database.getCredential(uri.toString());
if(cred==null){
cred = new Credential("anonymous","", buildKeyFromUri(uri).toString(), true);
}
FTPClient ftpclient = ftpsClients.get(cred);
if (ftpclient!=null && ftpclient.isConnected()){
return ftpclient;
}
// Not previous session found, open a new one
Log.d(TAG, "create new ftp session for "+uri);
FTPClient ftp = getNewFTPSClient(uri, FTP.BINARY_FILE_TYPE);
if(ftp==null)
return null;
Uri key = buildKeyFromUri(uri);
Log.d(TAG, "new ftp session created with key "+key);
ftpsClients.put(cred, ftp);
return ftp;
}
示例3: connectClient
import org.apache.commons.net.ftp.FTP; //导入依赖的package包/类
@Override
public boolean connectClient() throws IOException {
boolean isLoggedIn = true;
client.setAutodetectUTF8(true);
client.setControlEncoding("UTF-8");
client.connect(host, port);
client.setFileType(FTP.BINARY_FILE_TYPE);
client.enterLocalPassiveMode();
client.login(username, password);
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();
LogUtils.LOGD(TAG, "Negative reply form FTP server, aborting, id was {}:"+ reply);
//throw new IOException("failed to connect to FTP server");
isLoggedIn = false;
}
return isLoggedIn;
}
示例4: initFtpClient
import org.apache.commons.net.ftp.FTP; //导入依赖的package包/类
private FTPClient initFtpClient(String remoteDir) throws IOException {
FTPClient ftp = new FTPClient();
// 设置连接超时时间
ftp.setConnectTimeout(CONNECT_TIMEOUT);
// 设置传输文件名编码方式
ftp.setControlEncoding(CONTROL_ENCODING);
ftp.connect(host, ftpPort);
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
logger.debug("无法连接FTP");
return null;
}
boolean loginRet = ftp.login(ftpUsername, ftpPassword);
if (!loginRet) {
logger.debug("FTP登录失败");
return null;
}
// 进入被动模式
ftp.enterLocalPassiveMode();
boolean changeDirResult = MKDAndCWD(ftp, remoteDir);
if (!changeDirResult) {
logger.debug("创建/切换文件夹失败");
return null;
}
// 传输二进制文件
ftp.setFileType(FTP.BINARY_FILE_TYPE);
return ftp;
}
示例5: connect
import org.apache.commons.net.ftp.FTP; //导入依赖的package包/类
/**
* Connect to the FTP server using configuration parameters *
*
* @return An FTPClient instance
* @throws IOException
*/
private FTPClient connect() throws IOException {
FTPClient client = null;
Configuration conf = getConf();
String host = conf.get(FS_FTP_HOST);
int port = conf.getInt(FS_FTP_HOST_PORT, FTP.DEFAULT_PORT);
String user = conf.get(FS_FTP_USER_PREFIX + host);
String password = conf.get(FS_FTP_PASSWORD_PREFIX + host);
client = new FTPClient();
client.connect(host, port);
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
throw NetUtils.wrapException(host, port,
NetUtils.UNKNOWN_HOST, 0,
new ConnectException("Server response " + reply));
} else if (client.login(user, password)) {
client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
client.setFileType(FTP.BINARY_FILE_TYPE);
client.setBufferSize(DEFAULT_BUFFER_SIZE);
} else {
throw new IOException("Login failed on server - " + host + ", port - "
+ port + " as user '" + user + "'");
}
return client;
}
示例6: uploadAll
import org.apache.commons.net.ftp.FTP; //导入依赖的package包/类
private boolean uploadAll() {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(FTP_HOST, FTP_PORT);
if (!ftpClient.login(FTP_USER, FTP_PASSWORD)) {
return false;
}
String dirName = getDirName();
ftpClient.makeDirectory(dirName);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
boolean result = true;
for (File file: files) {
result &= upload(ftpClient, file, dirName + "/" + file.getName());
}
return result;
} catch (IOException e) {
Log.e(BugReportSenderFtp.class.getSimpleName(), "FTP network error: " + e.getMessage());
} finally {
closeSilently(ftpClient);
}
return false;
}
示例7: connect
import org.apache.commons.net.ftp.FTP; //导入依赖的package包/类
/**
* 连接FTP服务器
* @param host FTP服务器地址
* @param port FTP服务器端口号
* @param username 用户名
* @param password 密码
* @return
*/
public static boolean connect(String host, int port, String username, String password) {
try{
ftpClient = new FTPClient();
ftpClient.connect(host, port);
// 登录
ftpClient.login(username, password);
ftpClient.setBufferSize(FTP_SERVER_BUFFER_SIZE_VALUE);
ftpClient.setControlEncoding(FTP_CLIENT_ENCODING);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setDataTimeout(FTP_SERVER_DATA_TIME_OUT);
ftpClient.setSoTimeout(FTP_SERVER_SO_TIME_OUT);
// 检验是否连接成功
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
LOGGER.error("连接FTP服务器失败,响应码:"+reply);
disConnectionServer();
return false;
}
}catch(IOException e){
LOGGER.error("连接FTP服务器失败",e);
return false;
}
return true;
}
示例8: connect
import org.apache.commons.net.ftp.FTP; //导入依赖的package包/类
/**
* Opens the Server connection using the specified IP address and the default port.
* @throws Exception Thrown if an exception is encountered during the connect or login operations.
*/
public void connect() throws Exception {
logger.info("Connecting to '" + address + "'...");
server.connect(address, port);
logger.info("Logging in with credentials '" + username + "', '<hidden>'");
server.login(username, password);
logger.info("Configuring connection...");
server.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
server.setBufferSize(1024 * 1024);
server.setAutodetectUTF8(true);
server.setControlKeepAliveTimeout(300);
logger.info("Connection established.");
}
示例9: connectToFtpServer
import org.apache.commons.net.ftp.FTP; //导入依赖的package包/类
/**
* Connects the client to the FTP Server if is not already connected.
*/
public void connectToFtpServer() {
try {
ftpClient.connect(ftpServerName, port);
ftpClient.login(username, password);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.changeWorkingDirectory(ftpHomeDirectory);
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
throw new IOException("FTP server refused connection.");
}
LOGGER.info(String.format("Connected to an FTP Server with IP (%s:%s).", ftpServerName, port));
} catch (IOException e) {
LOGGER.error(String.format("Failed to connect to an FTP Server with IP (%s:%s).", ftpServerName, port), e);
}
}
示例10: getFTPInputStream
import org.apache.commons.net.ftp.FTP; //导入依赖的package包/类
private static InputStream getFTPInputStream(String ftpServerName, String checklistRemoteFilePath) throws IOException {
FTPClient ftp = new FTPClient();
ftp.connect(ftpServerName);
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
throw new IOException("FTP server " + ftpServerName + " refused connection");
}
if (!ftp.login(USERNAME, PASSWORD)) {
ftp.logout();
throw new IOException("FTP server failed to login using username anonymous");
}
ftp.setFileType(FTP.ASCII_FILE_TYPE);
InputStream inputStream = ftp.retrieveFileStream(checklistRemoteFilePath);
return inputStream;
}
示例11: getVersionAndTimestamp
import org.apache.commons.net.ftp.FTP; //导入依赖的package包/类
private Pair<String, Date> getVersionAndTimestamp(String path) throws SocketException, IOException, InterruptedException, ParseException {
try (MyFTPClient ftp = new MyFTPClient(ftpServer, ftpPort, ftpUser, ftpPw)) {
for (FTPFile f : ftp.listFiles(path)) {
if (f.getName().equals(ProgramPackageFile.BUILD_FN)) {
File tsF = new File("./updateTimestamp.txt");
ftp.downloadFile(path, f, tsF, FTP.ASCII_FILE_TYPE);
Properties p = new Properties();
FileInputStream fis = new FileInputStream(tsF);
p.load(fis);
fis.close();
tsF.delete();
logger.debug("remote timestamp string: "+p.getProperty("Date"));
return Pair.of(p.getProperty("Version"), CoreUtils.DATE_FORMAT.parse(p.getProperty("Date")));
}
}
}
return null;
}
示例12: downloadUpdate
import org.apache.commons.net.ftp.FTP; //导入依赖的package包/类
@Override
public void downloadUpdate(final File downloadFile, FTPProgramPackageFile f, final IProgressMonitor monitor, boolean downloadAll) throws Exception {
monitor.beginTask("Downloading "+f.f.getName(), 100);
// TODO: use FTPUtils.downloadFile method...
FTPTransferListener transferL = new FTPTransferListener(f.f) {
@Override public void downloaded(int percent) {
monitor.worked(percent);
monitor.subTask(percent+"%");
if (monitor.isCanceled()) {
boolean success = abort();
}
}
};
try {
FTPUtils.downloadFile(ftpServer, ftpPort, ftpUser, ftpPw,
f.path, FTP.BINARY_FILE_TYPE, f.f, downloadFile, transferL);
} catch (IOException e) {
throw new InvocationTargetException(e, e.getMessage());
}
monitor.done();
}
示例13: connect
import org.apache.commons.net.ftp.FTP; //导入依赖的package包/类
public boolean connect(String hostname,int port,String username,String password) throws IOException{
ftpClient.setConnectTimeout(30*1000);
ftpClient.connect(hostname, port);
//ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
if(ftpClient.login(username, password)){
logger.info("login to [{}] with username[{}] password=[{}] success",hostname,username,password);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
return true;
}
else{
logger.error("unable to login to ftp server[{}] with username[{}] password=[{}], abort!",hostname,username,password );
disconnect();
}
}
return false;
}
示例14: login
import org.apache.commons.net.ftp.FTP; //导入依赖的package包/类
private void login() throws IOException {
if(!isConnected()){
connect();
}
ftpClient.login(user, password);
if(passiveMode){
ftpClient.enterLocalPassiveMode();
}
//PDF transmitted with ASCII file type break the file
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setBufferSize(0);
ftpClient.setControlKeepAliveTimeout(timeOutInSeconds);
}
示例15: connect
import org.apache.commons.net.ftp.FTP; //导入依赖的package包/类
/**
* Connect to the FTP server using configuration parameters *
*
* @return An FTPClient instance
* @throws IOException
*/
private FTPClient connect() throws IOException {
FTPClient client = null;
Configuration conf = getConf();
String host = conf.get("fs.ftp.host");
int port = conf.getInt("fs.ftp.host.port", FTP.DEFAULT_PORT);
String user = conf.get("fs.ftp.user." + host);
String password = conf.get("fs.ftp.password." + host);
client = new FTPClient();
client.connect(host, port);
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
throw new IOException("Server - " + host
+ " refused connection on port - " + port);
} else if (client.login(user, password)) {
client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
client.setFileType(FTP.BINARY_FILE_TYPE);
client.setBufferSize(DEFAULT_BUFFER_SIZE);
} else {
throw new IOException("Login failed on server - " + host + ", port - "
+ port);
}
return client;
}