You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by bhadu <ra...@rediffmail.com> on 2007/09/17 09:16:37 UTC

Uploading a File using apache commons Http clien

Hi all,

I have to web application using servlets.
My First web application's servlet is calling servlet of another application
using HTTP client connection.
 Calling servlet is sending file as parameter to second servlet.

My problem is that in my second servlet there is nothing in request
parameter.

Look at my code...............First servlet...........


           String urlString = "http://localhost:8787//testone//MyServlet";
		
	   		
			HttpClient client = new HttpClient();
	        PostMethod postMethod = new PostMethod(urlString);
	        post(postMethod);
	      
	        
	        File f = new File("C:\\test.txt");
	        System.out.println("File Length = " + f.length());
	 
	        postMethod.setRequestEntity(new   
FileRequestEntity(f,"multipart/form-data;boundary=7d41e5b3904fc"));
	        postMethod.addRequestHeader("Content-length",""+f.length());
	      
	        client.getParams().setParameter("http.useragent", "My Browser");
	        postMethod.addParameter("test","testvalue");
	        NameValuePair[] nm = {new NameValuePair("test","test")};
	        postMethod.setRequestBody(nm);
	        int statusCode1 = client.executeMethod(postMethod);
	        System.out.println(" status >>> "+statusCode1);
	        System.out.println("statusLine>>>" + postMethod.getStatusLine());
	        postMethod.releaseConnection();
	        

	}

	private void post(PostMethod postMethod) throws FileNotFoundException
	{
		 File f = new File("C:\\error.txt");
		 System.out.println("file exit = "+f.exists());
		Part[] parts = {
                new FilePart(f.getName(), f)
            };
	
postMethod.addRequestHeader("Content-type","multipart/form-data;boundary="+f.length());
		postMethod.addRequestHeader("content-disposition","form-data;
name="+f.getName());
		postMethod.setRequestEntity(
                new MultipartRequestEntity(parts, postMethod.getParams())
                );
	}

  And This is my Second servlet.......................



protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws IOException 
	{
		
		 String path = System.getenv("Target_PATH");	
		try
	    {
		  DiskFileItemFactory df = new DiskFileItemFactory();
	      PrintWriter out = response.getWriter();
	      
	        ServletFileUpload fu = new ServletFileUpload(df);
	        // If file size exceeds, a FileUploadException will be thrown
	        fu.setSizeMax(1000000);
	        
	        List fileItems = fu.parseRequest(request);
	        Iterator itr = fileItems.iterator();
	        
	        while(itr.hasNext()) {
	          FileItem fi = (FileItem)itr.next();
	         
	          createFile(path,"signedresources.txt", fi.get());
	          
	        }
	        
	        
	       
	        out.println("SUCCESS123456: ");
	      out.close();
	    }
	    catch(Exception e)
	    {
	      e.printStackTrace();
	    }
	  
		
 		
	}
	
     
	
	
    private  void createFile(String destinationPath, String fileName, byte[] 
content) throws IOException
    {     
        
        OutputStream outputStream = new FileOutputStream(destinationPath);
        outputStream.write(content);
        outputStream.flush();
        outputStream.close();
    }


problem is here....... List fileItems = fu.parseRequest(request); list is
empty.

So anybody can tell me what is wrong I m doing?

I am using Tomcat 5.0.

Thanx In advance

bhadu

-- 
View this message in context: http://www.nabble.com/Uploading-a-File-using-apache-commons-Http-clien-tf4464865.html#a12730645
Sent from the Commons - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: Uploading a File using apache commons Http clien

Posted by James Carman <ja...@carmanconsulting.com>.
Here's the example application which uploads a file...

http://svn.apache.org/viewvc/jakarta/httpcomponents/oac.hc3x/trunk/src/examples/MultipartFileUploadApp.java?view=markup

You can disregard the Swing stuff in there and concentrate on the
action listener's guts.  Or, just run it like it is and post a file to
your servlet with it.

On 9/18/07, bhadu <ra...@rediffmail.com> wrote:
>
>
> Hi James,
>
>  Thanx for your help.
>  I have removed  following code....
>    client.getParams().setParameter("http.useragent", "My Browser");
> >                 postMethod.addParameter("test","testvalue");
> >                 NameValuePair[] nm = {new NameValuePair("test","test")};
> >                 postMethod.setRequestBody(nm);
>
>
> But still it's not working.
>
> I have not tried simple java client,Please send me some link for that.
>
> Thanx
> Rajesh
>
>
> James Carman wrote:
> >
> > Have you tried posting a file from a simple Java client to that second
> > servlet first?  I'd do that to take out a variable and make sure your
> > logic is right.  I would venture a guess that this code wouldn't work
> > in isolation either, though.
> >
> > It looks like you're trying to mix a multipart post with a regular
> > post.  I believe you need to add mutiple "parts" to that
> > MultipartRequestEntity.  You need to use a StringPart to post regular
> > parameter values (your "test" parameter).
> >
> > On 9/17/07, bhadu <ra...@rediffmail.com> wrote:
> >>
> >> Hi all,
> >>  sorry for repost b'coz my first post is consists with error...
> >>
> >> this is right post......
> >>
> >>
> >>
> >> I have to web application using servlets.
> >> My First web application's servlet is calling servlet of another
> >> application
> >> using HTTP client connection.
> >>  Calling servlet is sending file as parameter to second servlet.
> >>
> >> My problem is that in my second servlet there is nothing in request
> >> parameter.
> >>
> >> Look at my code...............First servlet...........
> >>
> >>
> >>            String urlString =
> >> "http://localhost:8787//testone//MyServlet";
> >>
> >>
> >>                         HttpClient client = new HttpClient();
> >>                 PostMethod postMethod = new PostMethod(urlString);
> >>                 post(postMethod);
> >>
> >>
> >>
> >>                 client.getParams().setParameter("http.useragent", "My
> >> Browser");
> >>                 postMethod.addParameter("test","testvalue");
> >>                 NameValuePair[] nm = {new NameValuePair("test","test")};
> >>                 postMethod.setRequestBody(nm);
> >>                 int statusCode1 = client.executeMethod(postMethod);
> >>                 System.out.println(" status >>> "+statusCode1);
> >>                 System.out.println("statusLine>>>" +
> >> postMethod.getStatusLine());
> >>                 postMethod.releaseConnection();
> >>
> >>
> >>         }
> >>
> >>         private void post(PostMethod postMethod) throws
> >> FileNotFoundException
> >>         {
> >>                  File f = new File("C:\\error.txt");
> >>                  System.out.println("file exit = "+f.exists());
> >>                 Part[] parts = {
> >>                 new FilePart(f.getName(), f)
> >>             };
> >>
> >> postMethod.addRequestHeader("Content-type","multipart/form-data;boundary="+f.length());
> >>
> >> postMethod.addRequestHeader("content-disposition","form-data;
> >> name="+f.getName());
> >>                 postMethod.setRequestEntity(
> >>                 new MultipartRequestEntity(parts, postMethod.getParams())
> >>                 );
> >>         }
> >>
> >>   And This is my Second servlet.......................
> >>
> >>
> >>
> >> protected void doPost(HttpServletRequest request, HttpServletResponse
> >> response) throws IOException
> >>         {
> >>
> >>                  String path = System.getenv("Target_PATH");
> >>                 try
> >>             {
> >>                   DiskFileItemFactory df = new DiskFileItemFactory();
> >>               PrintWriter out = response.getWriter();
> >>
> >>                 ServletFileUpload fu = new ServletFileUpload(df);
> >>                 // If file size exceeds, a FileUploadException will be
> >> thrown
> >>                 fu.setSizeMax(1000000);
> >>
> >>                 List fileItems = fu.parseRequest(request);
> >>                 Iterator itr = fileItems.iterator();
> >>
> >>                 while(itr.hasNext()) {
> >>                   FileItem fi = (FileItem)itr.next();
> >>
> >>                   createFile(path,"signedresources.txt", fi.get());
> >>
> >>                 }
> >>
> >>
> >>
> >>                 out.println("SUCCESS123456: ");
> >>               out.close();
> >>             }
> >>             catch(Exception e)
> >>             {
> >>               e.printStackTrace();
> >>             }
> >>
> >>
> >>
> >>         }
> >>
> >>
> >>
> >>
> >>     private  void createFile(String destinationPath, String fileName,
> >> byte[]
> >> content) throws IOException
> >>     {
> >>
> >>         OutputStream outputStream = new
> >> FileOutputStream(destinationPath);
> >>         outputStream.write(content);
> >>         outputStream.flush();
> >>         outputStream.close();
> >>     }
> >>
> >>
> >> problem is here....... List fileItems = fu.parseRequest(request); list is
> >> empty.
> >>
> >> So anybody can tell me what is wrong I m doing?
> >>
> >> I am using Tomcat 5.0.
> >>
> >> Thanx In advance
> >>
> >> bhadu
> >>
> >> --
> >> View this message in context:
> >> http://www.nabble.com/Uploading-a-File-using-apache-commons-Http-clien-tf4464865.html#a12730645
> >> Sent from the Commons - User mailing list archive at Nabble.com.
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> >> For additional commands, e-mail: user-help@commons.apache.org
> >>
> >>
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> > For additional commands, e-mail: user-help@commons.apache.org
> >
> >
> >
>
> --
> View this message in context: http://www.nabble.com/Uploading-a-File-using-apache-commons-Http-clien-tf4464865.html#a12750007
> Sent from the Commons - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: Uploading a File using apache commons Http clien

Posted by bhadu <ra...@rediffmail.com>.

Hi James,
 
 Thanx for your help.
 I have removed  following code....
   client.getParams().setParameter("http.useragent", "My Browser");
>                 postMethod.addParameter("test","testvalue");
>                 NameValuePair[] nm = {new NameValuePair("test","test")};
>                 postMethod.setRequestBody(nm);


But still it's not working.

I have not tried simple java client,Please send me some link for that.

Thanx
Rajesh


James Carman wrote:
> 
> Have you tried posting a file from a simple Java client to that second
> servlet first?  I'd do that to take out a variable and make sure your
> logic is right.  I would venture a guess that this code wouldn't work
> in isolation either, though.
> 
> It looks like you're trying to mix a multipart post with a regular
> post.  I believe you need to add mutiple "parts" to that
> MultipartRequestEntity.  You need to use a StringPart to post regular
> parameter values (your "test" parameter).
> 
> On 9/17/07, bhadu <ra...@rediffmail.com> wrote:
>>
>> Hi all,
>>  sorry for repost b'coz my first post is consists with error...
>>
>> this is right post......
>>
>>
>>
>> I have to web application using servlets.
>> My First web application's servlet is calling servlet of another
>> application
>> using HTTP client connection.
>>  Calling servlet is sending file as parameter to second servlet.
>>
>> My problem is that in my second servlet there is nothing in request
>> parameter.
>>
>> Look at my code...............First servlet...........
>>
>>
>>            String urlString =
>> "http://localhost:8787//testone//MyServlet";
>>
>>
>>                         HttpClient client = new HttpClient();
>>                 PostMethod postMethod = new PostMethod(urlString);
>>                 post(postMethod);
>>
>>
>>
>>                 client.getParams().setParameter("http.useragent", "My
>> Browser");
>>                 postMethod.addParameter("test","testvalue");
>>                 NameValuePair[] nm = {new NameValuePair("test","test")};
>>                 postMethod.setRequestBody(nm);
>>                 int statusCode1 = client.executeMethod(postMethod);
>>                 System.out.println(" status >>> "+statusCode1);
>>                 System.out.println("statusLine>>>" +
>> postMethod.getStatusLine());
>>                 postMethod.releaseConnection();
>>
>>
>>         }
>>
>>         private void post(PostMethod postMethod) throws
>> FileNotFoundException
>>         {
>>                  File f = new File("C:\\error.txt");
>>                  System.out.println("file exit = "+f.exists());
>>                 Part[] parts = {
>>                 new FilePart(f.getName(), f)
>>             };
>>
>> postMethod.addRequestHeader("Content-type","multipart/form-data;boundary="+f.length());
>>                
>> postMethod.addRequestHeader("content-disposition","form-data;
>> name="+f.getName());
>>                 postMethod.setRequestEntity(
>>                 new MultipartRequestEntity(parts, postMethod.getParams())
>>                 );
>>         }
>>
>>   And This is my Second servlet.......................
>>
>>
>>
>> protected void doPost(HttpServletRequest request, HttpServletResponse
>> response) throws IOException
>>         {
>>
>>                  String path = System.getenv("Target_PATH");
>>                 try
>>             {
>>                   DiskFileItemFactory df = new DiskFileItemFactory();
>>               PrintWriter out = response.getWriter();
>>
>>                 ServletFileUpload fu = new ServletFileUpload(df);
>>                 // If file size exceeds, a FileUploadException will be
>> thrown
>>                 fu.setSizeMax(1000000);
>>
>>                 List fileItems = fu.parseRequest(request);
>>                 Iterator itr = fileItems.iterator();
>>
>>                 while(itr.hasNext()) {
>>                   FileItem fi = (FileItem)itr.next();
>>
>>                   createFile(path,"signedresources.txt", fi.get());
>>
>>                 }
>>
>>
>>
>>                 out.println("SUCCESS123456: ");
>>               out.close();
>>             }
>>             catch(Exception e)
>>             {
>>               e.printStackTrace();
>>             }
>>
>>
>>
>>         }
>>
>>
>>
>>
>>     private  void createFile(String destinationPath, String fileName,
>> byte[]
>> content) throws IOException
>>     {
>>
>>         OutputStream outputStream = new
>> FileOutputStream(destinationPath);
>>         outputStream.write(content);
>>         outputStream.flush();
>>         outputStream.close();
>>     }
>>
>>
>> problem is here....... List fileItems = fu.parseRequest(request); list is
>> empty.
>>
>> So anybody can tell me what is wrong I m doing?
>>
>> I am using Tomcat 5.0.
>>
>> Thanx In advance
>>
>> bhadu
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Uploading-a-File-using-apache-commons-Http-clien-tf4464865.html#a12730645
>> Sent from the Commons - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Uploading-a-File-using-apache-commons-Http-clien-tf4464865.html#a12750007
Sent from the Commons - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: Uploading a File using apache commons Http clien

Posted by James Carman <ja...@carmanconsulting.com>.
Have you tried posting a file from a simple Java client to that second
servlet first?  I'd do that to take out a variable and make sure your
logic is right.  I would venture a guess that this code wouldn't work
in isolation either, though.

It looks like you're trying to mix a multipart post with a regular
post.  I believe you need to add mutiple "parts" to that
MultipartRequestEntity.  You need to use a StringPart to post regular
parameter values (your "test" parameter).

On 9/17/07, bhadu <ra...@rediffmail.com> wrote:
>
> Hi all,
>  sorry for repost b'coz my first post is consists with error...
>
> this is right post......
>
>
>
> I have to web application using servlets.
> My First web application's servlet is calling servlet of another application
> using HTTP client connection.
>  Calling servlet is sending file as parameter to second servlet.
>
> My problem is that in my second servlet there is nothing in request
> parameter.
>
> Look at my code...............First servlet...........
>
>
>            String urlString = "http://localhost:8787//testone//MyServlet";
>
>
>                         HttpClient client = new HttpClient();
>                 PostMethod postMethod = new PostMethod(urlString);
>                 post(postMethod);
>
>
>
>                 client.getParams().setParameter("http.useragent", "My Browser");
>                 postMethod.addParameter("test","testvalue");
>                 NameValuePair[] nm = {new NameValuePair("test","test")};
>                 postMethod.setRequestBody(nm);
>                 int statusCode1 = client.executeMethod(postMethod);
>                 System.out.println(" status >>> "+statusCode1);
>                 System.out.println("statusLine>>>" + postMethod.getStatusLine());
>                 postMethod.releaseConnection();
>
>
>         }
>
>         private void post(PostMethod postMethod) throws FileNotFoundException
>         {
>                  File f = new File("C:\\error.txt");
>                  System.out.println("file exit = "+f.exists());
>                 Part[] parts = {
>                 new FilePart(f.getName(), f)
>             };
>
> postMethod.addRequestHeader("Content-type","multipart/form-data;boundary="+f.length());
>                 postMethod.addRequestHeader("content-disposition","form-data;
> name="+f.getName());
>                 postMethod.setRequestEntity(
>                 new MultipartRequestEntity(parts, postMethod.getParams())
>                 );
>         }
>
>   And This is my Second servlet.......................
>
>
>
> protected void doPost(HttpServletRequest request, HttpServletResponse
> response) throws IOException
>         {
>
>                  String path = System.getenv("Target_PATH");
>                 try
>             {
>                   DiskFileItemFactory df = new DiskFileItemFactory();
>               PrintWriter out = response.getWriter();
>
>                 ServletFileUpload fu = new ServletFileUpload(df);
>                 // If file size exceeds, a FileUploadException will be thrown
>                 fu.setSizeMax(1000000);
>
>                 List fileItems = fu.parseRequest(request);
>                 Iterator itr = fileItems.iterator();
>
>                 while(itr.hasNext()) {
>                   FileItem fi = (FileItem)itr.next();
>
>                   createFile(path,"signedresources.txt", fi.get());
>
>                 }
>
>
>
>                 out.println("SUCCESS123456: ");
>               out.close();
>             }
>             catch(Exception e)
>             {
>               e.printStackTrace();
>             }
>
>
>
>         }
>
>
>
>
>     private  void createFile(String destinationPath, String fileName, byte[]
> content) throws IOException
>     {
>
>         OutputStream outputStream = new FileOutputStream(destinationPath);
>         outputStream.write(content);
>         outputStream.flush();
>         outputStream.close();
>     }
>
>
> problem is here....... List fileItems = fu.parseRequest(request); list is
> empty.
>
> So anybody can tell me what is wrong I m doing?
>
> I am using Tomcat 5.0.
>
> Thanx In advance
>
> bhadu
>
> --
> View this message in context: http://www.nabble.com/Uploading-a-File-using-apache-commons-Http-clien-tf4464865.html#a12730645
> Sent from the Commons - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org