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!");
}
}
}
Read More:
- [Solved] Linux installation pillow error: ValueError: jpeg is required unless explicitly disabled using –disable-jpeg, aborting
- [Solved] Docker failed to delete image error: no such image: CentOS
- Linux Ubuntu ImportError: Libtk8.5.so: cannot open shared object file:No such file Install tkinter Library
- [Solved] Error: X_LINK_COMMUNICATION_NOT_OPEN or X_LINK_ERROR
- [Solved] Ubuntu Compile AOSP Error: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
- [Solved] ubuntu makefile Cross-compilation error: file not recognized: file format not recognized
- [Solved] ××: error while loading shared libraries: ××.so.19: cannot open shared object file: No such file or directory
- [Solved] Python Import mmcv Warning: ImportError: libGL.so.1: cannot open shared object file: No such file or directory
- Read and write BMP image with Pure C language
- How to open X Display on the server side (locally operable remote interface)
- Error: loading shared libraries: cannot open shared object file: No such file or directory
- Windows11 Install Ubuntu Error: WslRegisterDistribution failed with error: 0x800701bc
- [Solved] error while loading shared libraries: libjson.so.0: cannot open shared object file: No such file or
- [Solved] /usr/local/libexec/mecab/mecab-dict-index: error while loading shared libraries: libmecab.so.2: cannot open shared object file: No such file or directory
- Petalinux Failed to open PetaLinux lib: librdi_commonxillic.so: cannot open shared object file:
- [Solved] Ubuntu pyinstaller Error when packaging executable file: … qt.qpa.plugin: Could not find the Qt platform plugin “xcb“ in “…
- [Ubuntu] How to Solve dpkg Error: dpkg: error: failed to open package info file ‘/usr/local/var/lib/dpkg/status’ for reading: No such file or directory
- Initializing the Kubernetes master node ERROR: failed to pull image registry.aliyuncs.com/google_containers/coredns:v1.8.0
- Ubuntu: Redis starts the project Error [How to Solve]
- How to Solve Docker delete container image error: Error response from daemon: conflict: unable to delete 7cc1942f1ed5 (must be forced)