判断前端上传的文件是否是图片

it2025-02-09  12

公司写的,不用了,放出来留个备忘。

final String IMG_FORMAT = "png_jpg_bmp_gif_tif_jpeg_PNG_JPG_BMP_GIF_TIF_JPEG"; final String IMG_FORMAT_NUMBER = "8950_ffd8_424d_4749_4d4d_4949"; /** * 针对图片内容的格式效验。 * 分别以: * 1:判断后缀名的方式判断是否为图片 * 2:以魔术数字进行判断 * 3:以imageIO流的方式验证是否为图片 * * @param mFile * @return boolean * @author lihao */ public boolean isImage(MultipartFile mFile) { File file = null; InputStream is = null; Image img = null; byte[] bt = new byte[2]; try { file = File.createTempFile("tmp", null); mFile.transferTo(file); is = new FileInputStream(file); is.read(bt); img = ImageIO.read(file); } catch (IOException e) { return false; } //获取文件后缀进行判断 String suffix = mFile.getOriginalFilename().substring(mFile.getOriginalFilename().lastIndexOf(".") + 1); if (IMG_FORMAT.indexOf(suffix) == -1) { return false; } //以魔术数字进行判断 StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < bt.length; i++) { int v = bt[i] & 0xFF;//byte to int String hv = Integer.toHexString(v); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } System.out.println(stringBuilder.toString()); if (IMG_FORMAT_NUMBER.indexOf(stringBuilder.toString()) == -1) { return false; } //以imageIO流的方式对图片进行格式检查 if (img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) { return false; } if(file.exists()){file.delete();} return true; }

下面是给图片添加水印, 适合用base64方式存的时候拿过来用。

/** * 给图片添加水印、可设置水印图片旋转角度 * * @param logoText 水印文字 * @param degree 水印图片旋转角度 * @param clarity 透明度(小于1的数)越接近0越透明 * @param mFile 被操作的图片 */ public byte[] waterMarkByText(String logoText, Integer degree, Float clarity, MultipartFile mFile) { // 图片流 InputStream inputStream = null; ByteArrayOutputStream byteStream = null; Image srcImg = null; File file = null; byte[] byteImg = new byte[0]; //水印添加位置坐标 1像素=1磅*DPI/72 显示屏dpi 大多是72 少数96 这里不对 Integer width, height; Integer fontSize = 50; Integer rightButtomOffset = fontSize * 72 / 72 * 6; try { file = File.createTempFile("tmp", null); mFile.transferTo(file); srcImg = ImageIO.read(file); width = srcImg.getWidth(null) - rightButtomOffset; height = srcImg.getHeight(null); BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB); // 得到画笔对象 Graphics2D g = buffImg.createGraphics(); // 设置对线段的锯齿状边缘处理 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage( srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null); if (null != degree) { // 设置水印旋转 g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2); } // 设置颜色 g.setColor(Color.red); // 设置 Font g.setFont(new Font("宋体", Font.BOLD, fontSize)); float alpha = clarity; g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); // 第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y) . g.drawString(logoText, width, height); g.dispose(); // 生成图片 ImageIO.write(buffImg, "JPG", file); inputStream = new FileInputStream(file); byteStream = new ByteArrayOutputStream(); int ch; while ((ch = inputStream.read()) != -1) { byteStream.write(ch); } byteImg = byteStream.toByteArray(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != inputStream) inputStream.close(); if (null != byteStream) byteStream.close(); if (file.exists()) { file.delete(); } } catch (Exception e) { e.printStackTrace(); } } return byteImg; }

 

最新回复(0)