Apache FtpServer 实现文件的上传和下载

本贴最后更新于 884 天前,其中的信息可能已经时过境迁

Apache FtpServer 实现文件的上传和下载

1 下载需要的 jar 包

Ftp 服务器实现文件的上传和下载,主要依赖 jar 包为:

img

2 搭建 ftp 服务器

参考 Windows 上搭建 Apache FtpServer,搭建 ftp 服务器

3 主要代码

在 eclipse 中实现 ftp 的上传和下载功能还是很简单的,在编码过程中遇到的一个 bug 就是对于 ftp 中中文文件的下载不是乱码,就是下载后文件的大小是 0KB。后来发现问题在于 eclipse 的编码,更改为“utf-8”,在上传和下载的时候,设置 ftp 服务端目录的名字,编码为 iso-8859-1 格式。

package T0728;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

class FtpUtil {
    private FTPClient ftpClient;
    private String serverIp;
    private int port;
    private String userName;
    private String passWord;
    public FtpUtil(String serverIp, int port, String userName, String passWord) {
        super();
        this.serverIp = serverIp;
        this.port = port;
        this.userName = userName;
        this.passWord = passWord;
    }
  
    /**
     * 连接ftp服务器
     * @return
     */
    public boolean open(){
        if(ftpClient != null && ftpClient.isConnected())
            return true;
        //连接服务器
        try{
            ftpClient = new FTPClient();
            ftpClient.connect(serverIp, port);    //连接服务器
            ftpClient.login(userName, passWord);    //登录
        
            ftpClient.setBufferSize(1024);
            //设置文件类型,二进制
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        
            int reply = ftpClient.getReplyCode();
            if(!FTPReply.isPositiveCompletion(reply)){ //判断ftp服务器是否连通
                this.closeFtp();
                System.out.println("FtpServer连接失败!");
                return false;
            
            }
            return true;
        }catch(Exception e){
            this.closeFtp();
            e.printStackTrace();
            return false;
        }
    }
  
    /**
     * 关闭ftp服务器,主要是对disconnect函数的调用
     */
    public void closeFtp() {
       try {
           if (ftpClient != null && ftpClient.isConnected())
            ftpClient.disconnect();
       } catch (Exception e) {
           e.printStackTrace();
       }
       System.out.println("Close Server Success :"+this.serverIp+";port:"+this.port);
    }
  
    /**
     * 从ftp服务器下载文件
     * @param ftpDirectoryAndFileName 包含ftp部分的文件路径和名字,这里是从ftp设置的根目录开始
     * @param localDirectoryAndFieName 本文的文件路径和文件名字,相当于是绝对路径
     * @return
     */
    public boolean donwLoad(String ftpDirectoryAndFileName,String localDirectoryAndFieName){
        if(!ftpClient.isConnected()){
            return false;
        }
        FileOutputStream fos =null;
        try {
            fos = new FileOutputStream(localDirectoryAndFieName);
            //下面的函数实现文件的下载功能,参数的设置解决了ftp服务中的中文问题。这里要记得更改eclipse的编码格式为utf-8
            ftpClient.retrieveFile(new String(ftpDirectoryAndFileName.getBytes(), "iso-8859-1"), fos);
            fos.close();
            return true;
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return false;
        }finally{
        
            this.closeFtp();
        }  
    
    }
  
    /**
     *从本地上传文件到ftp服务器
     * @param ftpDirectoryAndFileName
     * @param localDirectoryAndFieName
     * @return
     */
    public boolean upLoading(String ftpDirectoryAndFileName,String localDirectoryAndFieName){
        if(!ftpClient.isConnected()){
            return false;
        }
    
        FileInputStream fis = null;
    
        try {
            fis = new FileInputStream(localDirectoryAndFieName);
            //和文件的下载基本一致,但是要注意流的写法
            ftpClient.storeFile(new String(ftpDirectoryAndFileName.getBytes(), "iso-8859-1"), fis);
            fis.close();
            return true;
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return false;
        }finally{
            this.closeFtp();
        }
    }
  
  

}
package T0728;

public class FtpMain {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        FtpUtil ftpUtil = new FtpUtil("168.33.51.174", 2121, "admin", "123456");
        if(ftpUtil.open()){
            //ftpUtil.donwLoad("/中.txt", "E:/ftp2/中文.txt");
            ftpUtil.upLoading("/hh/2.mp3", "E:/ftp2/1.mp3");
        }
    }

}

如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,让更多的人能够享受到获取知识的快乐!因为本人初入职场,鉴于自身阅历有限,所以本博客内容大部分来源于网络中已有知识的汇总,欢迎各位转载,评论,大家一起学习进步!如有侵权,请及时和我联系,切实维护您的权益!

版权声明

作者:CS408

出处:https://www.cnblogs.com/lixuwu/p/5715615.html

  • FTP
    19 引用 • 20 回帖
  • Java

    Java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的。Java 技术具有卓越的通用性、高效性、平台移植性和安全性。

    3169 引用 • 8208 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...