File对象(该对象里面是图片)
代码
public class Test {
public static void main(String[] args) throws Exception {
File file = new File("C:\\test\\20220619\\city.jpg");
long size = file.length() / 1024;
BufferedImage bufferedImage = ImageIO.read(new FileInputStream(file));
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
System.out.printf("图片大小:%skb;图片宽度:%s像素;图片高度:%s像素", size, width, height);
}
}
结果
图片大小:4424kb;图片宽度:7360像素;图片高度:4912像素
MultipartFile对象(该对象里面是图片)
代码
public class Test {
public static void main(String[] args) throws Exception {
MultipartFile file = 假设此处是前端传过来的对象;
long size = file.length() / 1024;
BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
System.out.printf("图片大小:%skb;图片宽度:%s像素;图片高度:%s像素", size, width, height);
}
}
结果
图片大小:4424kb;图片宽度:7360像素;图片高度:4912像素