欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一个ftp客户端

程序员文章站 2022-04-24 16:45:41
...

代码如下:

 

其中用到了apache commons-net包

 

public class FtpUtil {

	private static FTPClient ftpClient;
	private static FtpUtil instatnce;
	public static final int BINARY_FILE_TYPE = FTP.BINARY_FILE_TYPE;
	public static final int ASCII_FILE_TYPE = FTP.ASCII_FILE_TYPE;

	private FtpUtil() {

	}

	public static synchronized FtpUtil getInstatnce() {
		if (instatnce == null) {
			instatnce = new FtpUtil();
			ftpClient = new FTPClient();
		}
		return instatnce;
	}

	public synchronized boolean connectServer(FtpConfig ftpConfig) throws SocketException,IOException {
		boolean flag = false;
		String server = ftpConfig.getHost();
		int port = ftpConfig.getPort();
		String user = ftpConfig.getUser();
		String password = ftpConfig.getPwd();

		boolean logined = connectServer(server, port, user, password);
		if (logined) {
			flag = true;
		}
		return flag;
	}

	private boolean connectServer(String server, int port, String user,String password) throws SocketException, IOException {
		boolean flag = false;
		ftpClient.setControlEncoding("UTF-8");
		ftpClient.connect(server, port);
		//被动模式传输
		ftpClient.enterLocalPassiveMode();
		ftpClient.setFileType(BINARY_FILE_TYPE);
		boolean logined = ftpClient.login(user, password);
		if (logined) {
			flag = true;
		}
		return flag;
	}
	
	public void setFileType(int fileType) throws IOException {
		ftpClient.setFileType(fileType);
	}
	
	/**
	 * 关闭服务端
	 * @throws IOException
	 */
	public void closeServer() throws IOException {
		if (ftpClient.isConnected()) {
			ftpClient.disconnect();
		}
	}
	
	/**
	 * 退出ftp
	 * @throws IOException
	 */
	public synchronized void logOut() throws IOException {
		ftpClient.logout();
	}
	
	/**
	 * 改变服务端目录
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public boolean changeDirectory(String path) throws IOException {
		return ftpClient.changeWorkingDirectory(path);
	}
	
	/**
	 * 建立目录
	 * @param pathName
	 * @return
	 * @throws IOException
	 */
	public boolean createDirectory(String pathName) throws IOException {
		return ftpClient.makeDirectory(pathName);
	}

	/**
	 * 删除服务端目录
	 * @param path
	 * @return boolean
	 * @throws IOException
	 */
	public boolean removeDirectory(String path) throws IOException {
		return ftpClient.removeDirectory(path);
	}
	
	/**
	 * 上传文件到服务端
	 * @param fileName 
	 * @param newName
	 * @return
	 * @throws IOException
	 */
	public boolean uploadFile(String fileName, String newName)	throws IOException {
		boolean flag = false;
		InputStream iStream = null;
		try {
			iStream = new FileInputStream(fileName);
			ftpClient.storeFile(newName, iStream);
			if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
				flag = true;
			}

		}catch(IOException e) {
			flag = false;
			return flag;
		}finally {
			if (iStream != null) {
				iStream.close();
			}
		}
		return flag;
	}	
	
	public static void main(String[] args) {
		FtpConfig ftpConfig = new FtpConfig() ;
		ftpConfig.setHost("192.168.0.135") ;  
		ftpConfig.setUser("admin") ;
		ftpConfig.setPwd("1111") ;
		ftpConfig.setPort(21) ;	
		FtpUtil util = FtpUtil.getInstatnce();	
		try {
			boolean flag = util.connectServer(ftpConfig) ;
			if(flag){
				System.out.println("--------------成功连接ftp");				
				util.uploadFile("d:/aaaa.xml", "/aaa-----3.xml");
				util.uploadFile("d:/aaab.xml", "/aaa4.xml");
				System.out.println("--------------上传文件完成");
				util.logOut();				
			}else{
				System.out.println("--->ftp连接失败,请检查客户端或服务端设置");
			}
		} catch (SocketException e) {			
			e.printStackTrace();
		} catch (IOException e) {			
			e.printStackTrace();
		}
		finally{
			try {
				util.closeServer();
			} catch (IOException e) {				
				e.printStackTrace();
			}
		}
	}
	
	


}

 

 

public class FtpConfig {
	
	private String host;
	private int port;
	private String user;
	private String pwd;
	
	public FtpConfig() {
		
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
	
	
}

 

相关标签: XML Apache .net