Tech-Today
Encode image and display in html
To encode an image file(eg. png, jpg, gif) we will need the apache commons-codec library.
To avoid it in a maven project:
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
Encode a file:
public static String encodeFileToBase64Binary(File file) throws IOException {
String encodedFile = null;
try (FileInputStream fileInputStreamReader = new FileInputStream(file)) {
byte[] bytes = new byte[(int) file.length()];
fileInputStreamReader.read(bytes);
encodedFile = org.apache.commons.codec.binary.Base64.encodeBase64String(bytes);
}
return encodedFile;
}
Prepare the image:
String strImage = encodeFileToBase64Binary(new File(imagePath));
String imageExt = FilenameUtils.getExtension(imagePath);
imagePath = "data:image/" + imageExt + ";charset=utf-8;base64, " + strImage;
Now you can use imagePath in your HTML.
<img src="#{imagePath}" />
-
How To Download A File Using Rest Api
Normally I create an interface when defining a REST api, this is useful when testing using Arquillian. To define a download file api we must define the method below in the interface. @GET @Path("/downloadFile") ActionStatus downloadFile(@QueryParam("file")...
-
How To Zipped A File Or Directory In Java
There are times when we have to zipped a file or directory recursively in Java. This is how we do it: public static void zipFile(File file) throws IOException { byte[] buffer = new byte[1024]; FileOutputStream fos = new FileOutputStream( file.getParent()...
-
How To Implement An Image Streamer For Primefaces Graphicimage Component
This page provide code samples for using primefaces graphic image component. In this case we are assuming that the byte data of an image is stored in an entity and that entity is access by its id. See the param id in #1. The param id is read by the streamer...
-
How To Automate Typing In Tinymce Using Selenium Java
Prerequisites: 1.) selenium-java 2.) selenium-firefox-driver 3.) selenium-chrome-driver 4.) chromedriver - http://code.google.com/p/chromedriver/downloads/list Currently I'm working on a a test case that needs to...
-
Download A Url File Into Desktop. For Example, Download Image File (gif, Jpg) Using Java And Apache Commons Io Api
To make this code works you need: 1.) http://commons.apache.org/io/download_io.cgi 2.) make sure to configure your build path and include the apache commons io jar (I've used eclipse-java ide) 3.) the url that will be save are stored in the input/input.txt...
Tech-Today