本文整理匯總了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;
}