non-form based file upload servlet in Java - java

I am trying to upload files using HTML5 File API to a Java Servlet. Here is the use case
A user downloads the file to local file system (using HTML5 file api)
Once, the required change is done, the file needs to be uploaded back to the server.
There is no form based selection and the file needs to be streamed. I am successful in converting files to base64 encoded string and sending to the server (which is form based upload) as a string attribute.
But, I wanted to server to accept Arraybuffer or binary streams. This is not possible in form-based based upload. So, is there any other way of handling file uploads in Java other than form-based methodologies ?

Related

Send file directly from an HTTP API to end user in a Java Webapp

I am looking for a way to use my Java App as intermediary for a file transfer from an HTTP API to an end user.
Is this possible or do I have to download the file and then send it to the user?

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.

Uploading a base64 image via Form upload

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.

java - checking file content before uploading to server

i have got a situation now.
I need to develop a webpage where user can select a file to upload and before uploading the file to server i need to check first few lines of the file whether the data is valid or not and if the data is valid then upload the file, if not through an error message.
the file will be text file.
thanks,
Sandeep
HTML/Javascript does not offer a way of reading the contents of a local file. You must either upload it and check it in the server.
If you really want a client side check, you then must build a signed applet(or even ActiveX) to run in your webpage and handle the upload instead of using plain HTML.
You should perform your validation on the server side, right before you perform the upload.

Categories

Resources