0 kb file created once FTP is done in java

2019-02-24 02:22发布

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();
input.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

4条回答
干净又极端
2楼-- · 2019-02-24 02:48

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.

查看更多
Animai°情兽
3楼-- · 2019-02-24 02:53

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.

查看更多
Summer. ? 凉城
4楼-- · 2019-02-24 02:55

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.

查看更多
劫难
5楼-- · 2019-02-24 03:07

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.

查看更多
登录 后发表回答