简体   繁体   中英

Identifying the same image in coming as multipart file in request spring boot java

I am creating an application with spring boot where I am doing OCR of the image. My request asks for the multipart file. When I get the multipart file, I need to know if I have already processed the same image.

I create MultipartEntity and hash of the same. I believed that next time onwards if the same file comes. I will get to create the hash and compare it.

When I am trying to do this. I am finding that its hash is always different. Is there a way I can identify that this image was previously OCRed so on the basis of hash only I will retrieve the results.

Request params as file in request:-
@RequestParam("file") MultipartFile file

This is how I was trying to create the hash:

  FileBody fileBody = new FileBody(new File(fileName));
  MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.STRICT);
  multipartEntity.addPart("file", fileBody);
  String requestBodyHash = PanUtil.getHashFromRequestBody(multipartEntity.toString());

  public static String getHashFromRequestBody(String req) {

    String requestBodyHash = null;
    try {
      requestBodyHash = generateSha2FromPayload(req);
    } catch (NoSuchAlgorithmException | URISyntaxException e) {
      log.error("Exception occured while creating hash from request {}", e);
      throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, e.getMessage());
    }
    return requestBodyHash;
  }

  public static String generateSha2FromPayload(String json)
      throws URISyntaxException, NoSuchAlgorithmException {

    MessageDigest md = MessageDigest.getInstance("SHA-256");
    byte[] digest = md.digest(json.getBytes(StandardCharsets.UTF_8));
    return toHexString(digest);
  }

I have used the below code to find the same hash of the image.

  public static String toHexString(byte[] hash) {

    // Convert byte array into signum representation
    BigInteger number = new BigInteger(1, hash);

    // Convert message digest into hex value
    StringBuilder hexString = new StringBuilder(number.toString(16));

    // Pad with leading zeros
    while (hexString.length() < 32) {
      hexString.insert(0, '0');
    }
    return hexString.toString();
  }

Call this function. This will always give the same hex.

toHexString(file.getBytes())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM