FileWriting Problem using Servlet

  • Thread starter Emery Z. Balint Jr.
  • Start date
E

Emery Z. Balint Jr.

My objective here is to have a place where users could upload an image file
to the server. I'm using a form with encType set to multipart/form-data and
I've read up on how to extract the data that comes through. Easy enough,
except that for some reason when I get to the part of extracting the files,
I get corrupt data.

Below I've tried using PrintWriter (which works the best so far)
FileOutputStreams and even nio with a ByteBuffer. Anything that writes
binary stream hasn't worked at all, the file sizes that occur are several
times larger than the original, hence why I stuck with PrintWriter. The test
file written with this code below ends up having exactly the same file size
as the original, but again with corrupt data. I'm a little lost at this
point, as according to most documents I found on the subject I should be
doing this correctly. Many thanks in advance for any assistance!

Emery.
/\^/\^/\
Sun Certified Java Programmer
www.websamba.com/javarobotics/
E-stronomy - Astronomical Resources
www.websamba.com/e-stronomy/
Talking with Tonz - Dance/Alternative Music
www.soundclick.com/talkingwithtonz/


File file1 = new File("tester.png");
file1.createNewFile();
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file1)));

while (lineSize!=-1 && !nextLine.startsWith(delimiter)) {
lineSize = in.readLine(inLine, 0, 1024);
if ((lineSize==delimiter.length()+4)
&& (new String(inLine, 0, lineSize).startsWith(delimiter))) {

nextLine = nextLine.substring(0, nextLine.length()-2);
pw.print(nextLine);

} else {
pw.print(nextLine);

}
nextLine = new String(inLine, 0, lineSize);
}

pw.close();
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top