-----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: [email protected]
For additional commands, e-mail: [email protected]