You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by kaushik <ko...@gmail.com> on 2011/10/12 21:22:43 UTC

cxf Image upload - multipart

Hey Guys,

I am trying to upload an image using REST api. 

Below is my code:
@POST
@Consumes("multipart/mixed")
@Path("/image")
Response uploadImage(MultipartBody multipartBody) throws Exception;

Implementation:
public Response uploadImage(final MultipartBody multipartBody) throws
Exception {

System.out.println("Image upload" + multipartBody.getType());

final List<Attachment> attachments = multipartBody.getAllAttachments();

for (final Attachment attachment : attachments) {
System.out.println("Content type: " + attachment.getContentType());
System.out.println("Content disposition: " +
attachment.getContentDisposition());
final DataHandler dataHandler = attachment.getDataHandler();
final InputStream is = dataHandler.getInputStream();

// Get buffered image
final BufferedImage bi = ImageIO.read(is);
..
validate for correct type of file etc...
}


Testing:
WebClient client = WebClient.create("http://image");
client.type("multipart/mixed");
client.accept("application/xml");

ContentDisposition cd = new
ContentDisposition("attachment;filename=image.jpg");
Attachment att = new Attachment("root", imageInputStream, cd);
client.post(new MultipartBody(att));

final String imageInputStream = "image/gif";
final InputStream is = this.getClass().getResourceAsStream(
                "icon_test.gif");
final Attachment att = new Attachment("root", imageInputStream, is);

 //final File imageFile = new File("C:\\test\\icon_test.gif");

final Response response = client.post(new MultipartBody(att));



This works fine and my unit test is getting passed. But when i try to do it
with an RESTClient like poster or any other tool. I am getting
webapplication exception

Few other questions:
1. Is there any way we could restrict the file type for the upload?
2. I am trying to find out the info about uploaded file like name, extension
(.gif, jpg), mimetype etc. 

Ex: i am trying  dataHnadler.getName() which is null everytime

3. Can some one explain me why do we need "imageInputStream" i.e. image/gif
for attachment , is it specify the file type for individual attachment?  In
my case, it should only accept only one image (or can be multiple images) . 

4. Is there any way to validate the uploaded file?


Any help is appreciated..

Thank you



--
View this message in context: http://cxf.547215.n5.nabble.com/cxf-Image-upload-multipart-tp4896631p4896631.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: cxf Image upload - multipart

Posted by kaushik <ko...@gmail.com>.
Okay..Thank you1 I will check and let you know.

Also, can you please let me know, if these are possible with the headers and
stuff?
1. Is there any way we could restrict the file type for the upload? 
2. I am trying to find out the info about uploaded file like name, extension
(.gif, jpg), mimetype etc. 

Ex: i am trying  dataHnadler.getName() which is null everytime 

3. Can some one explain me why do we need "imageInputStream" i.e. image/gif
for attachment , is it specify the file type for individual attachment?  In
my case, it should only accept only one image (or can be multiple images) . 

4. Is there any way to validate the uploaded file? 

thank you

--
View this message in context: http://cxf.547215.n5.nabble.com/cxf-Image-upload-multipart-tp4896631p4897111.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: cxf Image upload - multipart

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi

Please see starting from here:

http://cxf.547215.n5.nabble.com/JAXRS-Multipart-Form-Data-requiring-Content-ID-td4616470.html#a4623596

I think it's a RESTClient bug. Does posting with wget or curl work ?

Cheers, Sergey

On 12/10/11 20:22, kaushik wrote:
> Hey Guys,
>
> I am trying to upload an image using REST api.
>
> Below is my code:
> @POST
> @Consumes("multipart/mixed")
> @Path("/image")
> Response uploadImage(MultipartBody multipartBody) throws Exception;
>
> Implementation:
> public Response uploadImage(final MultipartBody multipartBody) throws
> Exception {
>
> System.out.println("Image upload" + multipartBody.getType());
>
> final List<Attachment>  attachments = multipartBody.getAllAttachments();
>
> for (final Attachment attachment : attachments) {
> System.out.println("Content type: " + attachment.getContentType());
> System.out.println("Content disposition: " +
> attachment.getContentDisposition());
> final DataHandler dataHandler = attachment.getDataHandler();
> final InputStream is = dataHandler.getInputStream();
>
> // Get buffered image
> final BufferedImage bi = ImageIO.read(is);
> ..
> validate for correct type of file etc...
> }
>
>
> Testing:
> WebClient client = WebClient.create("http://image");
> client.type("multipart/mixed");
> client.accept("application/xml");
>
> ContentDisposition cd = new
> ContentDisposition("attachment;filename=image.jpg");
> Attachment att = new Attachment("root", imageInputStream, cd);
> client.post(new MultipartBody(att));
>
> final String imageInputStream = "image/gif";
> final InputStream is = this.getClass().getResourceAsStream(
>                  "icon_test.gif");
> final Attachment att = new Attachment("root", imageInputStream, is);
>
>   //final File imageFile = new File("C:\\test\\icon_test.gif");
>
> final Response response = client.post(new MultipartBody(att));
>
>
>
> This works fine and my unit test is getting passed. But when i try to do it
> with an RESTClient like poster or any other tool. I am getting
> webapplication exception
>
> Few other questions:
> 1. Is there any way we could restrict the file type for the upload?
> 2. I am trying to find out the info about uploaded file like name, extension
> (.gif, jpg), mimetype etc.
>
> Ex: i am trying  dataHnadler.getName() which is null everytime
>
> 3. Can some one explain me why do we need "imageInputStream" i.e. image/gif
> for attachment , is it specify the file type for individual attachment?  In
> my case, it should only accept only one image (or can be multiple images) .
>
> 4. Is there any way to validate the uploaded file?
>
>
> Any help is appreciated..
>
> Thank you
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/cxf-Image-upload-multipart-tp4896631p4896631.html
> Sent from the cxf-user mailing list archive at Nabble.com.


Re: cxf Image upload - multipart

Posted by Sergey Beryozkin <sb...@gmail.com>.
Sorry, forgot to answer the other questions, see comments inline

On 12/10/11 20:22, kaushik wrote:
> Hey Guys,
>
> I am trying to upload an image using REST api.
>
> Below is my code:
> @POST
> @Consumes("multipart/mixed")
> @Path("/image")
> Response uploadImage(MultipartBody multipartBody) throws Exception;
>
> Implementation:
> public Response uploadImage(final MultipartBody multipartBody) throws
> Exception {
>
> System.out.println("Image upload" + multipartBody.getType());
>
> final List<Attachment>  attachments = multipartBody.getAllAttachments();
>
> for (final Attachment attachment : attachments) {
> System.out.println("Content type: " + attachment.getContentType());
> System.out.println("Content disposition: " +
> attachment.getContentDisposition());
> final DataHandler dataHandler = attachment.getDataHandler();
> final InputStream is = dataHandler.getInputStream();
>
> // Get buffered image
> final BufferedImage bi = ImageIO.read(is);
> ..
> validate for correct type of file etc...
> }
>
>
> Testing:
> WebClient client = WebClient.create("http://image");
> client.type("multipart/mixed");
> client.accept("application/xml");
>
> ContentDisposition cd = new
> ContentDisposition("attachment;filename=image.jpg");
> Attachment att = new Attachment("root", imageInputStream, cd);
> client.post(new MultipartBody(att));
>
> final String imageInputStream = "image/gif";
> final InputStream is = this.getClass().getResourceAsStream(
>                  "icon_test.gif");
> final Attachment att = new Attachment("root", imageInputStream, is);
>
>   //final File imageFile = new File("C:\\test\\icon_test.gif");
>
> final Response response = client.post(new MultipartBody(att));
>
>
</snip>
> Few other questions:
> 1. Is there any way we could restrict the file type for the upload?

You can getContentType() from individual Attachments and ensure it's 
only gif. If a number of uploaded files is limited, you can also use
@Multipart attachment, example:
upload(@Multipart("partname", "image/gif") Attachment att)

> 2. I am trying to find out the info about uploaded file like name, extension
> (.gif, jpg), mimetype etc.
>
> Ex: i am trying  dataHnadler.getName() which is null everytime
>

You should use Attachment.getContentDisposition()

> 3. Can some one explain me why do we need "imageInputStream" i.e. image/gif
> for attachment , is it specify the file type for individual attachment?  In
> my case, it should only accept only one image (or can be multiple images) .
>
It represents a Content-Type of the individual attachment

> 4. Is there any way to validate the uploaded file?
>
I think it's out of scope for the JAXRS runtime

Sergey

>
> Any help is appreciated..
>
> Thank you
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/cxf-Image-upload-multipart-tp4896631p4896631.html
> Sent from the cxf-user mailing list archive at Nabble.com.