4

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

skaffman
  • 398,947
  • 96
  • 818
  • 769
M.J.
  • 16,266
  • 28
  • 75
  • 97

4 Answers4

2

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.

Josh
  • 36
  • 3
  • 1
    Apparently the stream will always get flushed when `close()` is called, according to the accepted answer of this question: http://stackoverflow.com/questions/2732260/in-java-when-i-call-outputstream-close-i-always-need-to-call-outputstream-flus – Chris Dennett Jul 16 '11 at 15:36
  • Well I'm not sure about that, but I know that flush(); has fixed problems for me in the past. Anywho I'll edit my post with a new idea. – Josh Jul 16 '11 at 15:41
  • @chris it's not mentioned in the base OutputStream though it is in the FilterOutputStream – ratchet freak Jul 16 '11 at 15:42
  • Perhaps it's just the input stream not reading the file before the stream is copied? – Josh Jul 16 '11 at 15:49
  • But if he copies the stream doesn't that copy whats currently stored? Henceforth he would have to read that file first so it copies actual data? – Josh Jul 16 '11 at 16:07
  • @Josh: `Util.copyStream(input, out);` will read data from `input` (until end of stream) and write all this data to `output`. Nothing else should be needed here. – Paŭlo Ebermann Jul 16 '11 at 16:22
0

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.

Mercury
  • 7,430
  • 3
  • 42
  • 54
0

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.

Alex Gitelman
  • 24,429
  • 7
  • 52
  • 49
  • Are you sure that no exception is thrown? I suggest that you step with debugger in your copy method and see if data is actually being transferred. – Alex Gitelman Jul 16 '11 at 15:59
0

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.

Community
  • 1
  • 1
Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210