JAVA FTP

package ftp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;

public class FTP {
	private FtpClient ftpClient;

	/**
	 * 连接FTP服务器
	 * 
	 * @param server
	 *            FTP服务器IP地址
	 * @param user
	 *            登录名
	 * @param password
	 *            密码
	 * @throws IOException
	 */
	public void connectionServer(String server, String user, String password)
			throws IOException {
		ftpClient = new FtpClient();
		ftpClient.openServer(server);
		ftpClient.login(user, password);
		ftpClient.binary();

	}

	/**
	 * 
	 * 连接FTP服务器,并指定登录路径
	 * 
	 * @param server
	 *            FTP服务器IP地址
	 * 
	 * @param user
	 *            登录名
	 * 
	 * @param password
	 *            密码
	 * 
	 * @param path
	 *            登录路径
	 * 
	 * @throws IOException
	 * 
	 */
	public void connectServer(String server, String user, String password,

	String path) throws IOException {

		ftpClient = new FtpClient();

		ftpClient.openServer(server);

		ftpClient.login(user, password);

		ftpClient.cd(path);

		ftpClient.binary();

	}

	/**
	 * 
	 * 上传文件.上传成功返回1,上传失败返回0.
	 * 
	 * 
	 * 
	 * @param iniputStream
	 *            上传文件的输入流
	 * 
	 * @param newName
	 *            上传文件后对文件的重命名
	 * 
	 * @return int
	 * 
	 * @throws IOException
	 * 
	 */

	public int upload(InputStream in, String newName) throws IOException {

		TelnetOutputStream os = null;

		try {

			// 命名文件

			os = ftpClient.put(newName);

			byte[] bytes = new byte[1024];

			int c;

			while ((c = in.read(bytes)) != -1) {

				os.write(bytes, 0, c);

			}

		} catch (IOException e) {

			return 0;

		} finally {

			if (in != null) {

				in.close();

			}

			if (os != null) {

				os.close();

			}

		}

		return 1;

	}

	/**
	 * 
	 * 获得文件和目录列表
	 * 
	 * 
	 * 
	 * @return
	 * 
	 * @throws IOException
	 * 
	 */

	public List getFileList() throws IOException {

		List list = new ArrayList();

		TelnetInputStream in = ftpClient.nameList(".");

		BufferedReader bf = new BufferedReader(new InputStreamReader(in));

		String l = null;

		while ((l = bf.readLine()) != null) {

			if (!l.equals(".") && !l.equals(".."))

				list.add(l);

		}

		return list;

	}

	/**
	 * 
	 * 下载文件
	 * 
	 * 
	 * 
	 * @param fileName
	 * 
	 * @return
	 * 
	 * @throws IOException
	 * 
	 */

	public InputStream getFile(String fileName) throws IOException {

		TelnetInputStream in = null;

		in = ftpClient.get(fileName);

		return in;

	}

	/**
	 * 
	 * 转到指定目录
	 * 
	 * 
	 * 
	 * @param path
	 * 
	 * @throws IOException
	 * 
	 */

	public void cdPath(String path) throws IOException {

		ftpClient.cd(path);

	}

	/**
	 * 
	 * 关闭FTP服务
	 * 
	 * 
	 * 
	 * @throws IOException
	 * 
	 */

	public void closeFTPClient() throws IOException {

		if (ftpClient != null)

			ftpClient.closeServer();

	}

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		String server = "221.12.40.*";
		String user = "um";
		String passWord = "123456";
		FTP ftp = new FTP();
		ftp.connectionServer(server, user, passWord);
		List<String> s = ftp.getFileList();
		for (String i : s)
			System.out.println(i);
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值