1. Deskripsi Masalah
Selama pengembangan proyek, kita sering menghadapi masalah mengunggah file, yang merupakan format gambar yang akan diperoleh. Dalam banyak kasus, banyak orang menggunakan nama akhiran untuk menilai, seperti yang ditunjukkan di bawah ini.
if (filename.endswith (". png") || filename.endswith (". jpg")) {// simpan gambar} else {lempar ioException baru ("Format file kesalahan!");}Namun, metode ini sangat tidak dapat diandalkan. Kami dapat mencoba mengunggah file zip, file RMVB, CSS, JS untuk memodifikasi nama akhiran JPG atau PNG, atau mengunggahnya ke server, yang menyebabkan data kotor muncul di server kami. Selain itu, untuk beberapa file gambar, jika dimodifikasi ke ekstensi yang salah, beberapa browser mungkin tidak dapat menampilkan gambar.
2. Solusi
Dalam sistem komputer, file jenis media memiliki [pengidentifikasi], dan zip dan gambar itu sendiri milik file media, sehingga kami dapat menentukan apakah gambar tersebut legal melalui pengkodean dan decoding.
1. Metode menilai dan menandai
private static boolean isBMP (byte [] buf) {byte [] markbuf = "bm" .getbytes (); // Dua byte pertama dari file gambar BMP Return Return (buf, markbuf); } private static boolean isicon (byte [] buf) {byte [] markbuf = {0, 0, 1, 0, 1, 0, 32, 32}; return compare (buf, markbuf); } private static boolean isWebp (byte [] buf) {byte [] markbuf = "riff" .getbytes (); // Webp Identifier Return Return Compare (BUF, MarkBUF); } private static boolean isgif (byte [] buf) {byte [] markbuf = "gif89a" .getbytes (); // GIF Identifier if (bandingkan (buf, markbuf)) {return true; } markbuf = "gif87a" .getbytes (); // GIF Identifier if (bandingkan (buf, markbuf)) {return true; } return false; } private static boolean ispng (byte [] buf) {byte [] markbuf = {(byte) 0x89,0x50,0x4e, 0x47,0x0D, 0x0a, 0x1a, 0x0a}; // PNG Identifier // String baru (BUF) .Indexof ("png")> 0 // Metode ini juga dapat digunakan untuk mengembalikan perbandingan (buf, markbuf); } private static boolean isjpegheader (byte [] buf) {byte [] markbuf = {(byte) 0xff, (byte) 0xd8}; // jpeg start character return return (buf, markbuf); } private static boolean isjpegfooter (byte [] buf) // jpeg ending character {byte [] markbuf = {(byte) 0xff, (byte) 0xd9}; return compare (buf, markbuf); }2. Metode inti
/ ** * Dapatkan mimetype file * @param fileName * @return */ private static string getMimetype (string fileName) {coba {string mimetype = readType (fileName); return string.format ("Image/%s", mimetype); } catch (ioException e) {e.printstacktrace (); } return null; } / ** * Baca jenis file * @param fileName * @return * @throws IoException * / Private Static String ReadType (String FileName) melempar IoException {FileInputStream fis = null; coba {file f = file baru (nama file); if (! f.exists () || f.isdirectory () || f.length () <8) {lempar ioException baru ("File ["+f.getAbsolutePath ()+"] bukan gambar!"); } fis = FileInputStream baru (f); byte [] bufheaders = readInputStreAMat (fis, 0,8); if (isjpegheader (bufheaders)) {long skiplength = f.length ()-2-8; // Pertama kali saya membaca 8 byte, jadi saya perlu mengurangi byte [] Buffooters = ReadInputStreAMat (FIS, Skiplength, 2); if (isjpegfooter (Buffooters)) {return "jpeg"; }} if (ispng (bufheaders)) {return "png"; } if (isGif (bufheaders)) {return "gif"; } if (isWebp (bufheaders)) {return "Webp"; } if (isBmp (bufheaders)) {return "bmp"; } if (isicon (bufheaders)) {return "ico"; } lempar ioException baru ("Format gambar tidak diketahui!"); } catch (FileNotFoundException e) {throw e; } akhirnya {coba {if (fis! = null) fis.close (); } catch (Exception e) { } } } /** * Mark consistency comparison* @param buf Mark to be detected* @param markBuf Identifier byte array* @return Return false Mark sign mismatch*/ private static boolean compare(byte[] buf, byte[] markBuf) { for (int i = 0; i < markBuf.length; i++) { byte b = Markbuf [i]; byte a = buf [i]; if (a! = b) {return false; }} return true; } /** * * @param fis Input stream object* @param skiplength Skip position length* @param length Length to read* @return Byte array* @throws IOException */ private static byte[] readInputStreamAt(FileInputStream fis, long skiplength, int length) throws IOException { byte[] buf = new byte[length]; fis.skip (skiplength); // int read = fis.read (buf, 0, length); return buf; }3. Kode Uji
Tes normal
kelas publik imagetype {public static void main (string [] args) {string filename = "oschina.jpg"; String type = getMimetype (fileName); System.out.println (type); }} Keluaran
Gambar/JPEG
Ubah tes ekstensi
① Ubah oschina.jpeg menjadi oschina.png
②Copy oschina.png untuk menghapus ekstensi
kelas publik imagetype {public static void main (string [] args) {string filename = "oschina.png"; String type = getMimetype (fileName); System.out.println (type); FileName = "Oschina"; type = getMimetype (nama file); System.out.println (type); }} Keluaran
Gambar/JPEG
Gambar/JPEG
Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.