当前位置: 代码迷 >> J2SE >> FTP上传文件,storeFileStream得到的输出流是空的解决办法
  详细解决方案

FTP上传文件,storeFileStream得到的输出流是空的解决办法

热度:399   发布时间:2016-04-24 01:31:53.0
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();
}
}
  相关解决方案