FileWriting Problem using Servlet

Discussion in 'Java' started by Emery Z. Balint Jr., Aug 20, 2003.

  1. 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();
    Emery Z. Balint Jr., Aug 20, 2003
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish
    Replies:
    4
    Views:
    2,407
    Andy Fish
    Dec 17, 2003
  2. Sean Clarke
    Replies:
    1
    Views:
    1,881
    Sudsy
    Jan 7, 2004
  3. circuit_breaker
    Replies:
    2
    Views:
    1,989
    Jack Jia
    Apr 4, 2004
  4. chamikara
    Replies:
    1
    Views:
    49,834
  5. DiscoStu
    Replies:
    0
    Views:
    495
    DiscoStu
    Jul 8, 2004
Loading...

Share This Page