Hello Folks, I am not sure I should be posting here as this is a problem using a jakarta api, but the error coming to me is entirely from java sdk and I feel more comfortable here so:
I am writing a proxy class for an ftp server that my client access, this proxy class must download files and upload files to a third party company, I am using apache commons-net for the ftp access, the download code goes this way:
public void download(File localDir, FileNameValidationRule[] rules) throws IOException, FenasegFTPException {
FTPClient client = new FTPClient();
try {
client.connect(host);
client.login(user, password);
int reply = client.getReplyCode();
if( !FTPReply.isPositiveCompletion( reply ) ) {
client.disconnect();
throw new FenasegFTPException( "Servidor " + host + " recusou a conex�o." );
}
client.changeWorkingDirectory(Congenere.CODIGO_MARITIMA_FENASEG);
List files = getFilesList( client, rules );
BufferedInputStream inputStream = null;
BufferedOutputStream outputStream = null;
for (Iterator iter = files.iterator(); iter.hasNext();) {
String nome = (String) iter.next();
if( FileUtil.isBinary(nome) ) {
client.setFileType(FTP.BINARY_FILE_TYPE);
} else {
client.setFileType(FTP.ASCII_FILE_TYPE);
}
try {
inputStream = new BufferedInputStream( client.retrieveFileStream(nome) );
File localFile = new File(localDir, nome);
System.out.println(localFile.getAbsolutePath());
outputStream = new BufferedOutputStream( new FileOutputStream(localFile ) );
for( int read = inputStream.read(); read != -1; read = inputStream.read() ) {
outputStream.write(read);
}
outputStream.flush();
} finally {
if( outputStream != null ) {
outputStream.close();
}
if( inputStream != null ) {
inputStream.close();
}
}
}
} finally {
if(client != null && client.isConnected() ) {
client.disconnect();
}
}
}
The problem, when I test the download I get the following exception:
java.io.IOException: Stream closed
at java.io.BufferedInputStream.ensureOpen(BufferedInputStream.java:120)
at java.io.BufferedInputStream.read(BufferedInputStream.java:199)
at maritima.central.bonus.FenasegFTPServerProxy.download(FenasegFTPServerProxy.java:162)
at maritima.central.bonus.FenasegFTPServerTest.testDownloadSemFiltro(FenasegFTPServerTest.java:33)
On debugging the code what happens is, there are 2 files on the directory from where I am downloading things, the file list comes ok, the first file downloads ok, but when the code enters the download of the second file, right at the begining of the copy from the input to the output it fires the exception.
Anyone have any idea on what may be causing it?
Thanks in advance,
Notivago.