Uploading a base64 image via Form upload - java

I have a web app that within it, it gets a base64 from the client side, omitting the details here is how the base64 is retrieved:
String source = e.getAttribute("src");
String base64 = source.substring(source.indexOf("base64,") + 7)
Now that the base64 string is retrieved, I need to upload it via Form upload. I'm not sure what is the correct approach to upload this as a File to my working backend.
The problem is not the backend, I have tested it to receive standard Form upload and it works fine. So the issue I face, is how to upload the base64 say to a backend endpoint /blob/upload
Update:
I'm thinking of using GWT Rpc to upload the base64 String but I am not sure if that is efficient for large files.

Is there a particular reason you're uploading a Base64 String? It will probably get messy.
If there is no reason to do it that way, use the com.google.gwt.user.client.ui.FileUpload widget on the client, and make a simple HttpServlet on the backend to accept the files.
You can then use the Apache Commons FileUpload library to grab the uploaded files and do with them as you please (See "The Simplest Case" section of that page for usage details. It's pretty easy to use).
Also, beware of large files. The default Tomcat POST size is 2 megabytes, I believe.

Related

View Content of data stored on google cloud to frontend application instead of download file with the name of blob

I am currently storing text on the cloud using
bucket.create(blobName, "hello".getBytes());
and blob name looks like this 1/1/1674/2020-06-02/9998-2-202062
The requirement is to generate pdf file from the data of multiple blobs. We can do this on our backend in java by getting the content from blob path like this
Blob blob=bucket.get("1/1/1674/2020-06-02/9998-2-202062");
new String(blob.getContent())
But we don't want to increase the load on the server by downloading the content first on the server and then send it to the front-end. So we are sending the signURL on the front-end so we can get the content at the front-end(angular 8) by using that URL and create the pdf. We are creating singurl this way.
Blob blob=bucket.get("1/1/1674/2020-06-02/9998-2-202062");
URL url=blob.signUrl(1, TimeUnit.HOURS,SignUrlOption.signWith(
ServiceAccountCredentials.fromStream(new FileInputStream(jsonfilePath))));
But the issue on front-end is whenever we click the URL on any browser it downloads the file with the blob-name
9998-2-202062 instead of just showing the content.
Is there any way so we can read the data from that cloud URL instead of downloading the content by chrome automatically?
And we do not change the browsers setting because it can not be done on the client machine?
The signUrl method only creates a link to the specified resource, you have to implement a way to read it, join the rest of the files you want to add and process them to create your PDF on the client side.
That said, for example you can create a javascript file and use the signed URL in a fetch function to gather all the text from the blobs, I found this example on another stackoverflow answer that might help you:
Promise.all([
fetch(signedURL1).then(x => x.text()),
fetch(signedURL2).then(x => x.text())
]).then(([sampleResp, sample2Resp]) => {
console.log(sampleResp);
console.log(sample2Resp);
});
Replace the signedURL1 and signedURL2 for the actual signed URLs that you created.
Once that you have the content of your files, you just have to create the PDF, I found a library named jsPDF that could be of your interest.

java - send a file to client by file URL without download on the server

In my web application I have a link which, when clicked, invokes an external web service to retrieve a download URL for a file.
I need to send back to client the file which is beyond this URL, instead of the download URL retrieved from the web service. If possible, I would also like to do it without having to download the file on my server beforehand.
I've found this question about a similar task, but which used PHP with the readfile() function.
Is there a similar way to do this in Java 8?
If you doesn't even want to handle that file you should answer the request with a redirect (eg HTTP 301 or 302). If you want to handle the file you should read the file in a byte buffer and send it to the client which would make the transfer slower.
Without seeing your implementation so far, this is my best suggest.

How to Upload Image/Videos to Google Cloud Storage Using Java

I'm developing a server in Java which will provide URLs for images that clients uploaded. Basically the Android/iOS will send some kind of data (I still don't know which data would be sent for image and videos) then I'll upload those images/videos to Google Storage and provide URLs which the users (Android/iOS) will be able to stream.
I've just started with Google Storage and I can't find any example of how to upload an image using Java. All examples I found or are deprecated or it's in PHP or it's not clean.
I think I will need to use the JSON client Library but I can't find a good example for this library either.
Please any help will be VERY appreciate.
Based on your answer to my comment above, you could use the method I described in the following Stack Overflow response in order to save the file from your servlet to the Datastore and return the URL:
How to return a file with Google Cloud Endpoints?.
The two differences between your requirements and the method described in this response are:
The way the file is created: in your case through a file input stream, as you mentionned
The way you return the URL: since you will probably not use Google
Endpoints (because of file input stream) you don't need to return the URL in a string wrapper.

How do I upload a file(picture) from android and save to the blobstore using GAE and google endpoints?

Sorry if this is a little general. Hopfully I can get pointed in the correct direction.
I have an android app that includes instant messaging. I want to add the functionality to send photos in messages as well. My backend is built on GAE and cloud endpoints and is written in Java.
So far, I've looked into google cloud storage, the blobstore, java servlet pages, etc. But a clear solution (or example using endpoints) has been impossible to find.
So, as the question states, how can I send and serve images from the blobstore using android and GAE endpoint backend?
Edit: This question is only regarding the back end. In android, I can do the http post easily enough. I'm just lost when it comes to doing this in endpoints
I suggest the following approach to implement what you require:
Pass the image data as base64 encoded text from your client.
On the server side, you can extract out the image content and then use one of many options that are available, which include Blobstore, Google Cloud Storage, etc. I suggest that you go with Google Cloud Storage because that is the recommended approach. If you prefer the Datastore, keep in mind that the data is limited to 1 MB in size, so you might hit that limit depending on the size of images that you are dealing with.
A SO question and answer here contains lot of relevant code that you could utilize: Sending images as a base64 string to a google cloud endpoint from cms
Why not using a simple HTTP POST to handler for image upload?
When I use the blobstore I typically have a pair of handlers one for uploading and one for serving images.
So, in your case I'll rather do a post to a URL sending the image file and some parameter than can link it to the proper place in the datastore (i.e. user ID).
Then, using endpoints I'll have the same linking key and display the image using the serving handler with the given parameter.
If you are already using endpoints you can follow #Romin 's suggestion and send the images as Base64 or upload your images in 2 steps using the blobstore/GCS service, first get the Upload URL (via endpoints) and then post your image to that URL.

Upload file to webservice (java)

I am looking for a solution to upload a file from a client to a server connected through a web service.
The client is written in c# and the web Service in java.
The files can be rather large < 100MB.
What approach would you suggest is best ?
Base64 encode the file and send it as an attachment. If you need to make sure the contents of the attachment do not get changed en route, use MTOM. Otherwise, use DIME.
Agree an encoding on both client and server then serialize the file using that encoding, wrap it in CDATA tags and assign the value to a text node in your SOAP request on the client.
Read the data between the CDATA tags on the server, deserialize it using the agreed encoding and you'll have the byte stream to use as need be.
It's probably a good idea for the encoding to involve some sort of compression if the files are large, although be wary of interop issues if the client is .NET and the server Java.
For the server side, you should have a look at Commons File Upload

Categories

Resources