users@jersey.java.net

[Jersey] Re: Test Client for Multipart Jersy Rest Service

From: Michal Gajdos <michal.gajdos_at_oracle.com>
Date: Mon, 27 Aug 2012 12:16:53 +0200

Hi,

FormDataMultiPart#field you're calling does not set the fileName
property of the FormDataContentDisposition instance. You can change your
client side code (creating FormDataMultiPart/FormDataBodyPart objects)
to something like:

FormDataMultiPart part = new FormDataMultiPart();
FormDataBodyPart p = new
FormDataBodyPart(FormDataContentDisposition.name("file").fileName("file").build(),
finStream, MediaType.TEXT_HTML_TYPE);
part.bodyPart(p);

and it should work.

Also you may want to change

file = new File("xx" + contentDisposition.getFileName());
new File("xxx").mkdirs();
file.createNewFile();

to have a correct path to the 'xxx' directory in the first statement:
file = new File("xxx/" + contentDisposition.getFileName());

regards,
Michal

On 24.8.2012 19:16, selvakumar netaji wrote:
> Hi,
>
> Even after changing the url its not working. I just want to know to
> write clients for multipart rest services.
>
> On Thu, Aug 23, 2012 at 10:20 PM, Marek Potociar
> <marek.potociar_at_oracle.com <mailto:marek.potociar_at_oracle.com>> wrote:
>
> From what I see the @Path value on your resource does not match
> the URI in your client. Might that be the cause of your issue?
>
> Marek
>
> On Aug 23, 2012, at 7:06 AM, selvakumar netaji
> <vvekselva.gm_at_gmail.com <mailto:vvekselva.gm_at_gmail.com>> wrote:
>
>> Hi,
>>
>>
>> I've created a new multipart part rest service and its working
>> fine with the html form. The rest service is
>>
>> @POST
>> @Path("/Upload")
>> @Produces(MediaType.TEXT_HTML)
>> @Consumes(MediaType.MULTIPART_FORM_DATA)
>> public String uploadFile(@FormDataParam("file") InputStream
>> inputStream,
>> @FormDataParam("file") FormDataContentDisposition
>> contentDisposition) {
>>
>> System.out.println("Method Entry");
>> System.out.println(contentDisposition.getFileName());
>>
>>
>> String result = "not Success";
>> File file = null;
>> if (contentDisposition != null
>> && contentDisposition.getFileName() != null
>> && contentDisposition.getFileName().trim().length() > 0) {
>> try {
>> file = new File("xx"
>> + contentDisposition.getFileName());
>> new File("xxx").mkdirs();
>> file.createNewFile();
>> OutputStream outputStream = new
>> FileOutputStream(file);
>> int read = 0;
>> byte[] bytes = new byte[1024];
>>
>> while ((read = inputStream.read(bytes)) != -1) {
>> outputStream.write(bytes, 0, read);
>> }
>> outputStream.flush();
>> outputStream.close();
>> result = "success";
>>
>> } catch (Exception e) {
>> System.out.println(e.toString());
>> }
>> }
>> System.out.println("Method Exit");
>> return result;
>>
>> }
>>
>> but the form the client code I'm not able to access the client
>>
>>
>> FileInputStream finStream = new FileInputStream(new File(
>> Error.txt"));
>>
>> FormDataMultiPart part = new
>> FormDataMultiPart().field("file",
>> finStream, MediaType.TEXT_HTML_TYPE);
>>
>> WebResource resource = Client.create().resource(
>> "http://localhost:8080/Multipart/Upload
>> <http://localhost:8080/Multipart/rest>");
>> String response =
>> resource.type(MediaType.MULTIPART_FORM_DATA_TYPE)
>> .post(String.class, part);
>> System.out.println(response);
>>
>>
>>
>> Did I've made any mistakes.
>
>