Tag Archives: Error interpreting JPEG image file (Not a JPEG file: starts with 0x89 0x50)

[Solved] Ubuntu Open jpg Image Error: Error interpreting JPEG image file (Not a JPEG file: starts with 0x89 0x50)

Question

The Java background receives a JPG format picture and finds that it can be opened and displayed normally under windows, but when it is opened under Ubuntu, it prompts error interpreting JPEG image file (not a JPEG file: starts with 0x89 0x50) . As shown in the following figure:

prompt that the file is not a JPEG image file, and the first two bytes of the file are 0x89 and 0x50 respectively.

Solution:

Via Wikipedia list_of_file_Signatures learned that the file signature at the beginning of these two bytes should be in PNG file format. Change the file suffix to PNG, and then open the file to display normally

for this reason, the logic for judging the file signature is added in the background interface to prevent the suffix submitted by the front end from being not the real format of the file (not limited to pictures).

For malicious files that deliberately forge file header information, the following logic cannot play a screening role.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class filetools
{
	public final static Map<String, String> FILE_TYPE_MAP = new HashMap<String, String>();

	static
	{
		getAllFileType();
	}

	private static void getAllFileType()
	{
		FILE_TYPE_MAP.put("jpg(JFIF)", "^FFD8FFE000104A4649460001"); // jpg JFIF file format
		FILE_TYPE_MAP.put("jpg(Exif)", "^FFD8FFE1.{4}457869660000"); // jpg Exif file format
		FILE_TYPE_MAP.put("jpeg", "^FFD8FFEE"); // jpeg
		FILE_TYPE_MAP.put("png", "^89504E470D0A1A0A"); // PNG (png)
		FILE_TYPE_MAP.put("bmp", "^424D"); // Windows Bitmap (bmp)
		FILE_TYPE_MAP.put("mp4", "^000000206674797069736F6D"); // ISO Base Media file (MPEG-4)
	}

	public final static String getFileByFile(File file)
	{
		String filetyp = null;
		byte[] fileheader = new byte[15]; 
		try
		{
			InputStream is = new FileInputStream(file);
			is.read(fileheader);
			filetyp = getFileTypeByStream(fileheader);
			is.close();
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return filetyp;
	}

	public final static String getFileTypeByStream(byte[] b)
	{
		String filetypeHex = String.valueOf(getFileHexString(b));
		Iterator<Entry<String, String>> entryiterator = FILE_TYPE_MAP.entrySet().iterator();
		while (entryiterator.hasNext())
		{
			Entry<String, String> entry = entryiterator.next();
			String fileTypeHexValue = entry.getValue();
			Pattern p = Pattern.compile(fileTypeHexValue);
			Matcher m = p.matcher(filetypeHex.toUpperCase());

			if (m.find())
			{
				if (0 == m.start()) 
					return entry.getKey();
			}
		}
		return "unknow type";
	}

	public final static String getFileHexString(byte[] b)
	{
		StringBuilder stringBuilder = new StringBuilder();
		if (b == null || b.length <= 0)
		{
			return null;
		}
		for (int i = 0; i < b.length; i++)
		{
			int v = b[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2)
			{
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}

	public static void main(String[] args)
	{
		File f = new File("/home/alderaan/111.jpg");
		if (f.exists())
		{
			String filetype = getFileByFile(f);
			System.out.println(filetype);
		} else
		{
			System.out.println(f.getPath() + " not found!");
		}
	}
}