Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes Uploading a file to the server(oreilly's MultiPartRequest class) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Uploading a file to the server(oreilly Watch "Uploading a file to the server(oreilly New topic
Author

Uploading a file to the server(oreilly's MultiPartRequest class)

Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi,
I have been trying to write a servlet which will upload a file to the server.For that i used oreilly's MultiPartRequest class. Iam sending data through http header from swing interface like that:-
url = new URL ("http://127.0.0.1:7001/FileUpload?x="+str1);


urlConn = url.openConnection();

urlConn.setDoInput (true);

urlConn.setDoOutput (true);


urlConn.setUseCaches (false);


urlConn.setRequestProperty("Content-Type","multipart/form-data;boundary=-----------------------------7d01ecf406a6");

printout = new DataOutputStream (urlConn.getOutputStream ());

String content="-----------------------------7d01ecf406a6\r\n"+"Content-Disposition: form-data;"+"name=\"upload\"; filename=\""+aa+"\"\r\n"+"Content-Type: text/plain\r\n\r\n\r\n"+conffile+"-----------------------------7d01ecf406a6--\r\n";
printout.writeBytes(content);
printout.flush ();
printout.close ();
But i get the following exception:
----------------------------------
java.io.IOException: Corrupt form data: no leading boundary
at com.oreilly.servlet.MultipartRequest.readRequest(MultipartRequest.java:361)
at com.oreilly.servlet.MultipartRequest.<init>(MultipartRequest.java:149)
at FileUpload.doPost(FileUpload.java:23)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:213)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:1265)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:1622)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:137)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:120)

Plz guide me where i done a mistake.
Regards
Bikash

k swamy
Greenhorn

Joined: Jul 31, 2001
Posts: 14
Hi
Add cos.jar file in ur classpath and then check..Also mention on what server u r running and the web application details if it doesnt work after this.
Swamy
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

This is not a classpath issue. Look at the stacktrace... cos is clearly in the classpath:
On the other hand, the top of the trace tells us there is something wrong with your form encoding:I'm not in the practice of making my own HTTP submission by hand, but isn't there a double newline between the header and the body of the request?

Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I've been thinking more about this, and I decided to have fun at work today.

So the packet-sniffer got dusted off.

Here is the result of POSTing data through a multi-part form submission:
And here is the response I get back (ie: the servlet forwards to uiPreview.jsp)
It will blow up the webpage, but I've preserved all the linefeeds and returns. I've added a single space where necessary so UBB preserves the double cr-lf, so don't just copy and paste.

One thing I note is that there are spaces between the elements of each part's header.

So when you concat Strings together (which most times, you don't need to be doing anyways...), you are not leaving a space. Look specifically after "form-data" below... (note, i *did* break the following lines after the "Disposition:" as this seemed a good enough place to have UBB wrap the line)
Should be:Note the additions and deletions of '\r\n' pairs, and also note the removal of unnecessary string concatenations ("+")
[This message has been edited by Mike Curwen (edited December 05, 2001).]
Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi Mike,
Thanks for ur guide.I am now facing with another problem my uploaded Image file and zip file not open(But .txt,.html,.xml etc. files r ok).Giving error format damaged/parsing error.I changed in below codes:-
String content="-----------------------------7d01ecf406a6\r\nContent-Disposition: form-data; name=\"upload\"; filename=\""+aa+"\"\r\nContent-Type: application/octet-stream\r\n\r\n" + conffile + "\r\n-----------------------------7d01ecf406a6--\r\n";
I alos tried with Content-Type:image/gif.But couldn't slove the problem.Plz guide me what should I do.
Regards
Bikash
Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi Mike,
Thanks for ur guide.Noe I am facing with another problem.My uploaded image and .zip file not open{but .txt,.html,.xml,.xsl files r ok).Giving error format damaged/parsing error.I changed in below codes:-
String content="-----------------------------7d01ecf406a6\r\nContent-Disposition: form-data; name=\"upload\"; filename=\""+aa+"\"\r\nContent-Type: application/octet-stream\r\n\r\n" + conffile + "\r\n-----------------------------7d01ecf406a6--\r\n";
I also tried with Content-Type:image/gif or image/jpg.But couldn't slove the problem.plz guide how i can slove this problem.
Best regards
Bikash
------------------
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Uploading a file to the server(oreilly's MultiPartRequest class)