Displaying byte array as an image in a JSP - image

I have a Servlet that I've designated as the welcome page for my Java Web Application. In the Servlets doGet method, I retrieve a list of Product objects from the database. The Product object is composed of: an ID, Name, Description, and Image(byte[]).
I then add the list of Products to the request as an attribute, and forward to a JSP where I want to display all of the products(in a table). Displaying the properties like name, id, and description are easy.
My problem is, I'm not sure how to display the image. Any ideas?

You would need to base 64 encode the image data and generate a data url which look like this:
data:image/png;base64,21fe8w4r7qwe/f4sd68f4/er41we5f1sd/1a3/13dfvd21
Something like this code would do the trick:
String url = "data:image/png;base64," + Base64.encode(bytes);
then use that url as your image src.

Related

How to add form fields to image preview?

In vue-filepond, I can drag or select files and they process fine, but I want to be able to submit user entered data along with each image. For example, they may want to tag the image or enter a title, etc..
I can't find any documentation on how to do this?
You can use the file.setMetadata method (https://pqina.nl/filepond/docs/patterns/api/file/#methods) to add metadata to each file, each upload will contain a File object and a JSON object (the JSON object will contain the metadata).

Attach a filename to a base64 image in a JSON file

I have an application that gets a JSON file via a web service. The JSON is fairly large, and represents a Person object, with typical properties such as first name, last name, title and image for example. The person's image is stored as a base64 field in the JSON. It is bound to an image tag using Angular. So for example, the HTML looks like this:
<img ng-src="data:image/jpeg;base64,{{ person.fileImage }}">
When the end user right clicks on the image and chooses to save the image, the browser defaults to the name "download.jpg". What I need to do is name the image so that when the user right clicks and chooses to save, it gives a meaningful filename, e.g.:
todd.davis.jpg
I'm not sure how to make this happen. I've seen some solutions that use an anchor tag with a download="todd.jpg" parameter, but that is not working for me. I think it expects an actual URL and in this case, I don't really have one. The image data is just embedded in the JSON.
Is there a way to manipulate this so that I can add a name to the image for saving purposes?

Is it possible to identify file name passing url in magento?

How to identify coding having this file at magento .
For example,
Link Mean
how to identify which file its denotes ?
http://localhost/index.php/about-magento-demo-store/ is not a file, it's CMS page content which is actually a row stored in the cms_page table in your database that gets filtered through a template to produce html page content that is pushed to your browser.
Look under the CMS=>Pages menu, you'll find a grid and if you search in the url key column for about-magento-demo-store, you can find the page content there.
Magento's content does not exist as static pages, it is data stored in the database that gets selected, filtered through templates and assembled into HTML that gets final styling from CSS. It only becomes a page, once it is downloaded by the web browser.
1)Url redirects
look at the core_url_rewrite table and find the url that you requested.
if you find it, and target path starts like catalog/category/view/id/5,
that means catalog module, category controller, view action which you can find in
app/code/core/Mage/Catalog/controllers/CategoryController.php the method is viewAction.
2)Cms Pages
it can be a cms page
3)One of modules controller
It is the best way to install a profiler and see which controller handles your request.

displaying images from mongodb gridfs using spring data

I am new to this area, I have added a number of images to gridfs. Now i want to display all these images on html page. I have retrieved the images from mongodb using the following code.
Query query = new Query(where("filename").is("file"));
List<GridFSDBFile> images = gridFsTemplate.find(query);
model.addAttribute("images",images");
It gives me all the images, now i have no idea how to display these images on html page. i am using velocity template.
#foreach($image in $images)
//code for image
#end
Outputting GridFSDBFile type of objects in velocity template does not make sense. You should rather output the URL to the iamge, like
#foreach($imageId in $imageIds)
<img src="getImageFromGridFs?id=$imageId">
#end
and then create a getImageFromGridFs Servlet (or relevant Server Side code) to fetch the corresponding image and stream it to the Servlet's OutputStream.
After fetching the image from GridFs, You can access the image data in the Servlet using GridFSDBFile.getInputStream()
Hope this helps.

how to pass url from controller / model to view

I have a model that has a property like "SomeUrl", it's an absolute url with some parameters in it, something like this for example: http://www.someexternalsite.com/q?param1=value1&param2=value2
My view takes a List and I am trying to use these url's in an anchor tag like this:
my link
The url is being encoded and ends up coming out like this:
http://www.someexternalsite.com/q?param1=value1&param2=value
How do I stop it from doing that?
Not encoding the & would result in invalid html. In this case the html is correctly encoded. If you want to render a string not encoded use:
#Html.Raw(...)
Change your link to this:
my link

Resources