File Upload Code Help

LTheobald
LTheobald used Ask the Experts™
on
Hi,

I was wondering if anyone could help me fix the problem in the following code.  In the end this code will hopefully be a bean that will upload a file to the web server, and then send it as an attachment in an email.  But so far all that happens is that it will read the details from the form, print any fields to the screen, and upload the file.

The problem is that the file uploading isn't working correctly.  The file uploaded is different to the original.  If I open them both in Notepad, I can see that some of the character's are replaced by ?, or blocks.

Can somebody tell me how I can avoid this?

Thanks

Lee

-----------------------------------

Here's the code

public void doUpload(HttpServletRequest request) throws IOException
{
     // Retrieve the info from the input stream - file details
     BufferedReader in = request.getReader();
         
     // Use a string to hold what is on each line of the file
     String line = "";
         
     //  Read the first line.  It should be a boundary in the form of something like "------?d2d7"
     //     If it's length is less than 3, something has gone wrong.
     line = in.readLine();
     if (line.length() < 3)
     {
          throw new IOException("Incorrect boundary length.  Please try again");
     }
         
     // Store the length of the boundary (-2 due to carriage return (\r))
     int boundaryLength = line.length() - 2;
     
     // Store the boundary itself
     String boundary = line.substring(0, boundaryLength);
         
     while (line.length() != boundaryLength + 4) // boundaryLength + 4 = end of inputstream
     {
          // Read the content descriptor.
          line = in.readLine();
         
          // If the line only has one semi-colon in it, it's just normal form data
          int semiCount = 0;
          for (int x=0; x<line.length(); x++)
          {
               if (line.charAt(x) == ';')
               {
                    semiCount++;
               }
          }
         
          if (semiCount == 1)
          {
               // Retrieve the name of the form data
               int firstQuote = line.indexOf("\"");
               int lastQuote = line.lastIndexOf("\"");
               String currField = line.substring(firstQuote+1, lastQuote);
                   
               line = in.readLine(); // blank line
               line = in.readLine(); // Start of form data
                   
               String formData = "";
               while (!line.startsWith(boundary))
               {
                    formData += "  "+ line;
                    line = in.readLine(); // Form Data
               }
               // fields.put(currField, line);
               System.out.println(currField +" = "+ formData);
          }
          else
          {
               line = in.readLine(); // Content Type
               line = in.readLine(); // Blank Line
               line = in.readLine(); // Start of file
                   
               PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("test.out")));
                   
               while (!line.startsWith(boundary))
               {
                    // System.out.println(line);  File contents look fine if I print them out here
                    pw.println(line);
                    line = in.readLine();    
               }
               pw.close();
          }
     }
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
Open it it Write (Wordpad) instead. The blocks should go. As for the question marks, this sounds like an encoding problem.

Author

Commented:
Opening it in WordPad doesn't display the block any differently.  And I've been looking at encoding, but I still haven't anything that works.

Anyone got any ideas?
Distinguished Expert 2018
Commented:
 Try this instead:

Writer pw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("test.out"), "UTF8"));

  By doing this you indicate you want a specific encoding. Try to play around with the encoding if the above does not work.

  Bare in mind that I wrote the above by heart so some of the classes might not work correctly. You might ned to chaneg in order to find the proper class hierarchy.
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Distinguished Expert 2018

Commented:
 .. you might also need to make sure that the encoding you send is exactly the same as the one you are writing to the file.
Top Expert 2016

Commented:
>>File contents look fine if I print them out here

Are you absolutely certain about this?
Distinguished Expert 2018

Commented:
 Sorry just looked quickly at the Java API. This:

pw.println(line);

  should be

pw.write(line);

  if you use the Writer.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial