本文整理匯總了Java中org.apache.commons.net.ftp.FTPReply類的典型用法代碼示例。如果您正苦於以下問題:Java FTPReply類的具體用法?Java FTPReply怎麽用?Java FTPReply使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
FTPReply類屬於org.apache.commons.net.ftp包,在下文中一共展示了FTPReply類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: ftpLogin
import org.apache.commons.net.ftp.FTPReply; //導入依賴的package包/類
/**
* @return 判斷是否登入成功
* */
public boolean ftpLogin() {
boolean isLogin = false;
FTPClientConfig ftpClientConfig = new FTPClientConfig();
ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());
this.ftpClient.setControlEncoding("GBK");
this.ftpClient.configure(ftpClientConfig);
try {
if (this.intPort > 0) {
this.ftpClient.connect(this.strIp, this.intPort);
} else {
this.ftpClient.connect(this.strIp);
}
// FTP服務器連接回答
int reply = this.ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
this.ftpClient.disconnect();
System.out.println("登錄FTP服務失敗!");
return isLogin;
}
this.ftpClient.login(this.user, this.password);
// 設置傳輸協議
this.ftpClient.enterLocalPassiveMode();
this.ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
System.out.println("恭喜" + this.user + "成功登陸FTP服務器");
//logger.info("恭喜" + this.user + "成功登陸FTP服務器");
isLogin = true;
} catch (Exception e) {
e.printStackTrace();
System.out.println(this.user + "登錄FTP服務失敗!" + e.getMessage());
//logger.error(this.user + "登錄FTP服務失敗!" + e.getMessage());
}
this.ftpClient.setBufferSize(1024 * 2);
this.ftpClient.setDataTimeout(30 * 1000);
return isLogin;
}
示例2: login
import org.apache.commons.net.ftp.FTPReply; //導入依賴的package包/類
/**
* Login to the FTP Server
*/
public boolean login() {
FTPClientConfig fcc = new FTPClientConfig();
fcc.setServerTimeZoneId(TimeZone.getDefault().getID());
ftpClient.setControlEncoding(encoding);
ftpClient.configure(fcc);
try {
if (port > 0) {
ftpClient.connect(hostname, port);
} else {
ftpClient.connect(hostname);
}
// check reply code
int code = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(code)) {
ftpClient.disconnect();
logger.error("Login FTP Server is failure!");
return false;
}
if(ftpClient.login(username, password)){
// setting
this.ftpClient.enterLocalPassiveMode();
this.ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
this.ftpClient.setBufferSize(BUFFER_SIZE);
this.ftpClient.setDataTimeout(TIMEOUT);
// logger.info(username + " successfully logined to the FTP server.");
return true;
}else{
throw new Exception("Please check your username and password.");
}
} catch (Exception e) {
logger.error(username + " failed to login to the FTP server", e);
}
return false;
}
示例3: download
import org.apache.commons.net.ftp.FTPReply; //導入依賴的package包/類
public static byte[] download(String url, int port, String username, String password, String remotePath,
String fileName) throws IOException {
FTPClient ftp = new FTPClient();
ftp.setConnectTimeout(5000);
ftp.setAutodetectUTF8(true);
ftp.setCharset(CharsetUtil.UTF_8);
ftp.setControlEncoding(CharsetUtil.UTF_8.name());
try {
ftp.connect(url, port);
ftp.login(username, password);// 登錄
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
ftp.disconnect();
throw new IOException("login fail!");
}
ftp.changeWorkingDirectory(remotePath);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
try (ByteArrayOutputStream is = new ByteArrayOutputStream();) {
ftp.retrieveFile(ff.getName(), is);
byte[] result = is.toByteArray();
return result;
}
}
}
ftp.logout();
} finally {
if (ftp.isConnected()) {
ftp.disconnect();
}
}
return null;
}
示例4: connect
import org.apache.commons.net.ftp.FTPReply; //導入依賴的package包/類
public boolean connect(String hostname, int port, String username,
String password) throws IOException {
// ���ӵ�FTP������
ftpClient.connect(hostname, port);
// �������Ĭ�϶˿ڣ�����ʹ��ftp.connect(url)�ķ�ʽֱ������FTP������
if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
if (ftpClient.login(username, password)) {
return true;
}
}
disconnect();
return false;
}
示例5: open
import org.apache.commons.net.ftp.FTPReply; //導入依賴的package包/類
@Override
public FSDataInputStream open(Path file, int bufferSize) throws IOException {
FTPClient client = connect();
Path workDir = new Path(client.printWorkingDirectory());
Path absolute = makeAbsolute(workDir, file);
FileStatus fileStat = getFileStatus(client, absolute);
if (fileStat.isDirectory()) {
disconnect(client);
throw new FileNotFoundException("Path " + file + " is a directory.");
}
client.allocate(bufferSize);
Path parent = absolute.getParent();
// Change to parent directory on the
// server. Only then can we read the
// file
// on the server by opening up an InputStream. As a side effect the working
// directory on the server is changed to the parent directory of the file.
// The FTP client connection is closed when close() is called on the
// FSDataInputStream.
client.changeWorkingDirectory(parent.toUri().getPath());
InputStream is = client.retrieveFileStream(file.getName());
FSDataInputStream fis = new FSDataInputStream(new FTPInputStream(is,
client, statistics));
if (!FTPReply.isPositivePreliminary(client.getReplyCode())) {
// The ftpClient is an inconsistent state. Must close the stream
// which in turn will logout and disconnect from FTP server
fis.close();
throw new IOException("Unable to open file: " + file + ", Aborting");
}
return fis;
}
示例6: testFTPConnect
import org.apache.commons.net.ftp.FTPReply; //導入依賴的package包/類
/**
* Simple test that connects to the inbuilt ftp server and logs on
*
* @throws Exception
*/
public void testFTPConnect() throws Exception
{
logger.debug("Start testFTPConnect");
FTPClient ftp = connectClient();
try
{
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
fail("FTP server refused connection.");
}
boolean login = ftp.login(USER_ADMIN, PASSWORD_ADMIN);
assertTrue("admin login not successful", login);
}
finally
{
ftp.disconnect();
}
}
示例7: connectClient
import org.apache.commons.net.ftp.FTPReply; //導入依賴的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;
}
示例8: find
import org.apache.commons.net.ftp.FTPReply; //導入依賴的package包/類
@Override
public PathAttributes find(final Path file) throws BackgroundException {
if(file.isRoot()) {
return PathAttributes.EMPTY;
}
try {
if(session.getClient().hasFeature(FTPCmd.MLST.getCommand())) {
if(!FTPReply.isPositiveCompletion(session.getClient().sendCommand(FTPCmd.MLST, file.getAbsolute()))) {
throw new FTPException(session.getClient().getReplyCode(), session.getClient().getReplyString());
}
final FTPDataResponseReader reader = new FTPMlsdListResponseReader();
final AttributedList<Path> attributes
= reader.read(file.getParent(), Arrays.asList(session.getClient().getReplyStrings()), new DisabledListProgressListener());
if(attributes.contains(file)) {
return attributes.get(attributes.indexOf(file)).attributes();
}
}
throw new InteroperabilityException("No support for MLST in reply to FEAT");
}
catch(IOException e) {
throw new FTPExceptionMappingService().map("Failure to read attributes of {0}", e, file);
}
}
示例9: handle
import org.apache.commons.net.ftp.FTPReply; //導入依賴的package包/類
private BackgroundException handle(final FTPException e, final StringBuilder buffer) {
final int status = e.getCode();
switch(status) {
case FTPReply.INSUFFICIENT_STORAGE:
case FTPReply.STORAGE_ALLOCATION_EXCEEDED:
return new QuotaException(buffer.toString(), e);
case FTPReply.NOT_LOGGED_IN:
return new LoginFailureException(buffer.toString(), e);
case FTPReply.FAILED_SECURITY_CHECK:
case FTPReply.DENIED_FOR_POLICY_REASONS:
case FTPReply.NEED_ACCOUNT:
case FTPReply.NEED_ACCOUNT_FOR_STORING_FILES:
case FTPReply.FILE_NAME_NOT_ALLOWED:
case FTPReply.ACTION_ABORTED:
return new AccessDeniedException(buffer.toString(), e);
case FTPReply.UNAVAILABLE_RESOURCE:
case FTPReply.FILE_UNAVAILABLE:
// Requested action not taken. File unavailable (e.g., file not found, no access)
return new NotfoundException(buffer.toString(), e);
case FTPReply.SERVICE_NOT_AVAILABLE:
return new ConnectionRefusedException(buffer.toString(), e);
}
return new InteroperabilityException(buffer.toString(), e);
}
示例10: sendCommands
import org.apache.commons.net.ftp.FTPReply; //導入依賴的package包/類
public void sendCommands(final List<String> commands, final FlowFile flowFile) throws IOException {
if (commands.isEmpty()) {
return;
}
final FTPClient client = getClient(flowFile);
for (String cmd : commands) {
if (!cmd.isEmpty()) {
int result;
result = client.sendCommand(cmd);
logger.debug(this + " sent command to the FTP server: " + cmd + " for " + flowFile);
if (FTPReply.isNegativePermanent(result) || FTPReply.isNegativeTransient(result)) {
throw new IOException(this + " negative reply back from FTP server cmd: " + cmd + " reply:" + result + ": " + client.getReplyString() + " for " + flowFile);
}
}
}
}
示例11: initFtpClient
import org.apache.commons.net.ftp.FTPReply; //導入依賴的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;
}
示例12: connect
import org.apache.commons.net.ftp.FTPReply; //導入依賴的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;
}
示例13: connect
import org.apache.commons.net.ftp.FTPReply; //導入依賴的package包/類
@Override
public void connect( String server, int port )
throws IOException
{
if( ftp.isConnected() ) {
throw new IllegalStateException( "Already connected" );
}
debug( () -> "Connecting to " + server + " on " + (port > 0 ? port : ftp.getDefaultPort()) );
if( port > 0 ) {
ftp.connect( server, port );
}
else {
ftp.connect( server );
}
debug( () -> "Connected to " + server + " on " + (port > 0 ? port : ftp.getDefaultPort()) );
// After connection attempt, you should check the reply code to verifysuccess.
if( !FTPReply.isPositiveCompletion( ftp.getReplyCode() ) ) {
debug( () -> "Connection refused to " + server + " on " + (port > 0 ? port : ftp.getDefaultPort()) );
throw new ConnectException( "Failed to connect to " + server );
}
}
示例14: connect
import org.apache.commons.net.ftp.FTPReply; //導入依賴的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;
}
示例15: exists
import org.apache.commons.net.ftp.FTPReply; //導入依賴的package包/類
public boolean exists(String filename) throws FileSystemException {
// we have to be connected
if (ftp == null) {
throw new FileSystemException("Not yet connected to FTP server");
}
try {
// check if the file already exists
FTPFile[] files = ftp.listFiles(filename);
// did this command succeed?
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
throw new FileSystemException("FTP server failed to get file listing (reply=" + ftp.getReplyString() + ")");
}
if (files != null && files.length > 0) {
// this file already exists
return true;
} else {
return false;
}
} catch (IOException e) {
throw new FileSystemException("Underlying IO exception with FTP server while checking if file exists", e);
}
}