eSolutionsGroupAS
asked on
Converting image into Base64
I need to fetch an image from a specific location on the server, then transfer it to a Base64 string, and insert the string into an XML.
What i basically need help with, is how to get the image from a location, and how to encode it to Base64 (preferably using org.apache.commons.codec.b inary.Base 64 for the encoding part.)
What i basically need help with, is how to get the image from a location, and how to encode it to Base64 (preferably using org.apache.commons.codec.b
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
> Try this
>
> public static String imageUrlToBase64(String url) {
> byte[] buffer = null;
> try {
> URLConnection conn = new URL(url).openConnection();
> buffer = new byte[conn.getContentLength ()];
> DataInputStream in = new DataInputStream(conn.getIn putStream( ));
> in.readFully(buffer);
> } catch (Exception e) {
> e.printStackTrace();
> return null;
> }
> return new String(new Base64().decode(buffer));
> }
Thanx, I'll try this. :-)
Another question though: I also need to extract the file extension from the file. I need to forward either "GIF" og "JPG" in the XML file.
>
> public static String imageUrlToBase64(String url) {
> byte[] buffer = null;
> try {
> URLConnection conn = new URL(url).openConnection();
> buffer = new byte[conn.getContentLength
> DataInputStream in = new DataInputStream(conn.getIn
> in.readFully(buffer);
> } catch (Exception e) {
> e.printStackTrace();
> return null;
> }
> return new String(new Base64().decode(buffer));
> }
Thanx, I'll try this. :-)
Another question though: I also need to extract the file extension from the file. I need to forward either "GIF" og "JPG" in the XML file.
>>I also need to extract the file extension from the file
Well you know that anyway don't you? - it's in the url ;-)
Well you know that anyway don't you? - it's in the url ;-)
ASKER
Hehe. I'm afraid it's not that easy.
The images are typically named with a customer's unique id,
and it is from this ID I will retrieve the right image. And since
I have no control over whether the images are of type gif or jpg,
I need to find out somehow. I hope this is possible though. :)
So the run is like this:
My method reaches into a specific folder, and finds an image based on
comparing the customer's ID (which the method already has) with the
name of the image. (The images are placed in the folder by someone else)
The whole image is to be converted into a Base64-String
(the code you provided above I hope does this part)
Then, the String is inserted into an XML file along with the file extension.
(The file extension I don't know how to retrieve...)
The images are typically named with a customer's unique id,
and it is from this ID I will retrieve the right image. And since
I have no control over whether the images are of type gif or jpg,
I need to find out somehow. I hope this is possible though. :)
So the run is like this:
My method reaches into a specific folder, and finds an image based on
comparing the customer's ID (which the method already has) with the
name of the image. (The images are placed in the folder by someone else)
The whole image is to be converted into a Base64-String
(the code you provided above I hope does this part)
Then, the String is inserted into an XML file along with the file extension.
(The file extension I don't know how to retrieve...)
You mean the url doesn't have the right extension?
ASKER
All I have is a location on my server. For example "c:\newaccounts\images\"
I must iterate all the files in that location and compare their names to an account
id that I get from somewhere else, and then retreive the right file. And then go on with
the encoding and so on.
I must iterate all the files in that location and compare their names to an account
id that I get from somewhere else, and then retreive the right file. And then go on with
the encoding and so on.
If you're on the same machine:
File imageDirectory = new File("c:/newaccounts/image s");
File[] images = imageDirectory.list(new FileFilter() {
public boolean accept(File f) {
String name = f.getName().toLowerCase();
return name.endsWith("gif") || name.endsWith("jpg"); // (More if necessary)
}
});
then adapt my code to
buffer = new byte[(int)f.length()];
starting from the old
>>buffer = new byte[conn.getContentLength ()];
File imageDirectory = new File("c:/newaccounts/image
File[] images = imageDirectory.list(new FileFilter() {
public boolean accept(File f) {
String name = f.getName().toLowerCase();
return name.endsWith("gif") || name.endsWith("jpg"); // (More if necessary)
}
});
then adapt my code to
buffer = new byte[(int)f.length()];
starting from the old
>>buffer = new byte[conn.getContentLength
>>File[] images = imageDirectory.list
should have been
File[] images = imageDirectory.listFiles
should have been
File[] images = imageDirectory.listFiles
If it's not on the same machine, but visible on your network, you should be able to do
File imageDirectory = new File("\\\\machine_name\\sh arename");
File imageDirectory = new File("\\\\machine_name\\sh
i have an easier approach to recognize for gif or jpg
open any GIF wth editor and find out that they all start with GIF
so:
byte [] giftest = "GIF".getBytes("UTF8");
public static String imageUrlToBase64(String url) {
> byte[] buffer = null;
> try {
> URLConnection conn = new URL(url).openConnection();
> buffer = new byte[conn.getContentLength ()];
> DataInputStream in = new DataInputStream(conn.getIn putStream( ));
> in.readFully(buffer);
> } catch (Exception e) {
> e.printStackTrace();
> return null;
> }
> return new String(new Base64().decode(buffer));
> }
//compare giftest to bufer
int i = 0;
while (i < giftest.length){
if (giftest[i] == buffer[i]) i++;
}
if (i ==3 )==> gif else jpg
for details on magicnumbers see /etc/magic or linux file command
open any GIF wth editor and find out that they all start with GIF
so:
byte [] giftest = "GIF".getBytes("UTF8");
public static String imageUrlToBase64(String url) {
> byte[] buffer = null;
> try {
> URLConnection conn = new URL(url).openConnection();
> buffer = new byte[conn.getContentLength
> DataInputStream in = new DataInputStream(conn.getIn
> in.readFully(buffer);
> } catch (Exception e) {
> e.printStackTrace();
> return null;
> }
> return new String(new Base64().decode(buffer));
> }
//compare giftest to bufer
int i = 0;
while (i < giftest.length){
if (giftest[i] == buffer[i]) i++;
}
if (i ==3 )==> gif else jpg
for details on magicnumbers see /etc/magic or linux file command
btw
>> return new String(new Base64().decode(buffer));
should be return new String(new Base64().encode(buffer));
>> return new String(new Base64().decode(buffer));
should be return new String(new Base64().encode(buffer));
Good catch ;-)
ASKER
wow! Thanks!
It'll take some time before I get to this part, but
I'm looking forward to try this on :)
It'll take some time before I get to this part, but
I'm looking forward to try this on :)
not even an assist....
;-(
;-(
:-)
TO load an image :
java.net.URL imageURL = myDemo.class.getResource("
...
if (imageURL != null) {
ImageIcon icon = newImageIcon(imageURL);
}
to convert to Base64:
https://www.experts-exchange.com/questions/20844229/How-to-convert-java-awt-image-BufferedImage-image-to-be-encoded-base64.html?query=Converting+image+into+Base64&topics=86