JsMartinez
JsMartinez

Reputation: 341

Save a JSON image in a server

I'm writing a RESTful web application where I need to provide the service of uploading images for a user. Currently, I have been able to upload an image from my current machine but I need to send it as JSON data over the web through the REST protocol.

In the server, there is a Java application running Jax-RS to manage the RESTful service. I was planning to save the JSON data that contains the image in the server and then provide a URL to the user for him to be able to locate it's image on the server.

Can someone provide some ideas on how can I do this?

Upvotes: 0

Views: 2167

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208974

If you want to send the image in a JSON object, then the image should be Base64 encoded it, or some other form of encoding. Then on the server side you will need to unmarshal the JSON and then decode back the image. You can get some ideas here on how that can be done.

Optionally, instead of doing all the converting inside the resource method (as in the example linked above), you could write a custom MessageBodyReader, where you can do the unmarshalling and decoding there.

If you decide you don't want to work with JSON, you can go the normal route and use Multipart. Depending on the implementation of JAX-RS you are using, multipart support will be different. You can see some examples (all examples have links to the official documentation)

There are other implementations, but I don't have examples for those. You will need to search for the documentation if you're using an implementation other than listed above.

Upvotes: 3

Related Questions