Loading a file not in the classpath into a Spring Resource? - spring

I currently have a Spring API service that takes a Base64 String img and decodes it.
String imageDataBytes = img.substring(img.indexOf(",")+1);
InputStream stream = new ByteArrayInputStream(Base64.decodeBase64(imageDataBytes));
I want to load contents of stream onto a Resource class. But so far my research is telling me I can only load resources through a URL or file path, which I don't have here. What can I do?

You can use ByteArrayResource. It takes a byte array in constructor. Here is the documentation .

Related

How to use a file read via "FileHandler" of "FileAccess API" inside "img" tag

Here's a detailed explanation on what I'm trying to do-
I'm reading a HTML file from a directory which has the image tag used but the assets are mapped locally and are not from url.
So somehow I'm able to get access to the file.
const fileHandle = await dirHandler.getFileHandle(file);
Now, I somehow want to use the content of this file inside img tag.
The way it's possible is using having a base64 url encoded file.
I'm not finding much stuff/detail on this.
A help would be highly appreciated.
Thanks
You don't need a base64 data url nor do you want it, instead create a blob: URL from the File object your FileHandle points to. To retrieve this File object, call the getFile() method of your handle.
const file = await fileHandle.getFile();
const url = URL.createObjectURL(file);
img.src = url;
or in a single line
img.src = URL.createObjectURL(await fileHandle.getFile());

org.apache.camel.component.file.GenericFileOperationFailedException: Cannot store file: output\getOrders.txt

I am learning Camel Integration with Springboot. I was trying to get the json response from restapi and then want to store the data into some file.
Below is the route class component which I created . It is not able to convert the json response into filestream type. Can anyone tell me what am I doing wrong here.
rest().get("/orders").produces(MediaType.APPLICATION_JSON_VALUE).route().
setBody(()-> orderService.getOrders()).to("direct:transferToFile").endRest();
from("direct:transferToFile").convertBodyTo(Order.class).to("file:output?fileName=getOrders.txt");
You cannot write an Order object into a file. You need to marshal it into json before writing it to the file.
You can add a marshal().json(JsonLibrary.Jackson) after the convertBodyTo.

How to send MultiPart HTTP request using Spring RestTemplate

I am new to Spring framework and i am learning.
My web applicaiton based out of Spring MVC needs to call a vendor service through RESTful interface.
I have current implementation for POST / GET for non multipart.
However i have a requirement to POST multipart form data consisting of JSON and Bytes
I am trying to see some smaple implementation online for reference but could not get one.
I need some idea about possible approach i can take to implement this using RestTemplate.
Thanks for reading.
if we want to send multipart form data in post request and with that if you also want some information in json format then you can create your REST call according to this. here #Consumes will say that this call will accept only MULTIPART_FORM_DATA, #Transactional is for starting new transaction.
Here i am accepting three path parameters which are
1) String jsonObj, this is a string or you can say json, in this JSON you can ask UI for required information like some tags, labels, etc
2) FormDataContentDisposition fileDetail, This will contain very basic detail of file, like fileName, contentType, etc
3) InputStream uploadedInputStream, this will contain some binary data, like image, videos, or any kind of files in byte format.
* Example *
#POST
#Path("/xyz")
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public VObject postMultiPartFormData(
#FormDataParam("jsonObj") String jsonObj,
#FormDataParam("mmFile") FormDataContentDisposition fileDetail,
#FormDataParam("mmFile") InputStream uploadedInputStream) {
return new VObject();
}
I hope this will help you.

spring data rest: why is all content exposed in general object uri (e.g. /files/), not only in specific uri (e.g. /files/1)

I am setting up a project using spring data rest.
Exposing my domain model seems to work, but I have some strange behaviour:
According to the wiki/docs if I access a uri like /files/
I should end up with an array of links to the single files. But, I get not only the links to the files, but also the attributs of the files objects when accessing the uri /files/.
This is anoying, because I have the content of the files as byte[] and I end up transmitting all contents of all files when accessing /files/
Does anybody know how to turn this behaviour off?
Ok, I just found the answer myself. I am posting this for others with the same question...
I have to use the header: "Accept: application/x-spring-data-compact+json" in my request to get the compact representation of my repositories...
That's it...

Drupal 6 - Services 3 - Image Upload

I'm using Services 3 to create a custom service that will create or update a specific content type (similar to the built-in Node Service). The content type uses CCK for a few fields, mainly an image (file) upload field. I have no problem connecting to the service and creating a new node by setting the text parameters (including some CCK fields), but I can't figure out how to handle submitting a file.
I've tried base64 encoding the jpg then passing it as a parameter using the same field name as the node creation form uses, files[field_um_high_res_0]. I've also tried submitting a multipart form.
Any ideas?
Thanks,
Howie
Looks like all I had to do was encode the file as base64 then use the file resource to create a new file in the system and take the resultant fid and use it to load the new file object which is passed as the value of the CCK file field.
So it looks like this:
my_cck_file_field_name[0] = the_file_object

Resources