I write a class which inherit form sun.net.ftp.FtpClient. In this
class, there is a method named downLoadFile to download file from a ftp
server. Here is some code of this method:
byte[] content = new byte[1024];
super.cd(remotePath);
RandomAccessFile file = new RandomAccessFile(downLoadFileName, "rw");
TelnetInputStream in = super.get(remoteFile);
DataInputStream input = new DataInputStream(in);
while(input.available() > 0) {
input.read(content);
file.write(content);
}
in.close();
file.close();
When I use this method, I got a error:
sun.net.ftp.FtpProtocolException: Error reading FTP pending reply
Anybody can help me?
> I write a class which inherit form sun.net.ftp.FtpClient. In this
> class, there is a method named downLoadFile to download file from a ftp
[quoted text clipped - 15 lines]
> sun.net.ftp.FtpProtocolException: Error reading FTP pending reply
> Anybody can help me?
Which of the operations causes the exception?
This may be unrelated, but your read loop has a number of serious
problems.
First, the call to available() doesn't serve any useful purpose and in
fact can give you problems on a slow network. Simply read from the
input stream until you reach EOF.
Second, don't expect read() to always read the amount you've
requested. Instead, check the return value and write only the same
number of bytes.
Also, you should be using a plain InputStream to read from the remote
and a plain OutputStream to write the file. Other stream types perform
various types of data conversion that can corrupt the contents of most
files.
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Roedy Green - 10 Dec 2005 22:13 GMT
>> while(input.available() > 0) {
>> input.read(content);
>> file.write(content);
>> }
you can TEMPORARILY have no input available with a socket.
See http://mindprod.com/jgloss/readblocking.html
http://mindprod.com/jgloss/readeverything.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
long990802@gmail.com - 11 Dec 2005 02:46 GMT
3Q all.
I have fixed the problem with your suggestions.