乐趣区

关于ftp:FTP服务器

首先对于配置 FTP 服务器

1.1、vsftpd.conf

以装置在 linux 下的 FTP 为例,首先查找到 vsftpd 的配置文件(默认装置的状况下配置文件位于 /etc/vsftpd/vsftpd.conf),而后进行编写。具体的表格如下:

choose_local_user=YES choose_local_user=NO
choose_list_enbale=YES 1. 所有用户都被限度在其主目录下 2. 应用 chroot_list_file 指定的用户列表,这些用户作为“例外”,不受限制 1. 所有用户都不被限度其主目录下 2. 应用 chroot_list_file 指定的用户列表,这些用户作为“例外”,受到限制
choose_list_enable=NO 1、所有用户都被限度在其主目录下 2. 没有例外用户 1. 所有用户都没有限度,也没有例外用户
package com.manage.util;

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

import java.io.*;

import static org.apache.commons.codec.binary.StringUtils.newString;

/**
 * @Author: lgz
 * @Date: 2020/6/8 13:40
 */

public class FTPConnectUtil {
    private String FTP_ADDRESS = "192.168.174.140";

    private int PORT = 21;

    private String FTP_USERNAME = "admin";

    private String FTP_PASSWORD = "123";

    private String FTP_PATH = "/home/admin/files";

    public FTPClient ftpClient = null;

    /**
     * 初始化 ftp 服务器
     */
    public void initFtpClient() {ftpClient = new FTPClient();
        ftpClient.setControlEncoding("utf-8");
        try {System.out.println("connecting...ftp 服务器:" + this.FTP_ADDRESS + ":" + this.PORT);
            ftpClient.connect(FTP_ADDRESS, PORT); // 连贯 ftp 服务器
            ftpClient.login(FTP_USERNAME, FTP_PASSWORD); // 登录 ftp 服务器
            int replyCode = ftpClient.getReplyCode(); // 是否胜利登录服务器
            if (!FTPReply.isPositiveCompletion(replyCode)) {System.out.println("connect failed...ftp 服务器:" + this.FTP_ADDRESS + ":" + this.PORT);
            }
            System.out.println("connect successful");
        } catch (IOException e) {e.printStackTrace();
        }
    }

    /**
     * 上传文件
     *
     * @param pathname       ftp 服务保留地址
     * @param fileName       上传到 ftp 的文件名
     * @param originfilename 待上传文件的名称(相对地址)*
     * @return
     */
    public boolean uploadFile(String pathname, String fileName, String originfilename) {
        boolean flag = false;
        InputStream inputStream = null;
        try {System.out.println("开始上传文件");
            inputStream = new FileInputStream(new File(originfilename));
            initFtpClient();
            ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
      //     CreateDirecroty(pathname);
      //      ftpClient.makeDirectory(pathname);
            ftpClient.changeWorkingDirectory(pathname);
            ftpClient.storeFile(fileName, inputStream);
            inputStream.close();
            ftpClient.logout();
            flag = true;
            System.out.println("上传文件胜利");
        } catch (Exception e) {System.out.println("上传文件失败");
            e.printStackTrace();} finally {if (ftpClient.isConnected()) {
                try {ftpClient.disconnect();
                } catch (IOException e) {e.printStackTrace();
                }
            }
            if (null != inputStream) {
                try {inputStream.close();
                } catch (IOException e) {e.printStackTrace();
                }
            }
        }
        return true;
    }


    /**
     * 上传文件
     *
     * @param pathname    ftp 服务保留地址
     * @param fileName    上传到 ftp 的文件名
     * @param inputStream 输出文件流
     * @return
     */
    public boolean uploadFile(String pathname, String fileName, InputStream inputStream) {
        boolean flag = false;
        try {System.out.println("开始上传文件");
            initFtpClient();
            ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            CreateDirecroty(pathname);
            ftpClient.makeDirectory(pathname);
            ftpClient.changeWorkingDirectory(pathname);
            ftpClient.storeFile(new String(fileName.getBytes("GBK"),"ISO-8859-1"), inputStream);
            Thread.sleep(200);
            System.out.println("正在上传图片");
           inputStream.close();
            ftpClient.logout();
            flag = true;
            System.out.println("上传文件胜利");
        } catch (Exception e) {System.out.println("上传文件失败");
            e.printStackTrace();} finally {if (ftpClient.isConnected()) {
                try {ftpClient.disconnect();
                } catch (IOException e) {e.printStackTrace();
                }
            }
            if (null != inputStream) {
                try {inputStream.close();
                } catch (IOException e) {e.printStackTrace();
                }
            }
        }
        return true;
    }

