您的位置:首页 > 理论基础 > 计算机网络

根据图片地址检查图片格式,今日头条有效

2017-09-16 10:32 363 查看

直接看代码

/**
* 判断图片格式,如果不是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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java http协议 url