上传的照片大小规格限制

对上传照片大小规格限制,有两种方式。
第一种,是按照照片的宽度,高度。来进行限制
        /**
* <p>Title: main
* <p>Description: 获取上传照片的长宽高
* @param args
*/
public static void main(String[] args) {

String imgPath = "F:\\01\\123.jpg";

                 /**
*
InputStream stream =  myFile.getInputStream();
                  BufferedImage bufImage = ImageIO.read(stream);
                  bufImage.getHeight();
                  bufImage.getWidth();
*/
File file = new File(imgPath);
Integer width = getImgWidth(file);
Integer heigth = getImgHeigth(file);
System.out.println(width);
System.out.println(heigth);
                  if (width <= 307 && heigth <= 377){
System.out.println("上传的照片大小规格:必须大于307像素(宽)×377像素(高)!");
}else {
System.out.println("上传照片格式符合标准!");
}
}

/**
* <p>Title: getImgWidth
* <p>Description: 获取照片的宽度
* @param file
* @return
*/
public static int getImgWidth(File file) { 
        InputStream is = null; 
        BufferedImage src = null; 
        int ret = -1; 
        try { 
            is = new FileInputStream(file); 
            src = javax.imageio.ImageIO.read(is); 
            ret = src.getWidth(null); // 得到源图宽 
            is.close(); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return ret; 
    } 

/**
* <p>Title: getImgHeigth
* <p>Description: 
* @param file
* @return
*/
public static int getImgHeigth(File file){
InputStream is = null;
BufferedImage src = null;
int ret = -1;
try {
is = new FileInputStream(file);
src = ImageIO.read(is);
ret = src.getHeight();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}


第二种,是按照上传照片的大小,进行限制。上传照片的字节码,和需要限制的文件大小进行限制。 需要乘以 1024个字节

猜你喜欢

转载自liushuifeiyu.iteye.com/blog/2384887