日期:2014-05-20  浏览次数:21803 次

FTP上传文件,storeFileStream得到的输出流是空的
如题,使用的是apache的net类库,FTPClient,贴代码:
Java code

public static void main(String[] args) throws IOException {
        FTPClient ftp = new FTPClient();
        int reply;
        ftp.connect("***.***.***.***");
        ftp.login("***", "***");
        System.out.println(ftp.getReplyString());
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            System.err.println("FTP server refused connection.");
            System.exit(1);
        }
        InputStream is = new FileInputStream("d:/1/2/4.txt");
        OutputStream os = ftp.storeFileStream("/home/test/4.txt");
        byte[] buffer = new byte[1024];
        int len;
        while((len = is.read(buffer)) != -1) {
            os.write(buffer, 0, len);
        }
        is.close();
        os.close();
        ftp.logout();
        if(ftp.isConnected()) {
            ftp.disconnect();
        }
    }


连接是没有问题的,可以登录成功,但是storeFileStream得到的os却是null的,求助。


------解决方案--------------------
public static void main(String[] args) throws IOException {
FTPClient ftp = new FTPClient();
int reply;
ftp.connect("***.***.***.***");
if (!ftp.login("***", "***")) {
ftp.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
InputStream is = new FileInputStream("d:/1/2/4.txt");
OutputStream os = ftp.storeFileStream("/home/test/4.txt");
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
is.close();
os.close();
ftp.logout();
if(ftp.isConnected()) {
ftp.disconnect();
}
}