java测试FTP服务器的上传、下载、删除文件

创建本地FTP服务器教程链接:http://jingyan.baidu.com/article/574c5219d466c36c8d9dc138.html

jar名称:commons-net-3.6.jar

jar包下载地址:http://download.csdn.net/download/lshcsdn/9937674


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

public class Ftp {
    //ftp对象
    private FTPClient ftp;
    //需要连接到的ftp端的ip
    private String ip;
    //连接端口,默认21
    private int port;
    //要连接到的ftp端的名字
    private String name;
    //要连接到的ftp端的对应得密码
    private String pwd;

    //调用此方法,输入对应得ip,端口,要连接到的ftp端的名字,要连接到的ftp端的对应得密码。连接到ftp对象,并验证登录进入fto
    public Ftp(String ip, int port, String name, String pwd) {
        ftp = new FTPClient();
        this.ip = ip;
        this.port = port;
        this.name = name;
        this.pwd = pwd;

        //验证登录
        try {
            ftp.connect(ip, port);
            System.out.println(ftp.login(name, pwd));
            ftp.setCharset(Charset.forName("UTF-8"));
            ftp.setControlEncoding("UTF-8");

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    
   //验证登录
    public void login() {
      try {
          ftp.connect(ip, port);
          System.out.println(ftp.login(name, pwd));
          ftp.setCharset(Charset.forName("UTF-8"));
          ftp.setControlEncoding("UTF-8");

      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
    }

    //上传文件
    public boolean putFile(String remoteFileName,String localFileUrl) {
        try {
            //将本地的"D:/Tshit.rar"文件上传到ftp的根目录文件夹下面,并重命名为"Tshit.rar"
//            System.out.println(ftp.storeFile("Tshit.rar", new FileInputStream(new File("D:/Tshit.rar"))));
        	ftp.setFileType(FTP.BINARY_FILE_TYPE);
        	return ftp.storeFile(remoteFileName, new FileInputStream(new File(localFileUrl)));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }

    //下载文件
    public boolean getFile(String remoteFileName,String localFileUrl) {
        try {
            //将ftp根目录下的"testavi.avi"文件下载到本地目录文件夹下面,并重命名为"2.avi"
//            ftp.retrieveFile("testavi.avi", new FileOutputStream(new File("D:/2.avi")));
        	return ftp.retrieveFile(remoteFileName, new FileOutputStream(new File(localFileUrl)));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }

        //删除ftp文件
        public boolean delete(String remoteFileName) {
            try {
            	return ftp.deleteFile(remoteFileName);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }
        }

    public static void main(String args[]) {
        Ftp m = new Ftp("127.0.0.1",21,username,password);
         m.login();
         m.putFile(remoteFileName,localFileUrl);//localFileUrl是本地文件的路径,remoteFileName是上传到ftp服务器后的文件名
m.getFile(remoteFileName,localFileUrl);
         m.delete(remoteFileName);
        
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值