javascript字节数组转换为16进制/**
* byte数组转换成16进制字符串
* @param src
* @return
*/
public static String bytesToHexString(byte[] src){
StringBuilder stringBuilder = new StringBuilder();
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = HexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
String();
}
/**
* 根据⽂件流读取图⽚⽂件真实类型
* @param is
* @return
*/
public static String getTypeByStream(FileInputStream is){
byte[] b = new byte[4];
try {
} catch (IOException e) {
e.printStackTrace();
}
String type = bytesToHexString(b).toUpperCase();
ains("FFD8FF")){
return "jpg";
}else ains("89504E47")){
return "png";
}else ains("47494638")){
return "gif";
}else ains("49492A00")){
return "tif";
}else ains("424D")){
return "bmp";
}
return type;
}
public static void main(String[] args) throws Exception {
// String src = "D:/workspace//8129.jpg";
// String src = "D:/workspace//temp/1.gif";
String src = "D:/workspace//temp/2.bmp";
FileInputStream is = new FileInputStream(src);
// byte[] b = new byte[4];
// is.read(b, 0, b.length);
// System.out.println(bytesToHexString(b));
String type = getTypeByStream(is);
System.out.println(type);
/*
* JPEG (jpg),⽂件头:FFD8FF
PNG (png),⽂件头:89504E47
GIF (gif),⽂件头:47494638
TIFF (tif),⽂件头:49492A00
Windows Bitmap (bmp),⽂件头:424D
*/
}
==================================
⽤⽂件头判断。直接读取⽂件的前⼏个字节。
常⽤⽂件的⽂件头如下:
JPEG (jpg),⽂件头:FFD8FF
PNG (png),⽂件头:89504E47
GIF (gif),⽂件头:47494638
TIFF (tif),⽂件头:49492A00
Windows Bitmap (bmp),⽂件头:424D
CAD (dwg),⽂件头:41433130
Adobe Photoshop (psd),⽂件头:38425053
Rich Text Format (rtf),⽂件头:7B5C727466
XML (xml),⽂件头:3C3F786D6C
HTML (html),⽂件头:68746D6C3E
Email [thorough only] (eml),⽂件头:44656C69766572792D646174653A Outlook Express (dbx),⽂件头:CFAD12FEC5FD746F
Outlook (pst),⽂件头:2142444E
MS Word/Excel (doc),⽂件头:D0CF11E0
MS Access (mdb),⽂件头:5374616E64617264204A
WordPerfect (wpd),⽂件头:FF575043
Postscript. (ps),⽂件头:252150532D41646F6265
Adobe Acrobat (pdf),⽂件
头:255044462D312E
Quicken (qdf),⽂件头:AC9EBD8F
Windows Password (pwl),⽂件头:E3828596
ZIP Archive (zip),⽂件头:504B0304
javascript数组对象
RAR Archive (rar),⽂件头:52617221
Wave (wav),⽂件头:57415645
AVI (avi),⽂件头:41564920
Real Audio (ram),⽂件头:2E7261FD
Real Media (rm),⽂件头:2E524D46
MPEG (mpg),⽂件头:000001BA
MPEG (mpg),⽂件头:000001B3
Quicktime (mov),⽂件头:6D6F6F76
Windows Media (asf),⽂件头:3026B2758E66CF11
MIDI (mid),⽂件头:4D546864
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论