Wrox Home  
Search P2P Archive for: Go

  Return to Index  

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


Message #1 by bikashpaul_2001@y... on Wed, 5 Dec 2001 10:35:28
The FAQ page at www.servlets.com lists a question which sounds like your problem. You might 
want to check it out at http://www.servlets.com/cos/faq.html. 

The exception tells you the problem (more or less):

> java.io.IOException: Corrupt form data: no leading boundary
> at com.oreilly.servlet.MultipartRequest.readRequest
> (MultipartRequest.java:361)

When the serlvet tried to read the data, the MultipartRequest object was unable to find the 
part boundary. So, whatever your client code is doing to set the boundary is not correct.

Here are some parts of your code that don't look quite right:

> 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";

When you construct the String above, the content type in the String is not the same type as 
what you set with the call to setRequestProperty(). I don't know if that makes a difference, but 
it might.


> 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:-
> 
> Button.addActionListener(new ActionListener()
> 			 {
>                            public void actionPerformed(ActionEvent e)
> 				 {
> int returnVal = fc.showOpenDialog(ActionDemo4.this);
> 
>                 		      if (returnVal == 
> JFileChooser.APPROVE_OPTION) {
>                     		      File file = fc.getSelectedFile();
>                                       String aa=file.getAbsolutePath();
> 		                      textArea3.append(aa);
> 			              textArea2.append("Local URL:");
>                                       long l=file.length();
> 		try
> 		   {
> 		   byte buff[]=new byte[(int)file.length()];
> 		   InputStream fileIn=new FileInputStream(aa);
> 		   int i=fileIn.read(buff);
> 		   String conffile=new String(buff); 
> 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
> 

  Return to Index