tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <ch...@christopherschultz.net>
Subject Re: Tomcat 5.5 creates 0 byte files
Date Fri, 02 Jul 2010 20:21:13 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Murat,

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 = fileInputStream.read(bBuf)) != -1) {
>                 fileOutputStream.write(bBuf);

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

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

tempbBuf might not be full: store the result of fileInputStream.read 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 = fileInputStream.read(buffer))
  fileOutputStream.write(buffer, count);

fileOutputStream.flush();
fileOutputStream.close();

>         } 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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkwuSjkACgkQ9CaO5/Lv0PBxtwCeLJj0hwsZ8PLTD94jquewTpaG
+wgAn0ahUAG7c/q95MAzsRS0Z0bUkVU4
=ZV9L
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message