Base64 String in JSONRequest Java - java

I'm handling a Web Service and need some help. The process is that a pdf will be encoded with base64 and sent to my web service. I will then decode it back into a pdf and place it in the appropriate folder. The issue is that the request needs to contain the actual giant base64 string. First question is is this possible. Second, I am using postman to make the requests and was wondering how to even copy the base64 string into it. It seems there's a string limit. Any help would be greatly appreciated.

I don't know about postman but I can suggest to use JAX-RS and implement a ReaderInterceptor and a WriterInterceptor using Base64.Decoder#wrap respectively Base64.Encoder#wrap.
Otherwise, maybe postman has similar features?
Use streams like these as much as possible to reduce memory usage.
Tutorial:
https://jersey.java.net/documentation/latest/filters-and-interceptors.html#d0e9806

Alright it just seems to be an issue with Postman. When you place a string of that size it will give you errors and only put a certain length per line. It will still receive the entire string. I am able to receive it and decode it. Thank you all for your help!

Related

How can I receive a large string in Java Servlet?

I am building a JSP application. I am trying to send an screenshot image from the page to the servlet by using base64 encoding. It makes the returning string super long with 100k characters length. So when I post this to the servlet and with getParameter, I am getting null there.
Is there a way to get them by chunks are am I missing something?
I found this maybe useful for you.
Passing a huge String as post parameter to a servlet
Namit Rana:
We used GZip compression/decompression to lower the size of the string. And it worked effectively.
So, the .net service compressed the huge string, sent it to our Servlet. We then decompress it at our server.

How to get image size from URL in java

I want to get the image size from the URL.
My source is working locally but aws Ubuntu server will return -1.
What's the problem?
URL url=new URL("https://www.bithumb.com/resources/img/comm/sp_coin.png?v=180510");
int image_size = url.openConnection().getContentLength();
System.out.println("mage size:: "+image_size);
If the server doesn't respond with a valid Content-Length header, getContentLength returns -1. As an alternative, you can always get the size of the returned data:
size = IOUtils.toByteArray(url.openStream()).length;
Using Apahe commons IO IOUtils.
What's the problem?
Difficult to say. However, ultimately it is up to the implementation of the web service (not Ubuntu or AWS) to include a content length in the response.
So if you want to find out why (and fix it) you need to examine the implementation of the (your?) web service, and how it is creating the responses.
As #jspcal points out, you can work around the problem. But in order to do that you would need to (at least) read the entire image from the response data stream. Whether this is a viable / efficient solution depends how / when you intend to use the content length.

PDF encoding issue

I am having difficulty receiving a PDF file via a rest service. The rest service returns a long string with the data that is suppose to make up the PDF. My overall goal is to save the response as a PDF file for later use.
I call this service url: http://4.hidemyass.com/ip-2/encrypted/dnXuHKAZVZaONS2GNfC9RFn8k8puE2YJx6MjcPDMaKdpMRTBkvNF4CrTg4m7GeKjcLfO1bgYWIwR9bz1ZJP-LTK6Gm8tG_-d4V-oSUMfT-tIJMuZizsz9AeZp5tcZWVcz62A6j7YRWqJRAS_s_cMFLlo&f=norefer
and according the docs, it should be the string contents that make up a valid PDF.
What am I missing? What do I need to do in order to make is viewable as a PDF.
Thanks!
Chuc
Sorry the above link failed.
In the end we found that sending the PDF as binary data wrapped in JSON did not work very well. The creators of the service found that their framework was manipulating the binary data ever so slightly and converting some characters. They ended up switching to Base 64 encoding which worked great.

RESTful webservice with unknown number of parameters

I am writing a RESTful web service using Java which should be able to take as (POST) input an unknown number of parameter name, value pairs. I am thinking what is the best way to achieve this.
The POST input will be JSON with the expected format being:
{"input" :
{ "x" = 1, "y"="Hello, "z"="1.2.3.4",... }
}
The parameter values are expected to be a mix of int/long/float/string etc. The expected behavior is to save this input to the DB and provide it back on request. The tricky part is with the parameter names being unknown, I cannot write getter and setter methods. Is there a simple way for handling this?
Any pointers in the right direction will be very much appreciated. Can I read this as a List? Do I need to write Custom MessageBodyReaders? Is there some other way of achieving this? The only option I can think of is to have these sent as a file and parse it but am hoping that there are simpler/cleaner solutions. Thank you for any pointers...
If all you need to do is save it and make it available on request, why parse it at all? Simply write the json as a string to your database, and return it as a string while setting the response type to "application/json". The one caveat being you're going to have to have some way to identify which json string you want and associate a unique id that you're going to be able to determine when you're requesting the specific json string in the future.
Something worth noting here would be that when you don't parse something, you don't actually know what it is. Even though a lot of modern libraries/frameworks sanitize against sql injection/xss already, it would be worth it to verify that this is being done in your server if you don't already know for sure.
That being said, if you're still feeling as though you need to parse the json, I would look into gson: http://code.google.com/p/google-gson/

Arabic Encoding in UFT-8 from Web Service

I am trying to encode Arabic text from a Web service. Currently the values come as question marks (???).
I have read many blogs (even stackoverflow answers/links) but nothing seems to worked.
Any idea of how I can resolve this issue?
Thanks
If you use dreamweaver's designer view and paste your Arabic text in design view you will get ascii characters in dreamweaver's code view which will work in any web browser.
First, an important aside: check that the web service you are consuming sends you actual Arabic characters and not actual question marks. Check a network dump if you are not sure, and use wget/curl to perform a simple transaction; check the results.
If the raw data as sent by the WS is question marks, you have an uphill battle - try again and fiddle with the Accept/Accept-Charset headers. If all fail, it may be that the server itself isn't coded properly and there ain't much you can do after that...
Also, you're trying to decode the text, convert it from a byte representation to abstract characters.
This has been the problem Sending UTF-8 data from Android. Your code would work fine except that you will have to encode your String to Base64 . At Server PHP you just decode Base64 String back. It worked for me. I can share if you need the code.

Categories

Resources