java ftpclient.storefile,FtpClient storeFile总是返回False

Please figure this out. The code runs properly without any exception.

FTPClient ftp = new FTPClient();

ftp.connect(server);

if(!ftp.login(username, password))

{

ftp.logout();

return false;

}

int reply = ftp.getReplyCode();

if (!FTPReply.isPositiveCompletion(reply))

{

ftp.disconnect();

return false;

}

InputStream in = new FileInputStream(localfile);

ftp.setFileType(ftp.BINARY_FILE_TYPE, ftp.BINARY_FILE_TYPE);

ftp.setFileTransferMode(ftp.BINARY_FILE_TYPE);

Store = ftp.storeFile(destinationfile, in);

in.close();

ftp.logout();

ftp.disconnect();

}

catch (Exception ex)

{

ex.printStackTrace();

return false;

}

return Store;

Buttttttttt

return statement always return false and the file is not uploaded on the server. Some one please help on this.

For your information,

1) i am in an office network. ---> do we need to add any proxies?

File file = new File("C:\\Users\\sg0214273\\Desktop\\seagate\\seagate.txt");

FileInputStream input = new FileInputStream(file);

client.setFileType(FTP.BINARY_FILE_TYPE);

if (!client.storeFile(file.getName(), input)) {

System.out.println("upload failed!");

}

reply = client.getReplyCode();

if(!FTPReply.isPositiveCompletion(reply)) {

System.out.println("upload failed!");

}

Login success...

230 User ******** logged in.

upload failed!-----> is form boolean return value of storefile

upload failed!---------> is from replycode...

Logout from FTP server...

Please help out

解决方案

The exact failure message can be found by calling FtpClient#getReplyCode(). From that page (my emphasis):

Immediately after connecting is the only real time you need to check

the reply code (because connect is of type void). The convention for

all the FTP command methods in FTPClient is such that they either

return a boolean value or some other value. The boolean methods return

true on a successful completion reply from the FTP server and false on

a reply resulting in an error condition or failure. The methods

returning a value other than boolean return a value containing the

higher level data produced by the FTP command, or null if a reply

resulted in an error condition or failure. If you want to access the

exact FTP reply code causing a success or failure, you must call

getReplyCode after a success or failure.

To see what a return code means, you can see Wikipedia: List of FTP server return codes.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: ftpclient.storefileFTPClient类中的一个方法,用于将本地文件上传到FTP服务器上。 具体用法如下: 1. 首先创建一个FTPClient对象,并连接到FTP服务器。 2. 然后使用storefile方法上传文件,该方法需要传入两个参数:远程文件名和本地文件名。 3. 上传完成后,关闭FTP连接。 示例代码: ``` FTPClient ftpClient = new FTPClient(); ftpClient.connect("ftp.example.com"); ftpClient.login("username", "password"); File localFile = new File("localfile.txt"); InputStream inputStream = new FileInputStream(localFile); ftpClient.storeFile("remotefile.txt", inputStream); inputStream.close(); ftpClient.logout(); ftpClient.disconnect(); ``` 以上代码将本地文件localfile.txt上传到FTP服务器上,并将其命名为remotefile.txt。 ### 回答2: FTPClientJava中常用的FTP客户端连接工具,其中ftpclient.storefileFTPClient上传文件的方法之一。 使用ftpclient.storefile进行上传文件,首先需要创建一个FTP连接,并使用FTPClient的login方法进行登录。然后,需要设置上传文件的相关参数,包括上传文件的路径、文件名、上传模式等。 在设置好相关参数之后,调用ftpclient.storefile方法即可上传文件到指定的FTP服务器中。上传文件的过程需要关注的是上传进度、异常处理和上传结果的返回值。FTPClient中提供了多种方法来获取上传进度、处理异常和获得上传结果。 需要注意的是,在上传文件的过程中,我们需要保证文件名和上传路径的正确性,避免出现上传路径不存在或文件名重复等问题。同时,也需要检查上传文件的访问权限,确保上传文件的可读性和可写性。 总之,ftpclient.storefile方法是Java上传文件FTP服务器的重要方法,使用时需要注意设置上传参数并处理相关异常,以确保文件能够正确地上传到FTP服务器中。 ### 回答3: FTPClientJava中常用的FTP客户端库,可以连接到FTP服务器并执行各种FTP操作。其中,storefileFTPClient中用来上传文件的方法。 storefile的基本用法是将本地文件上传到FTP服务器中指定的路径下。使用该方法需要执行以下步骤: 1. 连接到FTP服务器。可以通过FTPClient的connect方法连接FTP服务器,需要指定服务器的主机地址、端口号、用户名和密码等信息。 2. 设置传输模式。FTPClient支持两种传输模式:二进制传输和ASCII传输。通过设置FTPClient的setFileType方法来指定传输模式,一般情况下使用二进制传输。 3. 设置工作目录。使用FTPClient的changeWorkingDirectory方法将FTP服务器的当前工作目录切换到要上传文件的目录。 4. 执行上传操作。调用FTPClientstoreFile方法上传文件。需要指定本地文件的路径和文件名,以及上传后保存在FTP服务器中的文件名。 5. 关闭连接。上传完成后,使用FTPClient的disconnect方法关闭与FTP服务器之间的连接。 以下是storefile方法的代码示例: ``` FTPClient ftpClient = new FTPClient(); ftpClient.connect("ftp.example.com", 21); ftpClient.login("username", "password"); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.changeWorkingDirectory("/upload"); File localFile = new File("localfile.txt"); InputStream inputStream = new FileInputStream(localFile); ftpClient.storeFile("uploadedfile.txt", inputStream); inputStream.close(); ftpClient.logout(); ftpClient.disconnect(); ``` 在实际使用中,还需要处理上传文件失败的情况,并且考虑到网络传输中可能出现的稳定性问题,需要进行断点续传等操作。对于大文件的上传,可以考虑使用多线程或分片上传等技术来提高上传效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值