org.apache.commons.net工具下 对ftp的简单操作

/**
	 * 连接ftp服务器
	 * @param ip	IP地址
	 * @param port	端口号
	 * @param username	用户名
	 * @param password	密码
	 * @param ftpPath	ftp子目录
	 * @throws IOException
	 */
	public int connectServer(String ip, int port, String username,
			String password, String ftpPath) throws IOException {
		system.out.println(this, "=============all_attributes==========="+"ip_"+ip+"port_"+port+"username_"+username+"password_"+password+"ftpPath_"+ftpPath);
		int reply;
		int connectFlag = 1;
		try {

			ftpClient.connect(ip, port);//连接FTP服务器
			 //如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
 			ftpClient.login(username, password);
 			reply = ftpClient.getReplyCode();
 			if(!FTPReply.isPositiveCompletion(reply)){
 				ftpClient.disconnect();
 				connectFlag = 0;
 				return connectFlag;
 			}
 			 ftpClient.changeWorkingDirectory(ftpPath);//转移到FTP服务器目录  

			system.out.println(this, "=============connet_ftp_is_ok_falg_"+connectFlag+"===========");
		} catch (Exception e) {
			system.out.println(this, "=============connet_ftp_is_error"+connectFlag+"===========");
		}
			return connectFlag;
	}

----------------------------------------------------------------------------------------------------------------------------------------

	/**
	 * upload 上传文件
	 *
	 * @throws	 java.lang.Exception
	 * @return 	-1 文件不存在 -2 文件内容为空 >0 成功上传,返回文件的大小
	 * @param newname	上传后的新文件名
	 * @param filePath  上传的文件的路径
	 */
	public long upload(String localPath, String newFileName) throws Exception {
		long result = 0;

		try {
			File file = new File(localPath);
			if (!file.exists()) {
				result = -1;//文件不存在
			}
			if (file.length() == 0) {
				result = -2;//文件内容为空
			}
//			result = file.length();
			is = new FileInputStream(file);
			ftpClient.storeFile(newFileName, is);
			ftpClient.logout();
			system.out.println(this, "========="+newFileName+"文件上传成功==========");
		}catch (Exception e) {
			e.printStackTrace();
			system.out.println(this, "========="+newFileName+"文件上传失败==========");
		}finally{

		}
		return result;
	}
---------------------------------------------------------------------------------
/**
	 * 通过文件路径上传
	 * @param filePath
	 * @return
	 * @throws Exception
	 */
	public long upload(String filePath) throws Exception {
		String newname = "";
		if (filePath.indexOf("/") > -1) {
			newname = filePath.substring(filePath.lastIndexOf("/") + 1);
		} else {
			newname = filePath;
		}
		return upload(filePath, newname);
	}
