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.