Codenameone getting the byte[] of a file - image

After this line:
String photoFileLocation = Capture.capturePhoto();
I want to transmit such image to a server. How Can I get the bytes[] of the file which name is in the variable 'photoFileLocation'?

Use FileSystemStorage to open files.
But for actual file upload you should use MultipartRequest which accepts file URL's (more efficient than reading bytes). You can see an example of this including the server code in the recent workshop we released https://www.codenameone.com/blog/build-mobile-ios-apps-in-java-using-codename-one-on-youtube.html
You can see a full sample in the JavaDoc for multipart request here: https://www.codenameone.com/javadoc/com/codename1/io/MultipartRequest.html

Related

How to return an image file (Byte[]) as a compressed file with Spring API?

I'm the company's file server
Get the file as byte[] through the image path and authentication key.
(This server is not accessible to me.)
What I want to do is, when the user downloads the selected files, I want to compress these files and provide them as a compressed file.
Since the company's file server does not have a download API for multiple files, I think I need to request as many APIs as the number of file lists with a for statement in my service API.
In other words, it seems that we need to take a List<Byte[]> and compress this list.
Is there something wrong with my method?
And can I pass the result as json after compression? (I confirmed that the image file is passed as json.)

How to download file using ProtoBuf

I'm trying to implement file download directly via Browser. Our company uses Protocol Buffer as data communication format. So how can I download the file once I open the web page?
I tried to user bytes and stream of Protocol Buffer. But the result is
{"result":{"data":"Cw4ODg4ODgsMCw4ODg4ODgsMTUwMCwwLDE1MDAsNDAwMDAsMTAwMDAsMzAwMDAKMDMvMTEvMjAxNSxVbmtub3duIEl0ZW0sUHJlIFJvbGwgVmlkZW8gKG1heCAwOjMwKSw2MDAwMCwzMTAwMCwyOTAwMCw1MDAwMCwyNDAwMCwyNjAwMCwyMC4wMCUsODQ0NCwwLDQwMDAsNDQ0NCw4OTAzODgsMCwwLDAsODg4ODg4LDAsODg4ODg4LDE1MDAsMCwxNTAwLDQwMDAwLDIxMDAwLDE5MDAwCg=="}}
Protobuf is good for structured communication but http provides the perfect protocol for downloading files. The right headers need to be set and the browser will download the file.
If you really have to use protobuf to transfer files, then you need to add some javascript that is parsing the protobuf first and then turns it into a file that can be downloaded. See How to create a dynamic file + link for download in Javascript? for reference.
So, send the message as bytes, add the javascript that parses the protobuf message to extract the bytes, and then create the file download like on the linked answer.

Jmeter is downloading a 67Mb file but only saving 10,240 KB

Bit of Background, I am using Jmeter to search for a download url from a rest API with an Oauth authorisation token set from the rest API. Once I have this URL I am doing a HTTP request, GET with redirect automatically, keepAlive and browser-compatible headers all checked.
Hanging of this I have then attached a "Save response to a file" with the file name prefix set to "blob" (this will be a filename set by a parameter later) and Add timestamp to the file name checked.
The url in question points to a zip file that needs OAuth header token (which is set successfully) the whole test plan succeeds.
This is great and you can see looking at the results
You can see that there are 67821343 bytes downloaded by the HTTP Request this is what we are expecting to see as this is the size of the file around 67Mb
This is were it starts to go wrong however as the save file only has 10,240 KB is an OCTET-Stream File and renaming this file to a zip just does not work as it is not a complete zip file.
This is my issue the Save file is not saving all the information and only save 10,240KB, every single time.
What am i doing wrong?
Is there a better way to get this zip file?
Please help it is driving me mad.
As once I have downloaded it I then need to assess the contents of the zip file to prove that the download URL that we are being directed to contains the correct ZIP.
here's the RUB if i do it manually through a browser i get the file downloaded successfully and it is exactly the same size (67821343 Bytes) but it just doesn't save in Jmeter.
Add the following property to user.properties file:
httpsampler.max_bytes_to_store_per_request=73400320
This will allow JMeter to save files up to 70MB
You can also set this property to 0 - in this case JMeter will not truncate data (make sure you amend JVM heap so responses could fit in memory)
References:
Configuring JMeter
Apache JMeter Properties Customization Guide

Once Spring controller method is invoked can I confirm that the file is completely uploaded?

I am trying to upload an excel file in the OpenShift server data directory (OPENSHIFT_DATA_DIR) and store the data in the MySQL database provided by OpenShift. I am following the below steps:
Upload the excel file to the OPENSHIFT_DATA_DIR.
Read the excel file from OPENSHIFT_DATA_DIR and parse it.
Update/insert the parse data into the database
I am able to upload excel file into the OPENSHIFT_DATA_DIR with the below code. I use Spring's multipart resolver
private void uploadExcelFile(MultipartFile file) throws IOException{
String fileNameWithPath = System.getenv("OPENSHIFT_DATA_DIR")+"Test.xls";
file.transferTo(new File(fileNameWithPath));
//Start reading the excel file from OPENSHIFT_DATA_DIR
.......
.......
}
I will be able to read the excel file from OPENSHIFT_DATA_DIR, but my concern is I am not sure whether the file is completely uploaded or not at the time of reading the excel.
Once "transferTo()" method is executed can I confirm that the file is completely uploaded?
Or, How will I handle it if the file is not completely uploaded?
After the transferTo() mothod call, you can do anything you want, with the transferred file.
If you read the doc of the transferTo() method, you will see, it is not asynchron.
Documentation says:
void transferTo(File dest)
throws IOException,
IllegalStateException
Transfer the received file to the given destination file.
This may either move the file in the filesystem, copy the file in the filesystem, or save memory-held contents to the destination file. If the destination file already exists, it will be deleted first.
If the file has been moved in the filesystem, this operation cannot be invoked again. Therefore, call this method just once to be able to work with any storage mechanism.
Note: when using Servlet 3.0 multipart support you need to configure the location relative to which files will be copied as explained in Part.write(java.lang.String).
Parameters:
dest - the destination file
Throws:
IOException - in case of reading or writing errors
IllegalStateException - if the file has already been moved in the filesystem and is not available anymore for another transfer

Replacing the body of a proxied subrequest with the contents of a file

I'm using the upload module to write the uploaded file to disk as soon as it arrives in nginx. In addition, I'd like to create 2 subrequests:
POST to a URL containing the uploaded file
POST to another URL without the uploaded file
The second request is easy to do because the upload module has already stripped out the upload. My problem is with the first request: How do I get the uploaded file back into the the subrequest.
A solution for my question has been committed to the echo module.
The module you linked to has the upload_set_form_field directive and a few special variables (listed in that directive), which you can use to pass the file details to the backend as a POST variables. The example given appears to put the upload back in the POST data. Can you adapt your backend script to make that work?

Resources