1。問題の説明
プロジェクト開発中、私たちはしばしばファイルをアップロードする問題に遭遇します。これは、取得する画像の形式です。多くの場合、以下に示すように、多くの人が接尾辞名を使用して判断します。
if(filename.endswith( "。png")|| filename.endswith( "。jpg")){//画像を保存} else {throw new ioException( "error file format!");}ただし、この方法は非常に信頼できません。 ZIPファイル、RMVBファイル、CSS、JSをアップロードして、接尾辞名JPGまたはPNGを変更するか、サーバーにアップロードするため、サーバーにダーティデータが表示されます。さらに、一部の画像ファイルの場合、それらが間違った拡張機能に変更されている場合、一部のブラウザが画像を表示できない場合があります。
2。解決策
コンピューターシステムでは、メディアタイプファイルに[識別子]があり、zipと写真自体がメディアファイルに属しているため、エンコードとデコードを通じて写真が合法かどうかを判断できます。
1。審査とマーキングの方法
private static boolean isbmp(byte [] buf){byte [] markbuf = "bm" .getBytes(); // BMPイメージファイルの最初の2バイトは、return Compare(buf、markbuf); } private static boolean isicon(byte [] buf){byte [] markbuf = {0、0、1、0、1、0、32、32}; return比較(buf、markbuf); } private static boolean iswebp(byte [] buf){byte [] markbuf = "riff" .getBytes(); // webp画像識別子return比較(buf、markbuf); } private static boolean isgif(byte [] buf){byte [] markbuf = "gif89a" .getBytes(); // gif識別子if(compare(buf、markbuf)){return true; } markbuf = "gif87a" .getBytes(); // gif識別子if(compare(buf、markbuf)){return true; } falseを返します。 } private static boolean ispng(byte [] buf){byte [] markbuf = {(byte)0x89,0x50,0x4e、0x47,0x0d、0x0a、0x1a、0x0a}; // png識別子// new String(buf).indexof( "png")> 0 //この方法は、比較(buf、markbuf)を返すためにも使用できます。 } private static boolean isjpegheader(byte [] buf){byte [] markbuf = {(byte)0xff、(byte)0xd8}; // jpeg開始文字復帰比較(buf、markbuf); } private static boolean isjpegfooter(byte [] buf)// jpeg終了文字{byte [] markbuf = {(byte)0xff、(byte)0xd9}; return比較(buf、markbuf); }2。コアメソッド
/ ** *ファイルのMimetypeを取得 * @param filename * @return */ private static string getmimeType(string filename){try {string mimeType = readType(filename); return string.format( "image/%s"、mimetype); } catch(ioexception e){e.printstacktrace(); } nullを返します。 } / ** *ファイルタイプの読み取り * @param filename * @return * @throws ioexception * / private static string readtype(string filename)throws ioexception {fileinputStream fis = null; try {file f = new file(filename); if(!f.exists()|| f.isdirectory()|| f.length()<8){throw new ioException( "file ["+f.getabsolutepath()+"]は画像ではありません!"); } fis = new FileInputStream(f); byte [] bufheaders = readinputStreamat(fis、0,8); if(isjpegheader(bufheaders)){long skiplength = f.length()-2-8; //初めて8バイトを読んだとき、バイト[] 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"; } new IoException( "画像の形式はunkown!")をスローします。 } catch(filenotfoundexception e){throw e; }最後に{try {if(fis!= null)fis.close(); } catch(Exception e){}}} / ***マーク一貫性の比較* @param bufマーク検出* @param markbuf識別子バイト配列* @return return false mark sign mismatch* / private static boolean compare(byte [] buf、byte [] markbuf){int i = 0; i <0; i <0; i <0; i < Markbuf [i];バイトa = buf [i]; if(a!= b){return false; }} trueを返します。 } / **** @param fis inputストリームオブジェクト* @param Skiplength skip position length* @param長さ* @return byte array* @throws ioexception* / private static byte [] readinputStream fis、long skiplength、int length)Throws ioexception fis.skip(Skiplength); // int read = fis.read(buf、0、length); bufを返します。 }3.テストコード
通常のテスト
public class imageType {public static void main(string [] args){string filename = "oschina.jpg";文字列型= getmimeType(filename); System.out.println(type); }}出力
画像/jpeg
拡張テストを変更します
oschina.jpegをOschina.pngに変更します
extensionを削除するためのCopy oschina.png
public class imageType {public static void main(string [] args){string filename = "oschina.png";文字列型= getmimeType(filename); System.out.println(type); filename = "oschina"; type = getmimeType(filename); System.out.println(type); }}出力
画像/jpeg
画像/jpeg
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。