----------------------------------------------
/**
	 *
	 * closeServer
	 *
	 * 断开与ftp服务器的链接
	 *
	 * @throws java.io.IOException
	 */
	public void closeServer() throws IOException
	{
		try {
			if (is != null) {
				is.close();
			}
			if (ftpClient != null)
			{
				ftpClient.disconnect();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
-------------------------------------------------

/**
     * 读取文本文件.
     *
     */
    public static String readTxtFile(File filePathAndName){
        String read;
        FileReader fileread;
        try {
            fileread = new FileReader(filePathAndName);
            bufread = new BufferedReader(fileread);
            try {
                while ((read = bufread.readLine()) != null) {
                    readStr = readStr + read+ "\r\n";
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("文件内容是:"+ "\r\n" + readStr);
        return readStr;
    }
------------------------------------------------------

    /**
     * 写文件.
     *
     */
    public static void writeTxtFile(String newStr,File filePathAndName) throws IOException{
        //先读取原有文件内容,然后进行写入操作
        String filein = newStr + "\r\n" + readStr + "\r\n";
        RandomAccessFile mm = null;
        try {
            mm = new RandomAccessFile(filePathAndName, "rw");
            mm.writeBytes(filein);
        } catch (IOException e1) {
            // TODO 自动生成 catch 块
            e1.printStackTrace();
        } finally {
            if (mm != null) {
                try {
                    mm.close();
                } catch (IOException e2) {
                    // TODO 自动生成 catch 块
                    e2.printStackTrace();
                }
            }
        }
    }

----------------------------------------------------

/**
	 *
	 * downloadFile
	 *
	 * 下载ftp服务器的资源
	 * @param   remoteFileName  待下载文件名称
	 * @param  localDires 本里存放路径
	 * @param  ftpPath ftp服务器存放要下载文件的子目录
	 *
	 * @throws java.io.IOException
	 */
    public  void downloadFile(String remoteFileName,String localDires , String ftpPath ) throws IOException{
        String strfilepath = localDires +"/"+ remoteFileName;    //写入地址 文件保存路径
        BufferedOutputStream bf = null;
        boolean success=false;

        try {
        	ftpClient.changeWorkingDirectory(ftpPath);
            bf= new BufferedOutputStream(new FileOutputStream(strfilepath));

            system.out.println(this, "========="+remoteFileName+"开始下载==========");

            success = ftpClient.retrieveFile(remoteFileName,bf);  //下载操作
//            ftpClient.logout();
            if(success==true)
            {
            	system.out.println(this, "========="+remoteFileName+"成功下载到=========="+localDires);
            }

        } catch (IOException e1) {
            // TODO 自动生成 catch 块
            e1.printStackTrace();
        } finally {
            if (null != bf) {
                try {
                	bf.flush();
                    bf.close();
                } catch (IOException e2) {
                    // TODO 自动生成 catch 块
                    e2.printStackTrace();
                }
            }
        }
         if(success=false)
         {
        	 system.out.println(this, "========="+remoteFileName+"下载失败==========");
         }
    }

    -----------------------------------------
时间: 2024-08-07 15:45:24

org.apache.commons.net工具下 对ftp的简单操作的相关文章

Apache commons lang工具类学习笔记(2)--StringUtils

StringUtils工具类具有对String具有简单而强大的处理能力,从检查空串到分割字符串,到生成格式化的字符串,使用都很方便简洁,能减少很多代码量; 详细的使用方法可以参考下面的例子或者官方的API(http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/StringUtils.html#isAlpha(java.lang.CharSequence)) packa

cmd 下登陆ftp及相关操作

cmd 下登陆ftp及相关操作 2011-08-09 20:34:28|  分类: 小技巧|字号 订阅 一.举例 假设FTP地址为“ 61.129.83.39”(大家试验的时候不要以这个FTP去试,应该可能密码要改掉.)       1:“开始”-“运行”-输入“FTP”进去cmd界面 2.open 61.129.83.39 如果你的FTP服务器不是用的21默认端口,假如端口是9900,那么此步的命令应在后面空格加9900,即为 open 61.129.83.39 9900 3:它会提示输入用户

Apache Commons Random工具类介绍和使用

之前在工作中,有大量用到随机数的场景.当然我们可以自己扩展JDK自带的random函数,但是有现成的轮子是最好的,今天我们来学习下Apache Commons 中的Random类.

Ubuntu(linux)下的FTP服务器简单搭建

前言 ? ? ? ?今天才发觉FTP服务器超级好用.省却了数据线和恼人的局域网配置smb.当然也有一些缺点.比如只能客户端在服务器上读写.服务器不能在客户端上读写. 一 vsftpd安装 ? ? ? ubuntu下终端使用命令安装:sudo apt-get install vsftpd 二 vsftpd配置 配置文件默认路径是/etc/vsftpd.conf,下面我把自己用到的配置文件的内容说明下: 打开监听 listen=YES 是否允许匿名访问 anonymous_enable=NO 是否允

Apache POI读取和创建Excel ----01(简单操作)

public class ExcelCreatAndRead { /**     * 使用Apache POI创建Excel文档     * */    public static void createXL(){        /**Excel文件要存放的位置,假定在D盘下*/        String outputFile="D:\\test.xlsx";        try {        //创建新的Excel工作薄        XSSFWorkbook workboo

Ubuntu下安装MySQL及简单操作

Ubuntu上安装MySQL非常简单只需要几条命令就可以完成. 1. sudo apt-get install mysql-server 2. apt-get isntall mysql-client 3.  sudo apt-get install libmysqlclient-dev 安装过程中会提示设置密码什么的,注意设置了不要忘了,安装完成之后可以使用如下命令来检查是否安装成功: sudo netstat -tap | grep mysql 通过上述命令检查之后,如果看到有mysql 的

Linux下安装mysql及简单操作

1. 进入Linux系统挂载光盘,(或下载MysqL-server MysqL),配置yum源 2. yum install -y mysql-server mysql 3. Service mysqld start 4. 登录数据库mysql [-u username] [-h host] [-p[password]] [dbname] 5. 修改密码root用户:usr/bin/mysqladmin -u root password 'new-password' 6. 数据库目录  /var

高性能jdbc封装工具 Apache Commons DbUtils 1.6

转载自原文地址:http://gao-xianglong.iteye.com/blog/2166444 前言 关于Apache的DbUtils中间件或许了解的人并不多,大部分开发人员在生成环境中更多的是依靠Hibernate.Ibatis.Spring JDBC.JPA等大厂提供的持久层技术解决方案,或者是企业内部自己研发的持久层技术.但无论如何,使用这些技术的初衷和本质都是为了能够减少企业开发成本,提高生产效率,降低耦合. 放眼企业级项目,Hibernate等ORM产品是首选,而互联网领域,大

高性能jdbc封装工具 Apache Commons DbUtils 1.6(转载)

转载自原文地址:http://gao-xianglong.iteye.com/blog/2166444 前言 关于Apache的DbUtils中间件或许了解的人并不多,大部分开发人员在生成环境中更多的是依靠Hibernate.Ibatis.Spring JDBC.JPA等大厂提供的持久层技术解决方案,或者是企业内部自己研发的持久层技术.但无论如何,使用这些技术的初衷和本质都是为了能够减少企业开发成本,提高生产效率,降低耦合. 放眼企业级项目,Hibernate等ORM产品是首选,而互联网领域,大