java图⽚类型判断
⽅法⼀:根据图⽚url中的携带的ContentType信息判断图⽚的类型,如果图⽚被强制修改了图⽚的类型会有问题
/**
* 根据url中ContentType判断问价格式
* @param imgUrl
* @return
*/
public static String getImageType(String imgUrl) {
BufferedInputStream bis = null;
HttpURLConnection urlconnection = null;
String prefix = "";
try {
URL url = new URL(imgUrl);
urlconnection = (HttpURLConnection) url.openConnection();
bis = new InputStream());
String imageTypes = HttpURLConnection.guessContentTypeFromStream(bis);
if (ains("/")) {
prefix = imageTypes.substring(imageTypes.lastIndexOf("/"));
prefix = placeFirst("/", ".");
return prefix;
}
} catch (IOException e) {
e.printStackTrace();
}
return ".jpg";
}
⽅法⼆:将图⽚的信息读成流转16进制,根据⽂件流判断⽂件类型(推荐使⽤)
public class ImageTypeUtils {
/**
常⽤⽂件的⽂件头如下:(以前六位为准)
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
RAR Archive (rar),⽂件头:52617221
Wave (wav),⽂件头:57415645
AVI (avi),⽂件头:41564920
Real Audio (ram),⽂件头:2E7261FD
Real Media (rm),⽂件头:2E524D46
MPEG (mpg),⽂件头:000001BA
MPEG (mpg),⽂件头:000001BA
MPEG (mpg),⽂件头:000001B3
Quicktime (mov),⽂件头:6D6F6F76
Windows Media (asf),⽂件头:3026B2758E66CF11
MIDI (mid),⽂件头:4D546864
public static final String TYPE_JPG = ".jpg";
public static final String TYPE_GIF = ".gif";
public static final String TYPE_PNG = ".png";
public static final String TYPE_BMP = ".bmp";
public static final String TYPE_WEBP = ".webp";
public static final String TYPE_TIF = ".tif";
// public static final String TYPE_UNKNOWN = "unknown";
public static void main(String[] args) throws FileNotFoundException {
File pdfFile = new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\F35245_113_P2.jpg");
//test code
System.out.println("图⽚格式1: " + getPicType(new FileInputStream(pdfFile)));
}
/**
* 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 fis
* @return jpg/png/gif/bmp
*/
public static String getPicType(FileInputStream fis) {
//读取⽂件的前⼏个字节来判断图⽚格式
byte[] b = new byte[4];
try {
String type = bytesToHexString(b).toUpperCase();
if (ains("FFD8FF")) {
return TYPE_JPG;
} else if (ains("89504E47")) {
return TYPE_PNG;
} else if (ains("47494638")) {
return TYPE_GIF;
} else if (ains("424D")) {
return TYPE_BMP;
}else ains("52494646")){
return TYPE_WEBP;
return TYPE_WEBP;
}else ains("49492A00")){
return TYPE_TIF;
}else{
return TYPE_JPG;
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
/**
* 根据⽂件流判断图⽚类型
* @param fis
* @return jpg/png/gif/bmp
*/
public static String getPicType2(InputStream fis) {
//读取⽂件的前⼏个字节来判断图⽚格式
byte[] b = new byte[4];
try {
String type = bytesToHexString(b).toUpperCase();
if (ains("FFD8FF")) {
return TYPE_JPG;
} else if (ains("89504E47")) {
return TYPE_PNG;
} else if (ains("47494638")) {
return TYPE_GIF;
} else if (ains("424D")) {
return TYPE_BMP;
}else ains("52494646")){
return TYPE_WEBP;
}else ains("49492A00")){
return TYPE_TIF;
}else{
return null;
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
相关问题:webp图⽚ios⼿机和ie浏览器显⽰会有问题(可转换为png图⽚)实例:
java修改html文件url1:原图⽚路径
path:⽂件临时存储路径
也可转换为jpg,png等格式
String imgType = PicType(new FileInputStream(url1));
if (imgType.equals(".webp")) {//图⽚转化
ImageReader reader = ImageReadersByMIMEType("image/webp").next();    WebPReadParam readParam = new WebPReadParam();
readParam.setBypassFiltering(true);
reader.setInput(new FileImageInputStream(new File(url)));
url = path + UUID.randomUUID() + ".png";
BufferedImage image = ad(0, readParam);
ImageIO.write(image, "png", new File(url));
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。