异常图片检测用于检测图片中是否包含除图片以外的可疑文件,例如 MPEG-TS 文件。如果检测到图片包含可疑文件,您可以对该图片文件进行处理,防止存储空间被非法图床盗用。本文介绍如何通过 TOS Java SDK 进行异常图片检测。
以下代码展示如何检测异常图片。
import com.fasterxml.jackson.core.JacksonException; import com.fasterxml.jackson.core.type.TypeReference; import com.volcengine.tos.TOSV2; import com.volcengine.tos.TOSV2ClientBuilder; import com.volcengine.tos.TosClientException; import com.volcengine.tos.TosServerException; import com.volcengine.tos.internal.util.TosUtils; import com.volcengine.tos.model.object.GetObjectV2Input; import com.volcengine.tos.model.object.GetObjectV2Output; import java.io.ByteArrayOutputStream; import java.io.IOException; public class ImageInspectExample { static class ImageInspectResult { int picSize; String picType; boolean suspicious; int suspiciousBeginByte; int suspiciousEndByte; int suspiciousSize; String suspiciousType; public String toString() { return "[picSize:" + this.picSize + ",picType:" + this.picType + ",suspicious:" + this.suspicious + ",suspiciousBeginByte:" + this.suspiciousBeginByte + ",suspiciousEndByte:" + this.suspiciousEndByte + ",suspiciousSize:" + this.suspiciousSize + ",suspiciousType:" + this.suspiciousType + "]"; } } public static void main(String[] args) { String endpoint = "your endpoint"; String region = "your region"; String accessKey = System.getenv("TOS_ACCESS_KEY"); String secretKey = System.getenv("TOS_SECRET_KEY"); String bucketName = "bucket-example"; // 需要确保对象已存在 String objectKey = "image.png"; String style = "image/inspect"; // 异常图片检测 TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey); try { GetObjectV2Input input = new GetObjectV2Input().setBucket(bucketName).setKey(objectKey).setProcess(style); try (ByteArrayOutputStream stream = new ByteArrayOutputStream(); GetObjectV2Output output = tos.getObject(input)) { byte[] buffer = new byte[1024]; int length; while ((length = output.getContent().read(buffer)) != -1) { stream.write(buffer, 0, length); } String respBody = stream.toString("UTF-8"); System.out.println("Image inspect success, inspect info is " + respBody); ImageInspectResult result = TosUtils.getJsonMapper().readValue(respBody, new TypeReference<ImageInspectResult>() { }); System.out.println(result.toString()); } catch (JacksonException e) { System.out.println("parse response data failed"); e.printStackTrace(); } catch (IOException e) { System.out.println("read response data failed"); e.printStackTrace(); } } catch (TosClientException e) { // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送。 System.out.println("Image inspect failed"); System.out.println("Message: " + e.getMessage()); if (e.getCause() != null) { e.getCause().printStackTrace(); } } catch (TosServerException e) { // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息。 System.out.println("Image inspect failed"); System.out.println("StatusCode: " + e.getStatusCode()); System.out.println("Code: " + e.getCode()); System.out.println("Message: " + e.getMessage()); System.out.println("RequestID: " + e.getRequestID()); } catch (Throwable t) { // 作为兜底捕获其他异常,一般不会执行到这里。 System.out.println("Image inspect failed"); System.out.println("unexpected exception, message: " + t.getMessage()); } } }
关于异常图片检测的详细介绍,请参见异常图片检测。