【问题标题】:"mimetype: application" display on a picture instead of "mimetype: image" after a ImageIO.write在 ImageIO.write 之后,“mimetype: application” 显示在图片上,而不是“mimetype: image”
【发布时间】:2013-04-04 18:02:36
【问题描述】:

当我像这样简单地从另一个创建新图像时:

  public static void scaleByTwoRight(String src, String dest)
    throws IOException {
           BufferedImage bsrc = ImageIO.read(new File(src));
           int width          = bsrc.getWidth()/2;
           int height         = bsrc.getHeight();
           BufferedImage bdest = bsrc.getSubimage(width, 0, width, height);
           ImageIO.write(bdest,"PNG",new File(dest));
  }

源文件 (src) = C:...\Manga\Shonan Juna_ Gumi Tome 11\Shonan Junaï Gumi Tome 11 - 091B.png 目标文件 (dest) = C:...\Manga\Shonan Junaï Gumi Tome 11 - 091B_A.png

生成文件示例:https://docs.google.com/file/d/0B1vKCZzB5hxqYzNsUWF5RHA2Wm8/edit?usp=sharing

问题:新图像具有 mimetype: application 而不是 mimetype: image

我是如何得出这个结论的:我正在使用一个函数来测试文件是否为图像:

public static boolean isImage(String src)
    throws IOException {
          File f = new File(src);
          String mimetype= new MimetypesFileTypeMap().getContentType(f);
          String type = mimetype.split("/")[0];
          if(type.equals("image")){
              return true;
          }else{ 
              System.out.println("mimetype: "+type);
              return false;
          }
  } 

如果 Mime 类型不正确,影响不大,但我更喜欢让它正常工作..

感谢您的帮助!

注意: 我在 Windows 7 / 32b 下运行 JVM 1.7 / Eclipse Helios

【问题讨论】:

  • String dest dest 的值是多少?请以SSCCE 的形式回答。
  • Function : scaleByTwoRight(String src, String dest) 源文件 (src) = C:\...\Manga\Shonan Juna_ Gumi Tome 11\Shonan Junaï Gumi Tome 11 - 091B.png 目标文件(dest) = C:\...\Manga\Shonan Junaï Gumi Tome 11 - 091B_A.png
  • 请查找运行 scaleByTwoRight 函数后创建的目标(图像)示例:docs.google.com/file/d/0B1vKCZzB5hxqYzNsUWF5RHA2Wm8/…
  • 1) 不要将代码放在 cmets 或链接中,直接将它们添加为 edit to the question 2) 为了尽快获得更好的帮助,请发布 SSCCE

标签: java image file-io mime-types


【解决方案1】:

您的代码在我的机器上运行良好。 我有 Windows XP,32 位, 尝试使用 jpeg 图像,它仅将 mimetype 作为图像/jpeg 返回。 希望您不要尝试同时执行这两个功能。 目标文件名也应包含正确的扩展名,如 .jpeg 或。 png 等...

【讨论】:

  • 嗨 Mitpatoliya,当我从资源管理器中检查时,它看起来是正确的 > 文件 > 项目类型 > PNG 图像。但是如果我运行函数 isImage(..) 它会返回 Application 类型。我认为我必须在创建文件或类似的东西时手动创建 mime 类型。rgagnon.com/javadetails/java-0487.html
  • 你经过的目标路径是什么?
  • “尝试使用 jpeg 图像..” 当代码 sn-ps 显然设置为创建 PNG 时为什么要使用 JPEG?
  • 有多种方法可以从文件中获取 mimetypes。它们中的大多数使用文件扩展名从 map 中找出相对 mimetypes。因此,如果您没有具有适当扩展名的文件,那么您将没有得到正确的 mimetype 主要是我的意思。
猜你喜欢
  • 1970-01-01
  • 2013-03-21
  • 1970-01-01
  • 2016-06-08
  • 1970-01-01
  • 2014-04-09
  • 2020-09-19
  • 2011-05-08
  • 2019-03-22
相关资源
最近更新 更多