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.

Reply via email to