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)

更多相关文章

  1. Java:如何创建特定父类型的集合而不是其子类型?
  2. 线程“main”中的异常java.lang.RuntimeException:无法编译的源代
  3. 基于内容估计文本宽度的算法
  4. 了解数组类型和使用java.util.Arrays类
  5. 单击按钮后更改Frame的内容窗格
  6. Tinking in java枚举类型的自动贩售机
  7. Java怎么实现保存页面内容 导出为图片到本地
  8. 【读书笔记】JavaScript权威指南 第6版 (三)类型、值和变量
  9. RabbitMQ四种Exchange类型之Headers(Java)

随机推荐

  1. Java&Xml教程(七)使用JDOM修改XML文件内容
  2. 疯狂XML学习笔记(11)-----------XSLT讲解
  3. Java&Xml教程(六)使用JDOM解析XML文件
  4. 疯狂XML学习笔记(10)---------XML的作用
  5. Java&Xml教程(五)使用SAX方式解析XML文件
  6. 疯狂XML学习笔记(9)-------------Schema内
  7. Java&Xml教程(四)使用DOM方式生成XML文件
  8. 疯狂XML学习笔记(8)---------schema 的简单
  9. Java&Xml教程(三)使用DOM方式修改XML文件内
  10. 疯狂XML学习笔记(7)-----------XML Schema