上传的照片大小规格限制

对上传照片大小规格限制,有两种方式。
第一种,是按照照片的宽度,高度。来进行限制
/**
* <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个字节
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值