I have a base64 string like below
String base64string ="Suhnagsksbsgshsbsbshsushvshs.....etc;
From this I need to find a image type because I will get tiff format images also, so I cannot display tif format on chrome browser so I need to find out which format it is and then if it's a tiff I need to convert it to jpg then display at front end.
For this,
I'm storing base64 decoded string as a jpg file in temp directory.
I want to read the file and get original image format from the same
if it's tiff format I need to convert it to jpg.
Currently I'm facing error at second point
Find the below code which I have tried but not able to get the image format.
byte[] decoded = op.createSignature(encryptionKey,sign[1]);
String tempfolder = System.getProperty("java.io.tmpdir");
System.out.println("tempfolder :"+tempfolder);
String tmpfilepath = tempfolder+""+new Date().getTime()+".jpeg";
System.out.println("tmpfilepath :"+tmpfilepath);
File myfile = new File(tmpfilepath);
FileOutputStream stream = new FileOutputStream(myfile);
stream.write(decoded);
stream.close();
try {
ImageInputStream iis= ImageIO.createImageInputStream(myfile);
Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
while(iter.hasNext()){
ImageReader reader = iter.next(); //throw new RuntimeException("no readers found");
String formatName = reader.getFormatName();
System.out.println("image format :"+formatName);
}
iis.close();
}
From this code, iterator doesn't finding any data from image and stored image size is 12kb and not able to open manually aslo as showing cannot read this file error( I think it will not open)
If I throw exception it is showing as nosuchelementexception
I have tried to read sample original image file by passing file path to imageinputstream
I have got original file format but from base64 byte array not able to get the same.
Please suggest me to change anything in this code and I don't understand why it's not able to get image format from base64 byte array.
I'm using Java 1.7.