tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <>
Subject Re: Tomcat 5.5 creates 0 byte files
Date Fri, 02 Jul 2010 20:21:13 GMT
Hash: SHA1


On 7/2/2010 4:43 AM, Murat Birben wrote:
> I have a very simple file upload mechanism in java.

I'll say: it's simply not going to work.

>             File file = new File(strPath + strFileName);
>             fileOutputStream = new FileOutputStream(file);
>             fileInputStream = new FileInputStream(fFile);

Is this a multipart/form-data request? If so, request.getParameter isn't
going to work properly.

>             while ((iBufLen = != -1) {
>                 fileOutputStream.write(bBuf);

bBuf might not be full: use iBufLen to limit the output to write().

>             byte[] tempbBuf = new byte[iReadLen];
>   , 0, iReadLen);
>             fileOutputStream.write(tempbBuf);

tempbBuf might not be full: store the result of and
use it to limit the output when you call write().

Why are you using two different byte buffers?

Might I suggest a much simpler byte-copy routine?

byte[] buffer = new byte[1024];
int count = 0;

while(-1 != (count =
  fileOutputStream.write(buffer, count);


>         } finally {
>             fileOutputStream.close();
>             fileInputStream.close();

No null-check. This code might fail and mask another exception. Always
check for null when null is possible.

>             if (fFile.exists()) {
>                 fFile.delete();
>             }

No failure check. :( You might want to log an error when a file is
"leaked" by your upload servlet.

Have you considered simply re-naming the file that has apparently
already been saved to the disk, instead of byte-copying it yourself, and
then deleting the original?

- -chris
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla -


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message