栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

jdk1.8 FtpClientUtil

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

jdk1.8 FtpClientUtil

jdk1.8,FtpClientUtil 上传、下载单个文件

  
        
            commons-net
            commons-net
            3.8.0
        
import java.io.*;

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


public class FtpClientUtil {
    private FTPClient ftp = new FTPClient();
    private String server = "192.168.1.10";
    private String username = "dev";
    private String password = "123456";

    
    public void upload(String filename, String pathName, String remoteFileName) {

//		ftp.configure(config);
        boolean error = false;
        try {
            int reply;
            ftp.connect(server);
            ftp.login(username, password);
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            ftp.changeWorkingDirectory(pathName);
            try (final InputStream input = new FileInputStream(filename)) {
                ftp.storeFile(remoteFileName, input);
            }
            System.out.println("Connected to " + server + ".");
            System.out.print(ftp.getReplyString());

            // After connection attempt, you should check the reply code to verify
            // success.
            reply = ftp.getReplyCode();

            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                System.err.println("FTP server refused connection.");
                System.exit(1);
            }
            // transfer files
            ftp.logout();
        } catch (IOException e) {
            error = true;
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                    // do nothing
                }
            }
            System.exit(error ? 1 : 0);
        }
    }
    public void download(String filename, String pathName, String remoteFileName) {

//		ftp.configure(config);
        boolean error = false;
        try {
            int reply;
            ftp.connect(server);
            ftp.login(username, password);
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            ftp.changeWorkingDirectory(pathName);
            try (final OutputStream output = new FileOutputStream(filename)) {
                ftp.retrieveFile(remoteFileName, output);
            }
            System.out.println("Connected to " + server + ".");
            System.out.print(ftp.getReplyString());

            // After connection attempt, you should check the reply code to verify
            // success.
            reply = ftp.getReplyCode();

            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                System.err.println("FTP server refused connection.");
                System.exit(1);
            }
            // transfer files
            ftp.logout();
        } catch (IOException e) {
            error = true;
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                    // do nothing
                }
            }
            System.exit(error ? 1 : 0);
        }
    }


    public static void main(String[] args) {

        String fileName = "D:/a.png";
        String pathName = "test";
        String remoteFileName = "a1.png";

        FtpClientUtil ftpc = new FtpClientUtil();
        //将D:/a.png 上传到/test/a1.png
        ftpc.upload(fileName, pathName, remoteFileName);

        fileName="D:/a2.jpg";
        //将/test/a1.png 下载到D:/a2.png
        ftpc.download(fileName, pathName, remoteFileName);

    }

}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/950915.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号