File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes image file upload problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "image file upload problem" Watch "image file upload problem" New topic

image file upload problem

jamie ong

Joined: Nov 05, 2001
Posts: 24
i have written a java swing application to allow me to upload image file to the server. But somehow the file size on the server is smaller than the actual file size.
i'm using fileinputstream, dataoutputstream and byte class to transfer my files.
can anyone has any solution why this problem will occur??
many thanks!
jhun kam

Joined: Dec 05, 2001
Posts: 18
Hmm, did you forget to close() or flush() the output stream ? Cause if you dont, your bytes could still be in your memory buffer when you close the application.
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
Is it much smaller, or only a little smaller? If it is only a little smaller, it could be because you are translating CR/LF sequences to a newline when you read the file for upload, but only storing them as a LF on the server.
Can you give us a bit more detail of how you are reading and writing the data ?

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
jamie ong

Joined: Nov 05, 2001
Posts: 24
Hi Frank,
Here are the transfer part of the coding. The file is smaller ard like 300 to 600 bytes.
DataOutputStream dataoutputstream = new DataOutputStream(theUrlConnection.getOutputStream());
StringBuffer streambuffer = new StringBuffer();
streambuffer.append("Content-Disposition: form-data;name=\"pic1\"; filename=\""+img1+"\"\r\n");
streambuffer.append("Content-Type: multipart/form-data\r\n");
FileInputStream fileinputstream = new FileInputStream(new File(img1));
int filesize=fileinputstream.available();
while (fileinputstream.available() > 0) {
byte[] buffersize = new byte[filesize];
int numberOfBytesRead =;
streambuffer = new StringBuffer();
streambuffer.append("--" + boundary + "\r\n");
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
You have some very... creative... variable names there. Why is a StringBuffer called a streamBuffer? Why is a byte[] called a buffersize? (And why are you instantiating a new byte[] buffer each time through the while loop?) It's very confusing and misleading.
Hmmm... one error I see is actually something that might make the transferred file bigger (by adding junk data), not smaller. Look at the line
You're writing the whole buffer here. It may not be full (with real data, anyway). You need to make use of the numberOfBytesRead() which you read in the previous line.
The other error is probably what's causing the filesize to be too small. You're using available() incorrectly. It can return 0 even though there are more bytes to be read - this only means that the bytes are not available right now; you must wait a little bit (e.g. for the hard drive reader to move to the correct area on the hard disk). See this for more details.
If you have further troubles, I suggest you try transferring a text file rather than an image file, for testing anyway. This way you can look at the transferred file and figure out which parts are missing, and which are added. This can offer more useful clues. Good luck...

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: image file upload problem
It's not a secret anymore!