users@jersey.java.net

[Jersey] How to post a file and other form fields by Jersey client

From: Jitesh <jiteshks_at_yahoo.com>
Date: Tue, 21 Jun 2011 22:24:38 -0700 (PDT)

Hi,

I have a form which has a file upload field along with other fields (Text
boxes and List box).
So far,whatever example I have found,deal with only those forms that have
only file upload field in them.


Client code -
[code]
protected void forwardToTranslation(@ModelAttribute("testCommand")
TestCommand testCommand,HttpServletRequest request)
        {
                Client client = Client.create();
                WebResource service =
client.resource("http://localhost:8080/altcso/service/request");
                

                //uploadFile is byte array
                File file = convertToFile(uploadFile) ;

                //Check if right file is being picked.This is ok.
                System.out.println("File Size "+file.exists()+" abs path
"+file.getAbsolutePath()) ;
                
                Form formData = new Form() ;
                
                formData.add("param1",value1) ;
                formData.add("param2",value2) ;
                formData.add("uploadFile", file) ;
                formData.add("param3",value3) ;
                
                ClientResponse response =
service.type(MediaType.TEXT_HTML).header("appKey",appkey).post(ClientResponse.class,formData);
                
                System.out.println("response is "+response.toString()) ;


                
        }
[/code]
serverside code -

[code]


@Transactional
        @RequestMapping(method = RequestMethod.POST, value = "/request")
        @Consumes("multipart/form-data")
        public ModelAndView methodOnPost(@ModelAttribute("clientCommand")
ClientCommand clientCommand,
                        HttpServletRequest request, HttpServletResponse
response,_at_FormParam("uploadFile") InputStream file)
{
//convertToStream reads from InputStream to file.I think this is where it is
getting wrong.
req.setInput(convertStreamToString(file));
//following line prints
uploadFile=uploadFile.html&param1=value1&param2=value2&param3=value3
System.out.println("File ::"+ req.getInput());


}

[/code]

As you can see somehow I am not getting inputStream correctly on server.How
do I parse this so that I get file as well as other parameter values?
Please help.Thanks!!

--
View this message in context: http://jersey.576304.n2.nabble.com/How-to-post-a-file-and-other-form-fields-by-Jersey-client-tp6502947p6502947.html
Sent from the Jersey mailing list archive at Nabble.com.