Any code that creates a Stream is responsible for closing it cleanly under all circumstances. The following example, the developer unwittingly creates a new input stream but never closes it.
try
{
Uploader uploader = new FtpUploader(server,username,password);
uploader.uploadFile(file.getInputStream(), file.getName());
}
catch (Exception e)
{
log.error(e);
}
The above code does not close the input stream, even though it created it.
It causes the following error:
WARN org.springframework.web.multipart.cos.CosMultipartResolver Could not delete multipart file 'file' with original filename: tenseconds.avi, stored at C:\tomcat\temp\tenseconds.avi
The appropriate way to close the stream would be to keep a reference to it and close it in the finally block.
InputStream in = null;
try
{
Uploader uploader = new FtpUploader(server,username,password);
in = file.getInputStream();
uploader.uploadFile(in, file.getName());
}
catch (IOException e)
{
log.error(e);
}
finally
{
// this code always gets executed.
try {if (in != null) in.close();}
catch(IOException e){log.error(e);}
}