• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
------------------
 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic