FtpBean.java


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

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

/**
 * FTP下载方法BEAN 支持普通ftp下载
 * 
 * @author 宗锋
 * @vesion 1.0
 */
public class FtpBean
{
	// 定义私有方法
	// FtpClient aftp; // ftp客户端

	String hostname = ""; // 主机名

	int port = 21; // 主机端口

	String uid;

	String pwd;

	public FtpBean(String hostname, int port, String uid, String password)
	{
		this.hostname = hostname;
		this.pwd = password;
		this.uid = uid;
		this.port = port;
	}

	/**
	 * 连接FTP服务器
	 * @return  FtpClent
	 */
	public FtpClient connect()
	{
		FtpClient aftp = null;
		String mess;
		System.out.println("正在连接" + hostname + ",请等待.....");
		try
		{
			aftp = new FtpClient(hostname, port);
			aftp.login(uid, pwd);
			aftp.binary();

			mess = "连接主机:" + hostname + "成功!";
			System.out.println(mess);
			
		} catch (FtpLoginException e)
		{
			mess = "登陆主机:" + hostname + "失败!请检查用户名或密码是否正确:" + e;
			System.out.println(mess);
						
		} catch (IOException e)
		{
			mess = "连接主机:" + hostname + "失败!请检查端口是否正确:" + e;
			System.out.println(mess);
						
		} catch (SecurityException e)
		{
			mess = "无权限与主机:" + hostname + "连接!请检查是否有访问权限:" + e;
			System.out.println(mess);
					
		}
		
		return aftp;
	}

	/**
	 * 停止FTP客户端
	 * 
	 */
	public void stop(FtpClient aftp)
	{
		String message = "";
		try
		{
			if (aftp != null)
			{
				aftp.closeServer();
				message = "与主机" + hostname + "连接已断开!";
				System.out.println(message);
				
			}
		} catch (IOException e)
		{
			message = "与主机" + hostname + "断开连接失败!" + e;
			System.out.println(message);
			
		}
	}

	/**
	 * FTP下载类 支持从FTP服务器下载文件
	 * 
	 * @param aftp
	 *            ftp client
	 * @param filepath
	 *            下载文件保存路径
	 * @param filename
	 *            下载文件名
	 * @param ftppath
	 *            ftp服务器文件路径
	 * @return boolean 成功返回true,失败返回false
	 */
	public boolean downloadFile(FtpClient aftp, String filepath,
			String filename, String ftppath)
	{
		boolean result = true;
		String message = "";
		if (aftp != null)
		{
			System.out.println("正在下载文件" + filename + ",请等待....");

			String strdir = ftppath;

			try
			{
				// 判断保存路径是否存在,不存在则mkdirs()
				File dir = new File(filepath);
				if (!dir.exists())
					dir.mkdirs();

				File fi = new File(filepath + filename);

				// 写文件类
				PrintWriter write = new PrintWriter(fi);
				
				//BufferedWriter write = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fi)));

				// 切换到读取文件路径
				aftp.cd(strdir);

				// 获取读取文件流
				TelnetInputStream fget = aftp.get(filename);
				System.out.println("getFile inputstream succeed !");

				// 使用buffer优化读取速度
				BufferedReader br = new BufferedReader(new InputStreamReader(
						fget));

				String line = "";

				while ((line = br.readLine()) != null)
				{
					// 写文件,行尾增加显示换行
					write.write(line + "\r\n");
				}

				// 关闭流操作
				fget.close();
				br.close();
				write.close();
				// fc.closeServer();

				message = "下载" + filename + "文件到" + filepath + "目录成功!";
				System.out.println(message);
				
			} catch (IOException e)
			{
				message = "下载" + filename + "文件到" + filepath + "目录失败! 原因为:"
						+ e.getMessage();
				System.out.println(message);
				
				result = false;
			}
		} else
		{
			message = "ftp客户端传入参数为NULL,下载失败";
			System.out.println(message);
			
			result = false;
		}
		return result;
	}
	
	public boolean uploadFile(FtpClient aftp, String filepath,
			String filename, String ftppath)
	{
		boolean result = true;
		String message = "";
		if (aftp != null)
		{			
			System.out.println("正在上传文件" + filename + ",请等待....");

			try
			{
				filepath = filepath + filename;
//				File ftpFile = new File(ftppath);
//				if(!ftpFile.exists())
//					System.out.println(ftpFile.mkdirs());
				//aftp.sendServer("mkdir " + ftppath + " \n");
				//aftp.cd(ftppath);
				TelnetOutputStream os = aftp.put(ftppath+filename);
				File file = new File(filepath);
				FileInputStream is = new FileInputStream(file);
				byte[] bytes = new byte[1024];
				int c;
				while ((c = is.read(bytes)) != -1) {
				os.write(bytes, 0, c);
				}
				System.out.println("upload success");
				is.close();
				os.close();

			} catch (IOException e)
			{
				message = "上传" + filename + "文件到" + ftppath + "目录失败! 原因为:"
						+ e.getMessage();
				System.out.println(message);
				
				result = false;
			}
		} else
		{
			message = "ftp客户端传入参数为NULL,上传失败";
			System.out.println(message);
			
			result = false;
		}
		return result;
	}
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值