Java 227进入被动模式(124153,94,30242138)

Java 227进入被动模式(124153,94,30242138),java,android,ftp,sftp,Java,Android,Ftp,Sftp,我使用SimpleFTP.java代码通过FTP上传图像。我收到标题中提到的错误,无法上载图像。它只是将图像保存在大小为0KB的数据库中。请帮助我解决这个问题,因为我搜索了一整天,但没有找到正确的解决方案 public class SimpleFTP { /** * Create an instance of SimpleFTP. */ public SimpleFTP() { } /** * Connects to the

我使用SimpleFTP.java代码通过FTP上传图像。我收到标题中提到的错误,无法上载图像。它只是将图像保存在大小为0KB的数据库中。请帮助我解决这个问题,因为我搜索了一整天,但没有找到正确的解决方案

public class SimpleFTP {


    /**
     * Create an instance of SimpleFTP.
     */
    public SimpleFTP() {

    }


    /**
     * Connects to the default port of an FTP server and logs in as
     * anonymous/anonymous.
     */
    public synchronized void connect(String host) throws IOException {
        connect(host, 21);
    }


    /**
     * Connects to an FTP server and logs in as anonymous/anonymous.
     */
    public synchronized void connect(String host, int port) throws IOException {
        connect(host, port, "anonymous", "anonymous");
    }


    /**
     * Connects to an FTP server and logs in with the supplied username
     * and password.
     */
    public synchronized void connect(String host, int port, String user, String pass) throws IOException {
        if (socket != null) {
            throw new IOException("SimpleFTP is already connected. Disconnect first.");
        }
        socket = new Socket(host, port);
        reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

        String response = readLine();
        if (!response.startsWith("220 ")) {
            throw new IOException("SimpleFTP received an unknown response when connecting to the FTP server: " + response);
        }

        sendLine("USER " + user);

        response = readLine();
        if (!response.startsWith("331 ")) {
            throw new IOException("SimpleFTP received an unknown response after sending the user: " + response);
        }

        sendLine("PASS " + pass);

        response = readLine();
        if (!response.startsWith("230 ")) {
            throw new IOException("SimpleFTP was unable to log in with the supplied password: " + response);
        }

    }
        /**
     * Disconnects from the FTP server.
     */
    public synchronized void disconnect() throws IOException {
        try {
            sendLine("QUIT");
        } finally {
            socket = null;
        }
    }


    /**
     * Returns the working directory of the FTP server it is connected to.
     */
    public synchronized String pwd() throws IOException {
        sendLine("PWD");
        String dir = null;
        String response = readLine();
        if (response.startsWith("257 ")) {
            int firstQuote = response.indexOf('\"');
            int secondQuote = response.indexOf('\"', firstQuote + 1);
            if (secondQuote > 0) {
                dir = response.substring(firstQuote + 1, secondQuote);
            }
        }
        return dir;
    }


    /**
     * Changes the working directory (like cd). Returns true if successful.
     */
    public synchronized boolean cwd(String dir) throws IOException {
        sendLine("CWD " + dir);
        String response = readLine();
        return (response.startsWith("250 "));
    }


    /**
     * Sends a file to be stored on the FTP server.
     * Returns true if the file transfer was successful.
     * The file is sent in passive mode to avoid NAT or firewall problems
     * at the client end.
     */
    public synchronized boolean stor(File file) throws IOException {
        if (file.isDirectory()) {
            throw new IOException("SimpleFTP cannot upload a directory.");
        }

        String filename = file.getName();

        return stor(new FileInputStream(file), filename);
    }


    /**
     * Sends a file to be stored on the FTP server.
     * Returns true if the file transfer was successful.
     * The file is sent in passive mode to avoid NAT or firewall problems
     * at the client end.
     */
    public synchronized boolean stor(InputStream inputStream, String filename) throws IOException {

        BufferedInputStream input = new BufferedInputStream(inputStream);

        sendLine("PASV");
        String response = readLine();//227
      Log.e("RESPONSE ", response);
        if (!response.startsWith("200 ") && !response.startsWith("227 ")) {
            throw new IOException("SimpleFTP could not request passive mode: " + response);
        }

        String ip = null;
        int port = -1;
        int opening = response.indexOf('(');
        int closing = response.indexOf(')', opening + 1);
        if (closing > 0) {
            String dataLink = response.substring(opening + 1, closing);
            StringTokenizer tokenizer = new StringTokenizer(dataLink, ",");
            try {
                ip = tokenizer.nextToken() + "." + tokenizer.nextToken() + "." + tokenizer.nextToken() + "." + tokenizer.nextToken();
                port = Integer.parseInt(tokenizer.nextToken()) * 256 + Integer.parseInt(tokenizer.nextToken());
                //Log.e("FTP ",String.valueOf(port)+" P "+String.valueOf(port));
            } catch (Exception e) {
                throw new IOException("SimpleFTP received bad data link information: " + response);
            }
        }

        sendLine("STOR " + filename);

        Socket dataSocket = new Socket(ip, port);

        response = readLine();
        if (!response.startsWith("150 ")) {
            throw new IOException("SimpleFTP was not allowed to send the file: " + response);
        }

        BufferedOutputStream output = new BufferedOutputStream(dataSocket.getOutputStream());
        byte[] buffer = new byte[4096];
        int bytesRead = 0;
        while ((bytesRead = input.read(buffer)) != -1) {
            output.write(buffer, 0, bytesRead);
        }
        output.flush();
        output.close();
        input.close();

        response = readLine();
        return response.startsWith("226 ");
    }


    /**
     * Enter binary mode for sending binary files.
     */
    public synchronized boolean bin() throws IOException {
        sendLine("TYPE I");
        String response = readLine();
        return (response.startsWith("200 "));
    }


    /**
     * Enter ASCII mode for sending text files. This is usually the default
     * mode. Make sure you use binary mode if you are sending images or
     * other binary data, as ASCII mode is likely to corrupt them.
     */
    public synchronized boolean ascii() throws IOException {
        sendLine("TYPE A");
        String response = readLine();
        return (response.startsWith("200 "));
    }


    /**
     * Sends a raw command to the FTP server.
     */
    private void sendLine(String line) throws IOException {
        if (socket == null) {
            throw new IOException("SimpleFTP is not connected.");
        }
        try {
            writer.write(line + "\r\n");
            writer.flush();
            if (DEBUG) {
                System.out.println("> " + line);
            }
        } catch (IOException e) {
            socket = null;
            throw e;
        }
    }

    private String readLine() throws IOException {
        String line = reader.readLine();
        if (DEBUG) {
            System.out.println("< " + line);
        }
        return line;
    }

    private Socket socket = null;
    private BufferedReader reader = null;
    private BufferedWriter writer = null;

    private static boolean DEBUG = false;


}
公共类SimpleFTP{
/**
*创建SimpleFTP的实例。
*/
公共SimpleFTP(){
}
/**
*连接到FTP服务器的默认端口,并以
*匿名/匿名。
*/
公共同步的void connect(字符串主机)引发IOException{
连接(主机,21);
}
/**
*连接到FTP服务器并以匿名/匿名身份登录。
*/
公共同步的void connect(字符串主机、int端口)引发IOException{
连接(主机、端口、“匿名”、“匿名”);
}
/**
*连接到FTP服务器并使用提供的用户名登录
*和密码。
*/
公共同步的void connect(字符串主机、int端口、字符串用户、字符串传递)引发IOException{
if(套接字!=null){
抛出新IOException(“SimpleFTP已连接。请先断开连接”);
}
套接字=新套接字(主机、端口);
reader=new BufferedReader(new InputStreamReader(socket.getInputStream());
writer=newbufferedwriter(newoutputstreamwriter(socket.getOutputStream());
字符串响应=readLine();
如果(!response.startsWith(“220”)){
抛出新IOException(“SimpleFTP在连接到FTP服务器时收到未知响应:“+response”);
}
发送线(“用户”+用户);
response=readLine();
如果(!response.startsWith(“331”)){
抛出新IOException(“SimpleFTP在发送用户:“+response”后收到未知响应);
}
发送线(“通过”+通过);
response=readLine();
如果(!response.startsWith(“230”)){
抛出新IOException(“SimpleFTP无法使用提供的密码登录:“+响应”);
}
}
/**
*断开与FTP服务器的连接。
*/
公共同步的void disconnect()引发IOException{
试一试{
发送线(“退出”);
}最后{
socket=null;
}
}
/**
*返回连接到的FTP服务器的工作目录。
*/
公共同步字符串pwd()引发IOException{
发送线(“PWD”);
字符串dir=null;
字符串响应=readLine();
if(response.startsWith(“257”)){
int firstQuote=response.indexOf(“\”);
int secondQuote=response.indexOf(“\”,firstQuote+1);
如果(第二个报价>0){
dir=response.substring(第一个引号+1,第二个引号);
}
}
返回目录;
}
/**
*更改工作目录(如cd)。如果成功,则返回true。
*/
公共同步布尔cwd(字符串目录)引发IOException{
发送线(“CWD”+dir);
字符串响应=readLine();
返回(response.startsWith(“250”));
}
/**
*发送要存储在FTP服务器上的文件。
*如果文件传输成功,则返回true。
*文件以被动模式发送,以避免NAT或防火墙问题
*在客户端。
*/
公共同步布尔存储(文件)引发IOException{
if(file.isDirectory()){
抛出新IOException(“SimpleFTP无法上载目录”);
}
字符串文件名=file.getName();
返回stor(新文件输入流(文件),文件名);
}
/**
*发送要存储在FTP服务器上的文件。
*如果文件传输成功,则返回true。
*文件以被动模式发送,以避免NAT或防火墙问题
*在客户端。
*/
公共同步布尔存储(InputStream InputStream,字符串文件名)引发IOException{
BufferedInputStream输入=新的BufferedInputStream(inputStream);
发送线(“PASV”);
字符串响应=readLine();//227
Log.e(“响应”,响应);
如果(!response.startsWith(“200”)和&!response.startsWith(“227”)){
抛出新的IOException(“SimpleFTP无法请求被动模式:“+响应”);
}
字符串ip=null;
int端口=-1;
int opening=response.indexOf('(');
int closing=response.indexOf('),opening+1);
如果(关闭>0){
字符串dataLink=response.substring(打开+1,关闭);
StringTokenizer tokenizer=新的StringTokenizer(数据链接,“,”);
试一试{
ip=tokenizer.nextToken()+“+”+tokenizer.nextToken()+“+”+tokenizer.nextToken()+“+”+tokenizer.nextToken();
port=Integer.parseInt(tokenizer.nextToken())*256+Integer.parseInt(tokenizer.nextToken());
//Log.e(“FTP”,String.valueOf(port)+“P”+String.valueOf(port));
}捕获(例外e){
抛出新IOException(“SimpleFTP接收到错误的数据链路信息:“+响应”);
}
}
sendLine(“STOR”+文件名);
套接字数据套接字=新套接字(ip,端口);
response=readLine();
如果(!response.startsWith(“150”)){
抛出新IOException(“SimpleFTP不允许发送文件:“+响应”);
}
BufferedOutputStream输出=新的BufferedOutputStream(dataSocket.getOutputStream());
字节[]缓冲区=新字节[4096];
int字节读取=0;
而((bytesRead=input.read(buffer))!=-1){
输出写入(缓冲区,0,字节读取);
}
output.flush();
output.close();
input.close();
response=readLine();
返回响应。开始使用(“226”);
}
/**
*输入二进制模式以发送二进制文件