源码:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class ImageUtil {
public static void checkImageIsNormal(final MultipartFile imageFile)
throws Exception {
if (imageFile == null) {
throw new IllegalArgumentException("imageFile == null");
}
final long imageSize = imageFile.getSize();
System.out.println("length:" + imageSize);
if (imageSize > Config.IMG_MAX_SIZE) {
throw new IllegalArgumentException("图片大小不能超过2M");
}
final String fileName = imageFile.getOriginalFilename();
final String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
System.out.println("name:" + fileName);
System.out.println("type:" + suffix);
final String[] imageTypes = { "jpeg", "jpg", "png", "bmp" };
final List
imageTypeList = Arrays.asList(imageTypes);
if (!imageTypeList.contains(suffix.toLowerCase())) {
throw new IllegalArgumentException("图片格式不支持");
}
final BufferedImage bufferedImage = ImageIO.read(imageFile
.getInputStream());
final int width = bufferedImage.getWidth();
final int height = bufferedImage.getHeight();
System.out.println("width:" + width + " height:" + height);
if (width < Config.IMG_MIN_PIXEL || width > Config.IMG_MAX_PIXEL
|| height < Config.IMG_MIN_PIXEL
|| height > Config.IMG_MAX_PIXEL) {
throw new IllegalArgumentException("图片尺寸不符合");
}
}
public static void checkImageIsNormal(final String imageUrl)
throws Exception {
if (imageUrl == null) {
throw new IllegalArgumentException("imageUrl == null");
}
final URL url = new URL(imageUrl);
final URLConnection uc = url.openConnection();
if (uc == null) {
throw new NullPointerException("网络图片不存在");
}
final long imageSize = uc.getContentLength();
System.out.println("netImage length:" + imageSize);
if (imageSize > Config.IMG_MAX_SIZE) {
throw new IllegalArgumentException("图片大小不能超过2M");
}
final ImageInputStream iis = ImageIO.createImageInputStream(uc
.getInputStream());
final Iterator iter = ImageIO.getImageReaders(iis);
if (!iter.hasNext()) {
throw new RuntimeException("No readers found!");
}
final ImageReader reader = iter.next();
System.out.println("Net imageType: " + reader.getFormatName());
final String suffix = reader.getFormatName();
final String[] imageTypes = { "jpeg", "jpg", "png", "bmp" };
final List imageTypeList = Arrays.asList(imageTypes);
if (!imageTypeList.contains(suffix.toLowerCase())) {
throw new IllegalArgumentException("图片格式不支持");
}
reader.setInput(iis, true);
final int width = reader.getWidth(0);
final int height = reader.getHeight(0);
System.out.println("width:" + width + " height:" + height);
if (width < Config.IMG_MIN_PIXEL || width > Config.IMG_MAX_PIXEL
|| height < Config.IMG_MIN_PIXEL
|| height > Config.IMG_MAX_PIXEL) {
throw new IllegalArgumentException("图片尺寸不符合");
}
}
public static void imageReader() throws Exception, IOException {
final long start = System.currentTimeMillis();
final File file = new File("wwj6.jpg");
final ImageInputStream iis = ImageIO
.createImageInputStream(new FileInputStream(file));
final Iterator iter = ImageIO.getImageReaders(iis);
if (!iter.hasNext()) {
throw new RuntimeException("No readers found!");
}
final ImageReader reader = iter.next();
reader.setInput(iis, true);
final int width = reader.getWidth(0);
final int height = reader.getHeight(0);
final long end = System.currentTimeMillis();
System.out.println("userd:" + (end - start) + "ms");
System.out.println("width:" + width + " height:" + height);
}
public static void bufferedImage() throws Exception, IOException {
final long start = System.currentTimeMillis();
final File file = new File("wwj6.jpg");
final BufferedImage bufferedImage = ImageIO.read(new FileInputStream(
file));
final int width = bufferedImage.getWidth();
final int height = bufferedImage.getHeight();
final long end = System.currentTimeMillis();
System.out.println("userd:" + (end - start) + "ms");
System.out.println("width:" + width + " height:" + height);
}
public static void main(final String[] args) throws IOException {
System.out.println("imageReader:");
try {
imageReader();
} catch (final Exception e) {
e.printStackTrace();
}
System.out.println("bufferedImage:");
try {
bufferedImage();
} catch (final Exception e) {
e.printStackTrace();
}
}
}