I need to send larger video files (and other files) to server with base64 encode.
I get out of memory exception, because I want to store the file in the memory (in byte[]) then encode it to string with Base64.encodeToString. But how can I encode the file and send it out on-the-air and/or using less memory? Or how can I do this better?
To the request I using now MultipartEntityBuilder after I build it, I send it out to the server with post method and with the file I need to send other data too. So I need to send both in one request and the server only accepts files with base64 encoded.
OR
Because I using Drupal's REST module to create content from posts, it's another solution for me, if I can send normal post with a normal form. (like the browser does) The problem is, I can't find, just only one solution. When you call the <endpoint>/file url and you pass four things, these are:
array("filesize" => 1029, // file size
"filename" => "something.mp4", //file name
"uid" => 1, // user id, who upload the file
"file" => "base64 encoded file string")
After this request I get an fid, which is the uploaded file's id. I need to send this with the real content, when I create node. If I can send the file with normal post mode (without encode) like the browser does at form send, it would be better.
I need to send larger video files (and other files) to server with base64 encode.
You should consider getting a better server, one that supports binary uploads.
I get out of memory exception, because I want to store the file in the memory (in byte[]) then encode it to string with Base64.encodeToString.
That will not work for any significant video. You do not have heap space for this.
But how can I encode the file and send it out on-the-air and/or using less memory? Or how can I do this better?
You can implement a streaming converter to base64 (read the bytes in from a file and write the bytes out to a base64-encoded file, where you are only processing a small number of bytes at a time in RAM). Then, upload the file along with the rest of your form data.
Related
I have the controller shown below:
#RequestMapping(value = "/videos/{id}",
headers = "Accept=image/jpeg, image/jpg, image/png, video/mp4",
method = RequestMethod.GET)
public ResponseEntity<byte[]> loadVideo(#PathVariable("id") long campaignId,
Principal principal) throws IOException {
This controller returns a byte stream of the media associated with the given id. It works fine. The only issue I'm having is loading this videos associated metadata (title, description, view count, etc...) as I'm sending back an array of bytes, so I'm not too sure where to put the meta data.
Should I place the metadata in the response headers?
Should I have two separate calls, one for the video (byte steam) and
another call which returns an object containing the meta data?
Or is there a better way to go about this than either of the two
options above?
As my comment was already lenghty I decided to repost it here again:
If you deal with certain media-types like image/jpg or video/mp4 you should include the metadata as headers as the payload of the response should only include the bytes of the respective file. This also enables a lookup of the metadata without having to download the bytes of the actual file via a simple HEAD request.
Certain API provides, howerver, define their own media-type or send a JSON or XML based response to the client. In this cases, the payload contains often a predefined structure which includes the bytes of the file as a base64 encoded string as well as the metadata as plain text. These APIs argument that sending multiple files at once is easier this way then to handle multipart content.
My main question is how can I pass JSON as well as File to post request to REST API? What needs in Spring framework to work as client and wait for response by passing post with JSON and File?
Options:
Do I need to use FileRepresentation with ClientResource? But how can I pass file as well as JSON?
By using RestTemplate for passing both JSON as well as File? How it can be used for posting JSON as well as File?
Any other option is available?
Sounds like an awful resource you're trying to expose. My suggestion is to separate them into 2 different requests. Maybe the JSON has the URI for the file to then be requested…
From a REST(ish) perspective, it sounds like the resource you are passing is a multipart/mixed content-type. One subtype will be application/json, and one will be whatever type the file is. Either or both could be base64 encoded.
You may need to write specific providers to serialize/deserialize this data. Depending on the particular REST framework, this article may help.
An alternative is to create a single class that encapsulates both the json and the file data. Then, write a provider specific to that class. You could optionally create a new content-type for it, such as "application/x-combo-file-json".
You basically have three choices:
Base64 encode the file, at the expense of increasing the data size
by around 33%.
Send the file first in a multipart/form-data POST,
and return an ID to the client. The client then sends the metadata
with the ID, and the server re-associates the file and the metadata.
Send the metadata first, and return an ID to the client. The client
then sends the file with the ID, and the server re-associates the
file and the metadata.
I am working on SOAP web services. I have been given a third party WSDL URL for which I need to generate client code. I need to pass a zip file which contains a bundle of XML, for which the passing parameter should be type octet stream. I don't know how to pass an argument as an octet stream. How can I do this?
A SOAP Web-service needs to return the result in a format defined by SOAP - basically in XML. Octet streams are a different mechanism of file/data transfer, so that's not what you need.
What you need to use is "SOAP with Attachments" or "MTOM (Message Transmission Optimization Mechanism)", where you ZIP file will be attached to the SOAP message in some way. If you're using Java with Axis 2, This article in the Axis documentation describes what you what you need to do.
Remember that your webservice clients will also need to understand MTOM or SwA. So pick the approach that is easily supported by your clients.
Basically I need to provide REST service that would receive a String param, use that param to fetch a file from another system and then return the fetched file back as the response.
The effect should be the same as when a user clicks on a pdf or any other binary file link and the browser prompts him to save/download that file.
A couple of points:
is it possible to stream the file (to send bytes as I receive them from source system). In other words, how to handle very large files?
also related to streaming, when using regular HttpServletResponse, do I have to wait until a large file is completely read to return response.build()?
How do I go around doing this using Apache Wink?
PS Sorry, this may be trivial for Wink gurus, but I'm just starting to wrap my head around developer guide.
You can just return the java.io.File from your method. You can wrap it with Response if you like. Wink will handle the streaming. The streaming doesn't start when you call to response.build(), but rather when your method finishes.
If you want a correct download dialog, you should return the proper Content-Disposition header. See How to set response header in JAX-RS so that user sees download popup for Excel?
I have wriite a small proxy (in Java), with the intention of modifying specific data from a web server response. The data is a deflate compressed XML file: only some of the elements do I want to actually be received by the client.
I have no problems with the proxy functionality, but when I try to send my modified xml in place of the server's response, the client receives nothing (web-debugger shows content length 0) - I am sending data though, atleast there IS data in my app being written to the Socket's output stream.
See here for more details and some Code. I won't post it twice.