在JAVA中实现FTP功能

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.SocketException;

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

public final class FtpTest {

        public static final void main(String[] args) {
                int base = 0;
                boolean storeFile = false, binaryTransfer = false, error = false;
                String server, username, password, remote, local;
                FTPClient ftp;
                server = "172.16.1.1";
                username = "public";
                password = "public";
                remote = "ht/isic-0.05.tar";
                local = "d://isic-0.05.tar";

                ftp = new FTPClient();
                ftp.addProtocolCommandListener(new PrintCommandListener(
                                new PrintWriter(System.out)));
                //----------------新添加的----------------
                FTPClient ftest = new FTPClient();
                try {
                        ftest.connect(server);
                        ftest.login(username, password);
                        FTPFile[] files = ftest.listFiles();
                        System.out.println("输出ftp服务器上根目录下的文件或文件夹名字开始:/n");
                        for (int i = 0; i < files.length; i++) {
                                System.out.println(IsoToCh(files[i].getName()));
                        }
                        System.out.println("输出ftp服务器上根目录下的文件或文件夹名字结束。/n");
                } catch (SocketException e1) {
                        // TODO 自动生成 catch 块
                        e1.printStackTrace();
                } catch (IOException e1) {
                        // TODO 自动生成 catch 块
                        e1.printStackTrace();
                }
                //---------------------------------------------------

                try {
                        int reply;
                        ftp.connect(server);
                        System.out.println("Connected to " + server + ".");

                        // 尝试连接后,需要检验返回代码以验证是否连接成功
                        reply = ftp.getReplyCode();

                        if (!FTPReply.isPositiveCompletion(reply)) {
                                ftp.disconnect();
                                System.err.println("FTP server refused connection.");
                                System.exit(1);
                        }
                } catch (IOException e) {
                        if (ftp.isConnected()) {
                                try {
                                        ftp.disconnect();
                                } catch (IOException f) {
                                        // do nothing
                                }
                        }
                        System.err.println("Could not connect to server.");
                        e.printStackTrace();
                        System.exit(1);
                }

                __main: try {
                        if (!ftp.login(username, password)) {
                                ftp.logout();
                                error = true;
                                break __main;
                        }

                        System.out.println("Remote system is " + ftp.getSystemName());

                        if (binaryTransfer)
                                ftp.setFileType(FTP.BINARY_FILE_TYPE);

                        // 默认状态下使用passive mode因为大多都在防火墙后面
                        ftp.enterLocalPassiveMode();

                        if (storeFile) {
                                InputStream input;
                                input = new FileInputStream(local);
                                ftp.storeFile(remote, input);
                                input.close();
                        } else {
                                OutputStream output;
                                output = new FileOutputStream(local);
                                ftp.retrieveFile(remote, output);
                                output.close();
                        }
                        ftp.logout();
                } catch (FTPConnectionClosedException e) {
                        error = true;
                        System.err.println("Server closed connection.");
                        e.printStackTrace();
                } catch (IOException e) {
                        error = true;
                        e.printStackTrace();
                } finally {
                        if (ftp.isConnected()) {
                                try {
                                        ftp.disconnect();
                                } catch (IOException f) {
                                        // do nothing
                                }
                        }
                }
                System.exit(error ? 1 : 0);
        } // end main
        /**
         * iso8859_1转换为gb2312编码
         * @param param
         * @return
         */
        public static String IsoToCh(String param) {
                String temp = new String(param);
                //这里都需要new一下,否则websphere4.0不认。
                try {
                        temp = new String(temp.getBytes("iso8859_1"), "gb2312");
                } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                }
                return temp;
        }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值