码迷,mamicode.com
首页 > Web开发 > 详细

上传图片时获取到上传图片的长和宽

时间:2016-07-04 15:31:15      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

最近在做上传手机App背景图片的功能,但是在上传之前要验证手机要上传图片的长和宽是否符合要求:

1.以下是验证main方法

  public static void main(String[] args) throws IOException{
    File picture = new File("D:\\imgDisk\\6.jpg");
    BufferedImage sourceImage = ImageIO.read(new FileInputStream(picture));
    System.out.println(String.format("%.1f",picture.length()/1024.0));
    System.out.println(sourceImage.getWidth());
    System.out.println(sourceImage.getHeight());
  }

2.实际应用中是用对象MultipartFile file接收的,所以在实际应用中:

 BufferedImage sourceImage = ImageIO.read(file.getInputStream());
    if(sourceImage.getWidth()!=289 && sourceImage.getHeight()!=220){
      throw new ImgException("缩略图格式不正确");
    }

返回的exception到前端进行显示就好了。

 

上传图片时获取到上传图片的长和宽

标签:

原文地址:http://www.cnblogs.com/baizhanshi/p/5640299.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!