ftputil.java

来自「cwbbs 云网论坛源码」· Java 代码 · 共 163 行

JAVA
163
字号
package cn.js.fan.util;import java.io.*;import org.apache.commons.net.ftp.*;public class FTPUtil {    FTPClient ftp;    String message;    public FTPUtil() {    }    public FTPClient getFTPClient() {        return ftp;    }    public boolean isConnected() {        return ftp.isConnected();    }    public String getMessage() {        return message;    }    public boolean connect(String host, int port, String userName, String password, boolean isPassiveMode) {        ftp = new FTPClient();        boolean re = false;        try {            ftp.connect(host, port);            message = ftp.getReplyString();            re = ftp.login(userName,                                   password);            message = ftp.getReplyString();            if (!re) {                message = ftp.getReplyString();                close();            } else {                if (isPassiveMode)                    ftp.enterLocalPassiveMode();                else                    ftp.enterLocalActiveMode();            }        }        catch (IOException e) {            message = "Connect error.";            System.out.println(getClass() + ": " + e.getMessage());        }        return re;            }        public boolean storeFile(String ftpPath, String filePath) throws IOException {        ftp.changeWorkingDirectory("");        ftp.setFileType(ftp.BINARY_FILE_TYPE);                String fileName;        if (ftpPath.equals(""))            return false;        else {            if (ftpPath.startsWith("/"))                ftpPath = ftpPath.substring(1);            String[] paths = ftpPath.split("/");            int len = paths.length;            fileName = paths[len-1];            for (int i=0; i<len-1; i++) {                ftp.changeWorkingDirectory(paths[i]);                if (ftp.getReplyCode()!=250) {                    ftp.makeDirectory(paths[i]);                    ftp.changeWorkingDirectory(paths[i]);                                    }            }        }        FileInputStream is = new FileInputStream(filePath);        boolean re = ftp.storeFile(fileName, is);                 is.close();        message = ftp.getReplyString();        return ftp.getReplyCode()==226;    }    public boolean completePendingCommand() throws IOException {        return ftp.completePendingCommand();    }    public void close() {        if (ftp != null && ftp.isConnected()) {            try {                ftp.disconnect();            }            catch (IOException e) {                e.printStackTrace();            }            ftp = null;        }    }        public int del(String path) {        int re = -1;        try {            re = ftp.dele(path);        }        catch (IOException e) {            e.printStackTrace();        }        return re;    }    public int getReplyCode() {        return ftp.getReplyCode();    }    public String getReplyMessage() {        if (message!=null)            return message;        else            return ftp.getReplyString();    }    public void test() throws Exception {        if (!connect("127.0.0.1", 21, "cws", "1", true)) {                        return;        }                                        System.out.println(getClass() + " replyString=" + ftp.getReplyString());               System.out.println(getClass() + " upfile replyString=" + ftp.getReplyString());        del("/111/222/2.jpg");        System.out.println(getClass() + " replyString1=" + ftp.getReplyString());                                                                                                                close();    }}

⌨️ 快捷键说明

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