How to transfer files (PDF, images etc) in JSON? - java

How to send file from my backend project to my frontend using JSONObject in Java so I want to know if it is possible and how? (I'm using Spring framework)

You can directly send bytes of the file or you can convert file into stream and then send.

Related

need to upload an xml file, parse and store in database without storing file at any physical physical location

I need to upload an XML file, parse and store the data in the database without storing file at any physical location( if possible we can store it in request). I am using spring, spring REST.
You can parse the XML data using Jackson, GSON,etc library by creating a REST API(as you want to use RESTfull web service). It would be quite easy to parse if you know the contract of your XML file by creating POJOs.

RESTful Webservices with PDF as an input file

Is is possible to exchange PDF file as a input for RESTful webservices and also I would like to send PNG image as a response to it.
If we can do it through REST services please provide me the references link to implement it using REST services.
I was able to achieve a similar output by converting the file to string/byte[] data and sending it via REST.
My implementation was in Java and the steps used is outline below
Convert the file on disk to byte[] array (apache common-io can convert the file to byte[] in easy step. Try the IOUtils class)
Encoded the byte[] as String (apache common-codec was used for the encoding)
Wrapped the string data in a model class
Converted the model class to json format (GSON was used for the conversion)
Sent the json data over to the server
The server application reversed the process, and the file was available on the server
A rest service isn't the right way for what you want. The input for this kind of services are HTTP request attributes or some kind of pushed data. Maybe it's possible to implement a file upload but it's not typical.
For restful services is also common to tell your service how to handle the requested resource via the used request method (GET, POST, PUT, DELETE)
The response of rest services is normally some kind of structured text output - for instance json.
All in all rest services seems to me not the way to implement your desired scenario.
What about a normal cgi or servlet solution?

Posting file content to a REST webservice along with other data in java

I have a REST webservice developed using java. I have a jar file which does some logic and writes some data to a file. My requirement is to send the data along with a couple of other data (other two data are just simple strings). How do i write the service part ?
1. Should i write the service so as to accept data as byte stream ? If so how do i send the file content from the jar to the service ?
Any help is appreciated

How to deliver .jad file using Jersey Rest service?

I have to return a JavaME .jad file from restful web service (using Jersey), which is then used to install an app on a mobile phone. Before delivering the .jad file some values in there have to be changed. I was thinking of reading the original file, change the read input and writing it back to some outputstream. Can I just return the output stream in a Jersey Rest services? Is there anything special I have to take of in terms of mime-type, etc.? Does anyone know of some kind of example code or tutorial for this purpose?
Thanks in advance
Either like here:
Input and Output binary streams using JERSEY?
or you could send the StreamingOutput as an entity like this:
return Response.ok(streamingOutput).type("text/vnd.sun.j2me.app-descriptor");
Point is you have to use the OutputStream handed to you via the StreamingOutput.write(OutputStream outputStream) method.

How to expose Blob (Image) data via REST webservice

I'm storing images in blob form in a MySql database table. I need to expose these images via a REST webservices and was wondering what my options are. The main option I see is to read the database, create the image and store it on the filesystem and send the path to the image in the REST response. Any other ideas here? I'm thinking I will use RESTeasy for my JAX-RS implementation. Thanks for the help!
You can parse your image into bytes and then can send those bytes in JSON form via web service.

Categories