javax.imageio.IIOException: Not a JPEG file: starts with 0x47 0x49
来源:互联网 发布:js函数必须有返回值 编辑:程序博客网 时间:2024/06/05 16:46
java处理图片时出现异常
javax.imageio.IIOException: Not a JPEG file: starts with 0x47 0x49
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImageHeader(Native Method)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readNativeHeader(Unknown Source)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.checkTablesOnly(Unknown Source)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.gotoImage(Unknown Source)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readHeader(Unknown Source)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.getWidth(Unknown Source)
at com.test.filedownloader.ImageHandler.getImageDim(ImageHandler.java:60)
at com.test.filedownloader.ImageHandler.main(ImageHandler.java:96)
Exception in thread "main" java.lang.NullPointerException
at com.test.filedownloader.ImageHandler.getImageDim(ImageHandler.java:70)
at com.test.filedownloader.ImageHandler.main(ImageHandler.java:96)
出现这种错误是因为将gif 图片后缀改成了jpg
解决办法:
根据文件的前面几个字节,判断文件类型
private String getFileSuffix(final String path) throws IOException {String result = "";String hex="";if (path != null) {File image=new File(path);InputStream is = new FileInputStream(image);byte[] bt = new byte[2]; is.read(bt); MyLog.logger.info(bt+"\n"+bytesToHexString(bt)); hex=bytesToHexString(bt);is.close();if(hex.equals("ffd8")){result="jpg";}else if(hex.equals("4749")){result="gif";}else if(hex.equals("8950")){result="png";}}return result;}
public static String bytesToHexString(byte[] src) { StringBuilder stringBuilder = new StringBuilder(); if (src == null || src.length <= 0) { return null; } for (int i = 0; i < src.length; i++) { int v = src[i] & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } return stringBuilder.toString(); }
调用的话就
String suffix = getFileSuffix(path);
一个png文件十六进制内容
可参看链接
http://www.acgist.com/article/134.html
http://blog.csdn.net/fenglibing/article/details/7728275
http://www.cnblogs.com/Wendy_Yu/archive/2011/12/27/2303118.html
- javax.imageio.IIOException: Not a JPEG file: starts with 0x47 0x49
- javax.imageio.IIOException: Not a JPEG file: starts with 0x89 0x50
- Error interpreting JPEG image file (Not a JPEG file: starts with 0x89 0x50)
- Not a JPEG file: starts with 0x89 0x50問題
- tensorflow--Not a JPEG file: starts with 0x89 0x50
- javax.imageio.IIOException: Can't read input file!
- javax.imageio.IIOException: Can't read input file!
- 读取验证码时 Caused by: javax.imageio.IIOException: Can't create cache file!
- 解决:Tomcat不能加载图片问题.javax.imageio.IIOException: Can't create cache file!
- 读取验证码时报错 Caused by: javax.imageio.IIOException: Can't create cache file!
- javax.imageio.IIOException: Can't create output stream!
- javax.imageio.IIOException: Can't create output stream!
- javax.imageio.IIOException: Invalid icc profile: bad sequence number
- 关于javax.imageio.IIOException: Invalid icc profile: bad sequence number
- javax.imageio.IIOException: Can't create output stream!
- javax.imageio.IIOException: Can't create output stream!
- 图片处理 javax.imageio.IIOException: Unsupported Image Type
- javax.imageio.IIOException: Can't create output stream解决方法
- 黑马程序员 多线程
- Eclipse中将一个android项目当做另外一个android项目的library
- [leetcode-206]Reverse Linked List(c)
- MFC给Static Text赋值
- 数学模板 - 组合数学
- javax.imageio.IIOException: Not a JPEG file: starts with 0x47 0x49
- 设计模式-迭代器模式
- 1000以内不能被7整除的数之和
- Unity编程笔录--Unity3d获得android和ios设备的唯一标识
- 堆和栈的区别(转过无数次的文章)
- 比喻:进程和线程
- 我在工作中遇到的css问题汇总
- Java设计模式 -- Singleton模式
- html的表结构