file image uploaded to a webserver appears garbled after upload.

N

Nishi Bhonsle

Hi:

The images uploaded in a directory on a web server from a client browser, end up garbled. i.e. if the image is uploaded via the UI to some dir say C:\images, after the upload, if i view the image in the destination directory, it looks garbled.
I verified that the initial image in the source location was not garbled.
I checked the file size and the permissions on the image at the destination, and it seems to be the same as the one in the source location.
I also noticed that the garbling does not happen for gif images of size 1K or 2K or 3K but for .jpg images of size > 10 K etc
(I have not found bigger .gif files to test this.)

A part of the java code that handles upload creates an object of class MultipartFormHandler (which parses an incoming file upload post).

This class parses the incoming post and returns MultipartStreamItems for each form item.
If setCharacterEncoding() is called (with anything other than null), then strings returned from this class will be character-set decoded. Otherwise, strings returned from this class are not character-set decoded. In this way, MultipartFormHandler precisely matches the behavior of ServletRequest.


MultipartFormHandler handler = new MultipartFormHandler(request);
handler.setCharacterEncoding("UTF-8");

if (imageFile != null)
{
FileReader reader = new FileReader(file);
FileWriter writer = new FileWriter(imageFile);
int c;
while ((c=reader.read()) >= 0)
{
writer.write(c);
}
reader.close();
writer.close();
}

What can cause the garble?

Thanks, Nishi.
 
P

Paul Lutus

Nishi said:
Hi:

The images uploaded in a directory on a web server from a client browser,
end up garbled.


From the documentation:

"FileReader is meant for reading streams of characters. For reading streams
of raw bytes, consider using a FileInputStream."
 
W

William Brogden

Hi:

The images uploaded in a directory on a web server from a client
browser, end up garbled. i.e. if the image is uploaded via the UI to
some dir say C:\images, after the upload, if i view the image in the
destination directory, it looks garbled.
I verified that the initial image in the source location was not garbled.
I checked the file size and the permissions on the image at the
destination, and it seems to be the same as the one in the source
location.
I also noticed that the garbling does not happen for gif images of size
1K or 2K or 3K but for .jpg images of size > 10 K etc
(I have not found bigger .gif files to test this.)

A part of the java code that handles upload creates an object of class
MultipartFormHandler (which parses an incoming file upload post).

This class parses the incoming post and returns MultipartStreamItems for
each form item.
If setCharacterEncoding() is called (with anything other than null),
then strings returned from this class will be character-set decoded.
Otherwise, strings returned from this class are not character-set
decoded. In this way, MultipartFormHandler precisely matches the
behavior of ServletRequest.


MultipartFormHandler handler = new MultipartFormHandler(request);
handler.setCharacterEncoding("UTF-8");

if (imageFile != null)
{
FileReader reader = new FileReader(file);
FileWriter writer = new FileWriter(imageFile);

****** Bingo - A FileReader tries to turn a byte stream into characters
A FileWriter does a character conversion on writing.

You should be using plain InputStream etc. for manipulating bytes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top