Win10 搭建FTP环境,并使用Java实现上传,下载,删除

测试的环境一般都是在自己电脑上面装的,现在一般都使用Win10开发

搭建FTP:

第一步:打开控制面板:点击程序

第二步:

第三步:

 然后点击确认后等待完成

完成后在启动中找到IIS管理器

 打开

 在网站上右击,添加FTP站点

配置FTP名称并,设置文件夹

 

 设置IP,端口,和SSL证书,一般自己测试是没有证书的,就点击无

 配置身份认证和授权,然后点击完成

 可以看到已经已启动了,

 然后去测试一下是否可以使用

在文件夹地址栏上输入ftp://ip访问

 可以看到是可以使用的

然后开始写测试代码

首先需要导入

common-net包,我使用的是Maven项目直接配置依赖

<dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.3</version>
        </dependency>

创建一个测试文件

 文件内容

然后写代码,公共代码部分大家可以自己抽取,因为是测试demo我就没有抽取了

package utils;

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

import java.io.*;

/**
 * FTP工具类
 * @author ZYGisComputer
 */
public class FTPUtil {

    final static String url = "192.168.0.23";

    final static int port = 21;

    final static String username = "电脑用户名";

    final static String password = "密码";

    final static String path = "F:\FTP";

    final static String filename = "test.txt";

    final static String remotePath = "\";


    public static void main(String[] args) throws FileNotFoundException {

        InputStream in = new FileInputStream(new File("E:\dance\oracle\src\main\resources\FTP\test.txt"));

        // 测试上传
//        boolean b = uploadFile(url, port, username, password, path, filename, in);

//        System.out.println(b);

//         测试下载
        boolean b = downloadFile(url, port, username, password, remotePath, filename, "E:\dance\oracle\src\main\resources\download");

        System.out.println(b);

        // 测试删除
//        boolean b = deleteFtpFile(url, port, username, password, remotePath, filename);

//        System.out.println(b);

    }

    /**
     * Description: 向FTP服务器上传文件
     * @param url FTP服务器hostname
     * @param port FTP服务器端口,如果默认端口请写-1
     * @param username FTP登录账号
     * @param password FTP登录密码
     * @param path FTP服务器保存目录
     * @param filename 上传到FTP服务器上的文件名
     * @param input 输入流
     * @return 成功返回true,否则返回false
     */
    public static boolean uploadFile(String url, int port, String username, String password, String path,
                                     String filename, InputStream input)
    {
        boolean success = false;
        FTPClient ftp = new FTPClient();
        try
        {
            int reply;

            // 连接FTP服务器
            if (port > -1)
            {
                ftp.connect(url, port);
            }
            else
            {
                ftp.connect(url);
            }

            // 登录FTP
            ftp.login(username, password);
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply))
            {
                ftp.disconnect();
                return success;
            }
            ftp.changeWorkingDirectory(path);
            ftp.storeFile(filename, input);

            input.close();
            ftp.logout();
            success = true;
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (ftp.isConnected())
            {
                try
                {
                    ftp.disconnect();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return success;
    }

    /**
     * Description: 从FTP服务器下载文件
     * @Version1.0 Jul 27, 2008 5:32:36 PM by 崔红保(cuihongbao@d-heaven.com)创建
     * @param url FTP服务器hostname
     * @param port FTP服务器端口
     * @param username FTP登录账号
     * @param password FTP登录密码
     * @param remotePath FTP服务器上的相对路径
     * @param fileName 要下载的文件名
     * @param localPath 下载后保存到本地的路径
     * @return
     */
    public static boolean downloadFile(String url, int port, String username, String password, String remotePath,
                                       String fileName, String localPath)
    {
        boolean success = false;
        FTPClient ftp = new FTPClient();
        try
        {
            int reply;

            // 连接FTP服务器
            if (port > -1)
            {
                ftp.connect(url, port);
            }
            else
            {
                ftp.connect(url);
            }

            ftp.login(username, password);//登录
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply))
            {
                ftp.disconnect();
                return success;
            }
            ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录
            FTPFile[] fs = ftp.listFiles();
            for (FTPFile ff : fs)
            {
                if (ff.getName().equals(fileName))
                {
                    File localFile = new File(localPath + "/" + ff.getName());

                    OutputStream is = new FileOutputStream(localFile);
                    ftp.retrieveFile(ff.getName(), is);
                    is.close();
                }
            }

            ftp.logout();
            success = true;
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (ftp.isConnected())
            {
                try
                {
                    ftp.disconnect();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return success;
    }

    /**
     * <删除FTP上的文件>
     * <远程删除FTP服务器上的录音文件>
     * @param url FTP服务器IP地址
     * @param port FTP服务器端口
     * @param username FTP服务器登录名
     * @param password FTP服务器密码
     * @param remotePath 远程文件路径
     * @param fileName 待删除的文件名
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static boolean deleteFtpFile(String url, int port, String username, String password, String remotePath,
                                        String fileName)
    {
        boolean success = false;
        FTPClient ftp = new FTPClient();
        try
        {
            int reply;

            // 连接FTP服务器
            if (port > -1)
            {
                ftp.connect(url, port);
            }
            else
            {
                ftp.connect(url);
            }

            // 登录
            ftp.login(username, password);
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply))
            {
                ftp.disconnect();
                return success;
            }

            // 转移到FTP服务器目录
            ftp.changeWorkingDirectory(remotePath);
            success = ftp.deleteFile(remotePath + "/" + fileName);
            ftp.logout();
        }
        catch (IOException e)
        {
            e.printStackTrace();
            success = false;
        }
        finally
        {
            if (ftp.isConnected())
            {
                try
                {
                    ftp.disconnect();
                }
                catch (IOException e)
                {
                   e.printStackTrace();
                }
            }
        }
        return success;
    }

}

代码写完开始测试:

测试上传:

执行结果:

true

查看文件夹:

 上传成功

测试下载:

执行结果:

true

查看文件夹:

下载成功

测试删除:

执行结果:

true

查看文件夹

 删除成功

到此java操作FTP完成,如果要用到项目中可以稍作修改即可

作者:彼岸舞

时间:2020924

内容关于:工作中用到的小技术

本文来源于网络,只做技术分享,一概不负任何责任

原文地址:https://www.cnblogs.com/flower-dance/p/13725041.html