File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

image file upload problem

 
jamie ong
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 6920
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
jamie ong
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
streambuffer.append("\r\n");
dataoutputstream.write(streambuffer.toString().getBytes());
FileInputStream fileinputstream = new FileInputStream(new File(img1));
int filesize=fileinputstream.available();
while (fileinputstream.available() > 0) {
byte[] buffersize = new byte[filesize];
int numberOfBytesRead = fileinputstream.read(buffersize);
dataoutputstream.write(buffersize);
}
streambuffer = new StringBuffer();
streambuffer.append("--" + boundary + "\r\n");
dataoutputstream.write(streambuffer.toString().getBytes());
dataoutputstream.flush();
dataoutputstream.close();
Thanks!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
dataoutputstream.write(buffersize);
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 agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic