Reputation: 833
I am trying to convert my BufferedImage to an Inputstream using the following:
BufferedImage bi = ImageIO.read(file.getInputStream());
bi = Scalr.resize(bi, 300);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bi, file.getContentType(), os); //the content type is specified as image/jpeg
The problem I am facing is that my file.getContentType()
returns the value as "image/jpeg" instead of e.g. "jpg", and thus shows a corrupt file.
Is there a good way to convert this to jpg (or make it possibly accept image/jpeg)? I have many other file formats (png for instance) and think that a switch statement which does some logic to check what the content type is would be quite unnecessary.
Upvotes: 1
Views: 1840
Reputation: 8202
.jpeg
and .jpg
are equivalent extensions - jpg
exists due to the historic requirements of Windows to have an 8.3 filename.extension format.
To get the necessary extension, pull it out of the content type:
final String contentType = file.getContentType();
contentType.substring(contentType.lastIndexOf("/") + 1)
Upvotes: 2