本文整理匯總了Java中org.apache.commons.net.ftp.FTPClient.enterLocalPassiveMode方法的典型用法代碼示例。如果您正苦於以下問題:Java FTPClient.enterLocalPassiveMode方法的具體用法?Java FTPClient.enterLocalPassiveMode怎麽用?Java FTPClient.enterLocalPassiveMode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.net.ftp.FTPClient
的用法示例。
在下文中一共展示了FTPClient.enterLocalPassiveMode方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: initFtpClient
import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的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;
}
示例2: connectFTPServer
import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
* 連接FTP服務器
*
* @author wangwei
*/
public FTPClient connectFTPServer() throws Exception {
ftpClient = new FTPClient();
try {
ftpClient.configure(getFTPClientConfig());
ftpClient.connect(ftpConstant.getHost(), ftpConstant.getPort());
ftpClient.login(ftpConstant.getUsername(), ftpConstant.getPassword());
// 設置以二進製方式傳輸
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// 設置被動模式
ftpClient.enterLocalPassiveMode();
ftpClient.setControlEncoding("GBK");
// 響應信息
int replyCode = ftpClient.getReplyCode();
if ((!FTPReply.isPositiveCompletion(replyCode))) {
// 關閉Ftp連接
closeFTPClient();
// 釋放空間
ftpClient = null;
throw new Exception("登錄FTP服務器失敗,請檢查!");
} else {
return ftpClient;
}
} catch (Exception e) {
ftpClient.disconnect();
ftpClient = null;
throw e;
}
}
示例3: downloadFile
import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
* @desc 根據文件名下載文件
*
* @author liuliang
*
* @param filename
* 文件名
* @return boolean下載結果
*/
public byte[] downloadFile(String filename) {
FTPClient ftpClient = ftpConnManager.getFTPClient();
if (null == ftpClient || !ftpClient.isConnected()) {
log.error("根據文件名下載文件失敗,獲取ftpClient失敗,filename:{}", filename);
return null;
}
try {
ftpClient.enterLocalPassiveMode();
InputStream ins = ftpClient.retrieveFileStream(new String(filename.getBytes("UTF-8"), "iso-8859-1"));
if (null == ins) {
return null;
}
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[100];
int rc = 0;
int value =100;
while ((rc = ins.read(buff, 0, value)) > 0) {
swapStream.write(buff, 0, rc);
}
byte[] fileByte = swapStream.toByteArray();
// ftpClient.getReply();
return fileByte;
} catch (IOException e) {
log.error("根據文件名下載文件異常,filename:{}", filename, e);
} finally {
disconnect(ftpClient);
}
return null;
}
示例4: getFTPClient
import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
protected FTPClient getFTPClient() {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.setConnectTimeout(connectTimeout);
ftpClient.connect(hostname);
ftpClient.login(userName, passWord);
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
FtpUtils.disconnect(ftpClient);
log.warn("FTP登陸失敗,賬號或者密碼有誤");
} else {
ftpClient.setSoTimeout(soTimeout);
/* 設置緩衝區大小 */
ftpClient.setBufferSize(bufferSize);
/* 設置服務器編碼 */
ftpClient.setControlEncoding(encoding);
/* 設置以二進製方式傳輸 */
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
/* 設置服務器目錄 */
// ftpClient.changeWorkingDirectory(directory);
ftpClient.enterLocalPassiveMode();
log.debug("成功連接並登錄FTP服務器。。。");
}
} catch (Exception e) {
FtpUtils.disconnect(ftpClient);
log.error("error", e);
}
return ftpClient;
}
示例5: getFTPClient
import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
protected FTPClient getFTPClient(final String ftpServer, final String username, final String password)
throws Exception {
final FTPClient ftpClient = new FTPClient();
ftpClient.connect(ftpServer);
ftpClient.login(username, password);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
return ftpClient;
}
示例6: uploadFile
import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
* Description: 向FTP服務器上傳文件
* @param url FTP服務器hostname
* @param port FTP服務器端口
* @param username FTP登錄賬號
* @param password FTP登錄密碼
* @param path FTP服務器保存目錄
* @param filename 上傳到FTP服務器上的文件名
* @param input 輸入流
* @return 成功返回true,否則返回false
*/
public static boolean uploadFile(String url,int port,String username, String password, String path, String filename, InputStream input) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);//連接FTP服務器
//如果采用默認端口,可以使用ftp.connect(url)的方式直接連接FTP服務器
ftp.login(username, password);//登錄
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
//設置FTP以2進製傳輸
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
//TODO 讀取文件配置判斷是否使用主動
ftp.enterLocalPassiveMode();//被動
//ftp.enterLocalActiveMode();//主動
//創建目錄
mkDir(path,ftp);
//改變目錄
ftp.changeWorkingDirectory(path);
ftp.storeFile(filename, input);
input.close();
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
示例7: connect
import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
@ProtoMethod(description = "Connect to a ftp server", example = "")
@ProtoMethodParam(params = {"host", "port", "username", "password", "function(connected)"})
public void connect(final String host, final int port, final String username, final String password, final FtpConnectedCb callback) {
mFTPClient = new FTPClient();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
mFTPClient.connect(host, port);
MLog.d(TAG, "1");
if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
boolean logged = mFTPClient.login(username, password);
mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
mFTPClient.enterLocalPassiveMode();
isConnected = logged;
callback.event(logged);
}
MLog.d(TAG, "" + isConnected);
} catch (Exception e) {
MLog.d(TAG, "connection failed error:" + e);
}
}
});
t.start();
}
示例8: getNewFTPClient
import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
public FTPClient getNewFTPClient(Uri path, int mode) throws SocketException, IOException, AuthenticationException{
// Use default port if not set
int port = path.getPort();
if (port<0) {
port = 21; // default port
}
String username="anonymous"; // default user
String password = ""; // default password
NetworkCredentialsDatabase database = NetworkCredentialsDatabase.getInstance();
Credential cred = database.getCredential(path.toString());
if(cred!=null){
password= cred.getPassword();
username = cred.getUsername();
}
FTPClient ftp= new FTPClient();
//try to connect
ftp.connect(path.getHost(), port);
//login to server
if(!ftp.login(username, password))
{
ftp.logout();
throw new AuthenticationException();
}
if(mode>=0){
ftp.setFileType(mode);
}
int reply = ftp.getReplyCode();
//FTPReply stores a set of constants for FTP reply codes.
if (!FTPReply.isPositiveCompletion(reply))
{
try {
ftp.disconnect();
} catch (IOException e) {
throw e;
}
return null;
}
//enter passive mode
ftp.enterLocalPassiveMode();
return ftp;
}
示例9: upload
import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
* 上傳文件到FTP服務器,支持斷點續傳
*
* @param localFile 本地文件
* @param remoteFilePath 遠程文件路徑,使用/home/directory1/subdirectory/file.ext
* 按照Linux上的路徑指定方式,支持多級目錄嵌套,支持遞歸創建不存在的目錄結構
* @return 上傳結果
* @throws IOException
*/
public UploadStatus upload(FTPClient ftpClient, File localFile, String remoteFilePath) throws IOException {
// 設置PassiveMode傳輸
ftpClient.enterLocalPassiveMode();
// 設置以二進製流的方式傳輸
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setControlEncoding(DEAFULT_REMOTE_CHARSET);
UploadStatus result;
// 對遠程目錄的處理
String remoteFileName = remoteFilePath;
if (remoteFilePath.contains("/")) {
remoteFileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/") + 1);
// 創建服務器遠程目錄結構,創建失敗直接返回
if (createDirecroty(remoteFilePath, ftpClient) == UploadStatus.Create_Directory_Fail) {
return UploadStatus.Create_Directory_Fail;
}
}
// 檢查遠程是否存在文件
FTPFile[] files = ftpClient.listFiles(new String(remoteFileName
.getBytes(DEAFULT_REMOTE_CHARSET), DEAFULT_LOCAL_CHARSET));
if (files.length == 1) {
long remoteSize = files[0].getSize();
// File f = new File(localFilePath)
long localSize = localFile.length();
if (remoteSize == localSize) { // 文件存在
return UploadStatus.File_Exits;
} else if (remoteSize > localSize) {
return UploadStatus.Remote_Bigger_Local;
}
// 嘗試移動文件內讀取指針,實現斷點續傳
result = uploadFile(remoteFileName, localFile, ftpClient, remoteSize);
// 如果斷點續傳沒有成功,則刪除服務器上文件,重新上傳
if (result == UploadStatus.Upload_From_Break_Failed) {
if (!ftpClient.deleteFile(remoteFileName)) {
return UploadStatus.Delete_Remote_Faild;
}
result = uploadFile(remoteFileName, localFile, ftpClient, 0);
}
} else {
result = uploadFile(remoteFileName, localFile, ftpClient, 0);
}
return result;
}
示例10: getMessage
import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
@Override
public String getMessage() throws IOException {
String message = null;
try
{
FTPClient ftpClient = new FTPClient();
ftpClient.connect(server, port);
ftpClient.login(user, password);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(2);
String remoteFile1 = "/"+file;
InputStream inputStream = ftpClient.retrieveFileStream(remoteFile1);
message = StreamUtility.readStream(inputStream, "iso-8859-1");
Boolean success = ftpClient.completePendingCommand();
if (success) {
System.out.println("File has been downloaded successfully.");
}
inputStream.close();
}
catch (IOException e)
{
/* SendMail mail = new SendMail();
try
{
mail.postMail(this.properties.getProperty("mail"), "TMC-Fehler",
e.getStackTrace().toString(), "[email protected]", this.properties
.getProperty("smtpHost"), this.properties
.getProperty("smtpUser"), this.properties
.getProperty("smtpPort"));
}
catch (MessagingException e1)
{
e1.printStackTrace();
}
this.logger.debug("Error with FTP connection " +
e.getLocalizedMessage(), e);
throw new DownloadException(
"Error while downloading file from FTP " +
e.getLocalizedMessage(), e);
*/
}
return message;
}