I/O error reading PNG header!错误的一种可能原因
2017-12-12 17:33
309 查看
故名思意,该错误的原因主要为图片操作时搞错了图片类型,比如说把JPG图片重命名成PNG文件也能正常显示,但是如果操作这个伪PNG文件时就会触发此错误,一般来说这个错误的出现率并不高,比较稳妥的解决方案是在操作图片前先手动确认一下类型啦,这个已经有其他博主说了,一时找不到就不上链接了,代码为
public static String getFileExt(String filePath) {
FileInputStream fis = null;
String extension = FilenameUtils.getExtension(filePath);
try {
fis = new FileInputStream(new File(filePath));
byte[] bs = new byte[1];
fis.read(bs);
String type = Integer.toHexString(bs[0]&0xFF);
if(JPG_HEX.equals(type))
extension = JPG_EXT;
if(PNG_HEX.equals(type))
extension = PNG_EXT;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return extension;
}
private static final String JPG_HEX = "ff";
private static final String PNG_HEX = "89";
private static final String JPG_EXT = "jpg";
private static final String PNG_EXT = "png";
我之前碰到问题的时候也觉得是这个原因,但是明明自己操作的就是PNG文件啊,困扰了半天之后发现,.如果对一个图像文件进行裁剪操作时保存到同样的图片路径里时,也会触发此问题,这个时候明明是PNG图片却会报非PNG图片的错,相当的具有误导性啊
public static String getFileExt(String filePath) {
FileInputStream fis = null;
String extension = FilenameUtils.getExtension(filePath);
try {
fis = new FileInputStream(new File(filePath));
byte[] bs = new byte[1];
fis.read(bs);
String type = Integer.toHexString(bs[0]&0xFF);
if(JPG_HEX.equals(type))
extension = JPG_EXT;
if(PNG_HEX.equals(type))
extension = PNG_EXT;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return extension;
}
private static final String JPG_HEX = "ff";
private static final String PNG_HEX = "89";
private static final String JPG_EXT = "jpg";
private static final String PNG_EXT = "png";
我之前碰到问题的时候也觉得是这个原因,但是明明自己操作的就是PNG文件啊,困扰了半天之后发现,.如果对一个图像文件进行裁剪操作时保存到同样的图片路径里时,也会触发此问题,这个时候明明是PNG图片却会报非PNG图片的错,相当的具有误导性啊
相关文章推荐
- vs2008 error 1012错误的一种可能原因。
- SVN出现错误“error running context: an error occurred during ssl communication”的一种可能的解决方法
- Kafka错误2_ERROR Unexpected exception, exiting abnormally异常的一种原因
- boa 服务器 错误 cgi_header: unable to find LFLF错误的一种可能
- “段错误 (核心已转储) ”一种可能原因及其解决方法
- I/O error reading PNG header! java截取png图片时遇到的问题
- error at ::0 can't find referenced pointcut..可能出现错误的原因
- error at ::0 can't find referenced pointcut..可能出现错误的原因
- AAPT err(Facade for): libpng error: Not a PNG file 错误解决,错误的原因就是drawable下的文件不合法
- conversion to dalvik format failed with error 1的一种可能原因
- 安装mysql的1067错误的可能原因:Fatal error: Can't open privilege tables: Table mysql.user doesn't exist
- (Microsoft SQL Server, Error: 1326)错误原因的一种
- MFC 出现error LNK2005错误的一种原因和解决办法
- 关于Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors错误的原因
- Silverlight调用WebService出现CommunicationException The remote server returned an error: NotFound的一种可能原因
- 发现ORA-06508错误的一种新原因及解决办法
- The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.报该错误的一种原因。
- PHP 中 syntax error, unexpected $end 错误的一种原因及解决
- error while loading shared libraries: xxx.so.x"错误的原因和解决办法 [转]
- loadFileSystems error & ExceptionUtils错误原因分析