Chinaunix首页 | 论坛 | 博客
  • 博客访问: 533506
  • 博文数量: 135
  • 博客积分: 3568
  • 博客等级: 中校
  • 技术积分: 1942
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-19 17:52
文章分类

全部博文(135)

文章存档

2012年(29)

2011年(41)

2010年(26)

2009年(12)

2008年(9)

2007年(12)

2006年(6)

分类: Java

2010-12-08 13:37:48

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.security.NoSuchAlgorithmException;

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPConnectionClosedException;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemManager;
import org.apache.commons.vfs.VFS;

/**
 * 因为需要用到FTPS协议读取数据文件,调查了下,本来期望使用 Apache Commons VFS的,
 * 结果发现 VFS 支持 FTP,SFTP以及其他众多协议,却惟独不支持 FTPS。
 * 无奈,只好改吧改吧 Apache Commons Net的FTPS的例子来使用了。
 *
 * @author btpka3@163.com
 */

public class FTPS {

    // 使用Apache Commons Net 连接 SFTP 服务器(协议:SFTP)
    public static final void main(String[] args)
            throws NoSuchAlgorithmException {
        boolean storeFile = false;
        boolean binaryTransfer = false;
        boolean error = false;
        String protocol = "SSL"; // SSL/TLS
        String server = "192.168.1.28";
        String username = "zhangll";
        String password = "123456";
        String remote = "sss.txt";
        String local = "C:/sss.txt";

        FTPSClient ftps = new FTPSClient(protocol);
        ftps.addProtocolCommandListener(new PrintCommandListener(
                new PrintWriter(System.out)));

        try {
            ftps.connect(server);
            System.out.println("Connected to " + server + ".");

            // After connection attempt, you should check the reply code to verify
            // success.
            int reply = ftps.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftps.disconnect();
                System.err.println("FTP server refused connection.");
                System.exit(1);
            }
        } catch (IOException e) {
            if (ftps.isConnected()) {
                try {
                    ftps.disconnect();
                } catch (IOException f) {
                    // do nothing
                }
            }
            System.err.println("Could not connect to server.");
            e.printStackTrace();
            System.exit(1);
        }

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

            System.out.println("Remote system is " + ftps.getSystemName());
            if (binaryTransfer)
                ftps.setFileType(FTP.BINARY_FILE_TYPE);

            // Use passive mode as default because most of us are
            // behind firewalls these days.
            ftps.enterLocalPassiveMode();
            if (storeFile) {
                InputStream input;
                input = new FileInputStream(local);
                ftps.storeFile(remote, input);
                input.close();
            } else {
                OutputStream output;
                output = new FileOutputStream(local);
                ftps.retrieveFile(remote, output);
                output.close();
            }

            ftps.logout();
        } catch (FTPConnectionClosedException e) {
            error = true;
            System.err.println("Server closed connection.");
            e.printStackTrace();
        } catch (IOException e) {
            error = true;
            e.printStackTrace();
        } finally {
            if (ftps.isConnected()) {
                try {
                    ftps.disconnect();
                } catch (IOException f) {
                    // do nothing
                }
            }
        }

        System.exit(error ? 1 : 0);
    }

    // 使用 AapcheCommons VFS 连接 FTP服务器(协议FTP)
    public static void testFtps() throws IOException {
        FileSystemManager fsManager = VFS.getManager();
        FileObject file = fsManager.resolveFile("ftp://zhangll:123456@192.168.1.28/sss.txt");
        InputStream in = file.getContent().getInputStream();
        System.out.println(IOUtils.toString(in));
    }
}


阅读(2363) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~