ftpclient.java
来自「JavaExplorer是一个独立于平台的浏览器」· Java 代码 · 共 248 行
JAVA
248 行
/*
* Created on 2 oct. 03
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package javaexplorer.ftp;
import java.util.StringTokenizer;
import java.io.*;
import java.nio.*;
import java.nio.charset.*;
import java.nio.channels.*;
import java.net.*;
import javaexplorer.util.Log;
import javaexplorer.util.ftp.*;
/**
*
*
*
* @author veeb7280
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class FtpClient{
public static int MyClientNumber = 0;
public int clientNumber = 0;
public FtpClient(){
MyClientNumber ++;
clientNumber = MyClientNumber;
String encoding = System.getProperty("file.encoding");
charset = Charset.forName(encoding);
}
public static final String CHANGEDIR_COMMAND = "CWD ";
public static final String MAKEDIR_COMMAND = "MKD ";
public static final String REMOVEDIR_COMMAND = "RMD ";
public static final String DELETE_COMMAND = "DELE ";
public static final String LIST_COMMAND = "LIST";
public static final String DISCONNECT_COMMAND = "QUIT";
public static final String SYSTEM_COMMAND = "SYST";
public static final String ABORT_COMMAND = "ABOR";
private SocketChannel channel = null;
private Charset charset = null;
private boolean _isActive = false;
private boolean _isBinary = true;
private boolean _isLocked = false;
private String _system = null;
private String _user = null;
private Ftp _ftp;
private boolean debugCommands = false;
public void setFtpInformation(Ftp ftp){
_ftp = ftp;
}
private boolean reconnect(){
if( _ftp == null ) return false;
close();
channel = null;
//On se reconnecte sur le site FTP
try{
openServer( _ftp.getHost(), _ftp.getPort());
login(_ftp.getUser(), _ftp.getPassword());
return true;
}catch(IOException ioe2){
return false;
}
}
public synchronized void lock(){
_isLocked = true;
}
public synchronized void unlock(){
_isLocked = false;
}
public boolean isLocked(){
return _isLocked;
}
public void setBinary(boolean isBinary){
_isBinary = isBinary;
if( isAlive()){
ftpCommand("TYPE " + (_isBinary ? "I":"A") );
}
}
public boolean isBinary(){
return _isBinary;
}
public void setActive(boolean isActive){
_isActive = isActive;
}
public boolean isActive(){
return _isActive;
}
public void openServer( String addr, int port ) throws IOException{
channel = SocketChannel.open();
channel.connect(new InetSocketAddress(addr, port));
while( ! channel.isConnected() );
readLine();
//R閏up閞ation du system;
system();
}
public boolean isAlive(){
return channel != null && channel.isConnected();
}
public boolean login(String user, String password){
_user = null;
lock();
if( !isAlive()) return false;
ftpCommand("USER " + user);
if( checkResponse(ftpCommand("PASS " + password ), "230") ) {
setBinary(true);
_user = user;
unlock();
return true;
}
unlock();
return false;
}
public boolean changeDirectory(String newDir){
if( newDir == null ) newDir = "/";
return checkResponse(ftpCommand( CHANGEDIR_COMMAND + newDir ), new String[]{"250", "200"});
}
public void disconnect(){
ftpCommand(DISCONNECT_COMMAND);
}
public void close(){
try{
if( channel != null && channel.isOpen() ){
channel.close();
}
}catch( IOException ioe){
javaexplorer.util.Log.addError(ioe);
}
}
private boolean checkResponse(String response, String code){
return checkResponse( response, new String[]{code});
}
private boolean checkResponse(String response, String[] code_list){
if( code_list == null ) return false;
if( response == null || response.length() <= 4 ) return false;
boolean ret = false;
for( int i = 0; i < code_list.length; i++ ){
ret = ret || (response.substring(0,3).equals(code_list[i]));
}
return ret;
}
/**
* Description of the Method
*
*@param command Description of Parameter
*@exception IOException Description of Exception
*/
private synchronized String ftpCommand(String command){
String response = null;
Log.addDebug("FtpClient " + clientNumber + ", Command sent " + command);
try{
channel.write( charset.encode((command + "\r\n")) );
}catch(IOException ioe){
//Une exception arrive
javaexplorer.util.Log.addError(ioe);
//On tente une reconnection
if( reconnect() ){
try{
channel.write( charset.encode((command + "\r\n")) );
}catch(IOException ioe2){
response = ioe2.getMessage();
}
}
response = ioe.getMessage();
}
response = readLine();
return response;
}
private String readResponse(SocketChannel schannel, boolean close) throws IOException{
ByteBuffer buf = ByteBuffer.allocate(2048);
StringBuffer sb = new StringBuffer();
boolean done = false;
String partialResponse = null;
while( !done ){
if(schannel.read((ByteBuffer)buf.clear()) > 0) {
partialResponse = charset.decode(
(ByteBuffer) buf.flip()).toString();
sb.append(partialResponse);
}
else{
done = true;
}
}
if( close ){
schannel.close();
}
Log.addDebug("FtpClient " + clientNumber + ", ReadResponse " + sb.toString());
return sb.toString();
}
private void abort(){
ftpCommand(ABORT_COMMAND);
}
private String readBasicLine(){
ByteBuffer buf = ByteBuffer.allocate(1);
StringBuffer sb = new StringBuffer();
boolean done = false;
String partialResponse = null;
try{
//On lit jusqu'
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?