「Java」ファイルのマジックバイト(前頭最初5バイト)でpdfファイルを判定するサンプル
pdfのマジックバイト
%PDF- 25 50 44 46 2d
説明
1.public FileInputStream(String name)throws FileNotFoundException
ファイル・システム内のパス名nameで指定される実際のファイルへの接続を開くことにより、FileInputStreamを作成します。
2.public FileInputStream(String name)throws FileNotFoundException
ファイル・システム内のパス名nameで指定される実際のファイルへの接続を開くことにより、FileInputStreamを作成します。
Javaコード
package com.arkgame.study.file;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class PdfFileHannten {
public static int SIZE = 5;
//マジックバイトの配列の定義
public static byte pdfByte[] = { '%', 'P', 'D', 'F','-' };
public static void main(String[] args)
throws IOException {
String strFile = "c:\\data\\test001.pdf";
boolean isFlg = chkPdfFile(strFile);
System.out.println("指定ファイルがpdfファイルか: "+isFlg);
}
@SuppressWarnings("resource")
public static boolean chkPdfFile(String strFile)
throws FileNotFoundException,IOException {
boolean result = true;
InputStream ist = null;
ist = new FileInputStream(strFile);
byte[] cft = new byte[SIZE];
ist.read(cft);
String resStr = new String(cft, "US-ASCII");
System.out.println("byte配列->ASCIIコードの要素: " + resStr);
//マジックバイト判定
for (int i = 0; i < pdfByte.length; i++) {
if (cft[i] != pdfByte[i]) {
result = false;
}
}
return result;
}
}
package com.arkgame.study.file;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class PdfFileHannten {
public static int SIZE = 5;
//マジックバイトの配列の定義
public static byte pdfByte[] = { '%', 'P', 'D', 'F','-' };
public static void main(String[] args)
throws IOException {
String strFile = "c:\\data\\test001.pdf";
boolean isFlg = chkPdfFile(strFile);
System.out.println("指定ファイルがpdfファイルか: "+isFlg);
}
@SuppressWarnings("resource")
public static boolean chkPdfFile(String strFile)
throws FileNotFoundException,IOException {
boolean result = true;
InputStream ist = null;
ist = new FileInputStream(strFile);
byte[] cft = new byte[SIZE];
ist.read(cft);
String resStr = new String(cft, "US-ASCII");
System.out.println("byte配列->ASCIIコードの要素: " + resStr);
//マジックバイト判定
for (int i = 0; i < pdfByte.length; i++) {
if (cft[i] != pdfByte[i]) {
result = false;
}
}
return result;
}
}
package com.arkgame.study.file; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; public class PdfFileHannten { public static int SIZE = 5; //マジックバイトの配列の定義 public static byte pdfByte[] = { '%', 'P', 'D', 'F','-' }; public static void main(String[] args) throws IOException { String strFile = "c:\\data\\test001.pdf"; boolean isFlg = chkPdfFile(strFile); System.out.println("指定ファイルがpdfファイルか: "+isFlg); } @SuppressWarnings("resource") public static boolean chkPdfFile(String strFile) throws FileNotFoundException,IOException { boolean result = true; InputStream ist = null; ist = new FileInputStream(strFile); byte[] cft = new byte[SIZE]; ist.read(cft); String resStr = new String(cft, "US-ASCII"); System.out.println("byte配列->ASCIIコードの要素: " + resStr); //マジックバイト判定 for (int i = 0; i < pdfByte.length; i++) { if (cft[i] != pdfByte[i]) { result = false; } } return result; } }
実行結果
byte配列->ASCIIコードの要素: %PDF-
指定ファイルがpdfファイルか: true