    // 扭转目录门路
    public boolean changeWorkingDirectory(String directory) {
        boolean flag = true;
        try {flag = ftpClient.changeWorkingDirectory(directory);
            if (flag) {System.out.println("进入文件夹" + directory + "胜利!");

            } else {System.out.println("进入文件夹" + directory + "失败!开始创立文件夹");
            }
        } catch (IOException ioe) {ioe.printStackTrace();
        }
        return flag;
    }

    // 创立多层目录文件,如果有 ftp 服务器已存在该文件,则不创立,如果无,则创立
    public boolean CreateDirecroty(String remote) throws IOException {
        boolean success = true;
        String directory = remote + "/";
        // 如果近程目录不存在,则递归创立近程服务器目录
        if (!directory.equalsIgnoreCase("/") && !changeWorkingDirectory(new String(directory))) {
            int start = 0;
            int end = 0;
            if (directory.startsWith("/")) {start = 1;} else {start = 0;}
            end = directory.indexOf("/", start);
            String path = "";
            String paths = "";
            while (true) {String subDirectory = new String(remote.substring(start, end).getBytes("GBK"), "iso-8859-1");
                path = path + "/" + subDirectory;
                if (!existFile(path)) {if (makeDirectory(subDirectory)) {changeWorkingDirectory(subDirectory);
                    } else {System.out.println("创立目录 [" + subDirectory + "] 失败");
                        changeWorkingDirectory(subDirectory);
                    }
                } else {changeWorkingDirectory(subDirectory);
                }

                paths = paths + "/" + subDirectory;
                start = end + 1;
                end = directory.indexOf("/", start);
                // 查看所有目录是否创立结束
                if (end <= start) {break;}
            }
        }
        return success;
    }

    // 判断 ftp 服务器文件是否存在
    public boolean existFile(String path) throws IOException {
        boolean flag = false;
        FTPFile[] ftpFileArr = ftpClient.listFiles(path);
        if (ftpFileArr.length > 0) {flag = true;}
        return flag;
    }

    // 创立目录
    public boolean makeDirectory(String dir) {
        boolean flag = true;
        try {flag = ftpClient.makeDirectory(dir);
            if (flag) {System.out.println("创立文件夹" + dir + "胜利!");

            } else {System.out.println("创立文件夹" + dir + "失败!");
            }
        } catch (Exception e) {e.printStackTrace();
        }
        return flag;
    }

    /**
     * 下载文件 *
     *
     * @param pathname  FTP 服务器文件目录 *
     * @param filename  文件名称 *
     * @param localpath 下载后的文件门路 *
     * @return
     */
    public boolean downloadFile(String pathname, String filename, String localpath) {
        boolean flag = false;
        OutputStream os = null;
        try {System.out.println("开始下载文件");
            initFtpClient();
            // 切换 FTP 目录
            ftpClient.changeWorkingDirectory(pathname);
            // 改为被动模式
            ftpClient.enterLocalPassiveMode();
            System.out.println(filename);
            FTPFile[] ftpFiles = ftpClient.listFiles();
            for (FTPFile file : ftpFiles) {int type = file.getType();
                if (filename.equalsIgnoreCase(file.getName())) {System.out.println(localpath);

                    File localFile = new File(localpath + "/" + filename);  //
                    os = new FileOutputStream(localFile); //  os ==null
                    ftpClient.retrieveFile(newString(filename.getBytes("UTF-8"), "ISO-8859-1"), os);

                    os.close();}
            }
            ftpClient.logout();
            flag = true;
            System.out.println("下载文件胜利");
        } catch (Exception e) {System.out.println("下载文件失败");
            e.printStackTrace();} finally {if (ftpClient.isConnected()) {
                try {ftpClient.disconnect();
                } catch (IOException e) {e.printStackTrace();
                }
            }
            if (null != os) {
                try {os.close();
                } catch (IOException e) {e.printStackTrace();
                }
            }
        }
        return flag;
    }
    /**
     * 删除文件 *
     *
     * @param pathname FTP 服务器保留目录 *
     * @param filename 要删除的文件名称 *
     * @return
     */
    public boolean deleteFile(String pathname, String filename) {
        boolean flag = false;
        try {System.out.println("开始删除文件");
            initFtpClient();
            // 切换 FTP 目录
            ftpClient.changeWorkingDirectory(pathname);
            ftpClient.dele(filename);
            ftpClient.logout();
            flag = true;
            System.out.println("删除文件胜利");
        } catch (Exception e) {System.out.println("删除文件失败");
            e.printStackTrace();} finally {if (ftpClient.isConnected()) {
                try {ftpClient.disconnect();
                } catch (IOException e) {e.printStackTrace();
                }
            }
        }
        return flag;
    }



}
退出移动版