根据图片地址检查图片格式,今日头条有效
程序员文章站
2022-05-31 09:15:50
...
直接看代码
/**
* 判断图片格式,如果不是GIF就返回true
* 是GIF就返回false
*
* @param imgUrl
* @return
* @throws HttpException
* @throws IOException
* @author XiaoMingHui
* @date 2017-8-21 上午10:36:22
*/
private boolean judgeImgFormat(String imgUrl){
org.apache.commons.httpclient.methods.GetMethod get = new org.apache.commons.httpclient.methods.GetMethod(imgUrl);
try {
new org.apache.commons.httpclient.HttpClient().executeMethod(get);
} catch (HttpException e) {
logger.error("判断图片是否为GIF时出现HttpException,异常的图片地址为:" + imgUrl, e);
} catch (IOException e) {
logger.error("判断图片是否为GIF时出现IOException,异常的图片地址为:" + imgUrl, e);
}
org.apache.commons.httpclient.HeaderElement[] heElements = get.getResponseHeader("Content-Type")
.getElements();
for (HeaderElement hElement : heElements) {
if ("image/gif".equals(hElement.getName())) {
return false;
}
}
return true;
}
推荐阅读