开发者

0 kb file created once FTP is done in java

I am trying to FTP a file on to a remote machine. Below is my code :-

FTPClient ftpClient = new FTPClient(); 
ftpClient.connect("home.abc.com"); 
ftpClient.login("remote", "guesst12"); 
int replyCode = ftpClient.getReplyCode(); 
ftpClient.changeWorkingDirectory("share")) 
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
InputStream input = new FileInputStream(new File("H:/testFile.txt"));
OutputStream out =  ftpClient.storeFileStream("testFile.txt");
Util.copyStream(input, out);
out.close();
in开发者_运维问答put.close();
ftpClient.completePendingCommand()
ftpClient.logout();
ftpClient.disconnect();

When i execute this piece of code, the code is executed without any issues, but at the remote machine, when i check the file, the file is being created, but with no content (OKB) file. Am i missing something in code.

[Update] : I tried with the following code for storing file :-

if(ftpClient.storeFile("testCopy.txt", input)) {
    System.out.println("File Stored Successfully");
}
System.out.println(ftpClient.getReplyString());

Now the reply code i recieved is :- 451 Failure writing to local file. What does that means.

Thanks


After looking at it over and over I keep coming up with different things.

Are you sure that the InputStream is reading the file before your copying the stream? Because I'm not sure FileInputStream read's the file on initiation.


I suspect that the problem is inUtil.copyStream, which code you didn't provide. I highly recommend that you use IOutils from Apache Commons IO to copy streams.


Looking at older questions here with similar problems, it looks like you hit a bug of the Commons-NET library (of which the FTPClient is a part).

Try to install a newer version (3.0.1 or later), or an earlier version (2.2) to fix this.


One of the reasons running into FTP error 451 when trying to copy a file over FTP especially if you see 0 sized file created on the server side, is probably due to No Space on Disk.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