BufferedImage到InputStream - 格式不同
I am trying to convert my BufferedImage to an Inputstream using the following:
我正在尝试使用以下内容将我的BufferedImage转换为Inputstream:
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.
我面临的问题是我的file.getContentType()将值返回为“image / jpeg”而不是例如“jpg”,因此显示了一个损坏的文件。
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.
有没有一个很好的方法将其转换为jpg(或使其可能接受image / jpeg)?我有许多其他文件格式(例如png),并认为执行某些逻辑来检查内容类型的switch语句是非常不必要的。
1 个解决方案
#1
.jpeg
and .jpg
are equivalent extensions - jpg
exists due to the historic requirements of Windows to have an 8.3 filename.extension format.
.jpeg和.jpg是等效的扩展 - 由于Windows的历史要求具有8.3 filename.extension格式,因此存在jpg。
To get the necessary extension, pull it out of the content type:
要获得必要的扩展,请将其从内容类型中删除:
final String contentType = file.getContentType();
contentType.substring(contentType.lastIndexOf("/") + 1)
更多相关文章
- Java:如何创建特定父类型的集合而不是其子类型?
- 线程“main”中的异常java.lang.RuntimeException:无法编译的源代
- 基于内容估计文本宽度的算法
- 了解数组类型和使用java.util.Arrays类
- 单击按钮后更改Frame的内容窗格
- Tinking in java枚举类型的自动贩售机
- Java怎么实现保存页面内容 导出为图片到本地
- 【读书笔记】JavaScript权威指南 第6版 (三)类型、值和变量
- RabbitMQ四种Exchange类型之Headers(Java)