1 /**
2 * 根据img获取图片的宽高
3 * @param img 图片地址
4 * @return 图片的对象,对象中图片的真实宽高
5 */
6 public BufferedImage getBufferedImage(String imgurl) {
7 URL url = null;
8 InputStream is = null;
9 BufferedImage img = null;
10 try {
11 url = new URL(imgurl);
12 HttpURLConnection con =(HttpURLConnection)url.openConnection();
13 // 设置请求头信息
14 con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0");
15 con.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
16 con.setRequestProperty("Connection", "keep-alive");
17
18 is = con.getInputStream();
19 img = ImageIO.read(is);
20 } catch (MalformedURLException e) {
21 e.printStackTrace();
22 log.error(e);
23 } catch (Exception e) {
24 img=null;
25 log.error(e);
26 e.printStackTrace();
27 } finally {
28 try {
29 if(is!=null){
30 is.close();
31 }
32 } catch (IOException e) {
33 e.printStackTrace();
34 log.error(e);
35 }
36 }
37 return img;
38 }