⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftptest.java

📁 Java案例开发集锦,里面提供了很好的学习例子
💻 JAVA
字号:
//chp 9/** * @author wangmj */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;        }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -