Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8138291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:26:47+00:00 2026-06-06T11:26:47+00:00

I am using the java MongoDB driver for gridfs and would like to use

  • 0

I am using the java MongoDB driver for gridfs and would like to use MD5 hash to check if a file already exist before saving it. Essentially I am trying to do this in Java.

I tried DigestUtils from apache common-codec with the following logic:

public GridFSDBFile save(InputStream inputStream, String contentType, String filename) throws IOException {
    String md5 = DigestUtils.md5Hex(inputStream);

    List<GridFSDBFile> md5match = gridFs.find(new BasicDBObject("md5", md5));

    if (md5match!=null && md5match.size()>0) {
        return md5match.get(0);
    } else {
        GridFSInputFile input = gridFs.createFile(inputStream, filename, true);
        input.save();
        return gridFs.findOne(input.getId())
    }
}

Looking at the underlying implementation, both DigestUtils and MongoDB driver uses MessageDigest.getInstance(“MD5”) to calculate MD5 hash. However, it looks like the md5 hash code generated by DigestUtils are not the same as what gridfs generates. Overwrite the “md5” key in GridFSInputFile does not work either.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:26:49+00:00Added an answer on June 6, 2026 at 11:26 am

    The answer turns out to have nothing to do with Mongodb driver. In order to calculate the md5 hash, DigestUtils must read through the InputStream. In order for the above code to work properly, use mark/reset:

    inputStream.mark(Integer.MAX_VALUE);
    String md5 = DigestUtils.md5Hex(inputStream);
    inputStream.reset();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im using java/mongodb. I would like to create a table (collection) with some users.
I need to know how to use where in MongoDB queries using Java Driver.
I'm trying to retrieve only a subset of fields from mongodb using Java driver.
I'm trying to Or some conditions in MongoDB (using the Java driver). This is
I'm trying to use MongoDB's Java driver to make two updates ($set and $push)
I am using Mongodb Java driver for my Java client application. it needs to
Casbah is another layer on top of Java driver for MongoDb. Would this reduce
I'm using the MongoDB Java driver, and can't seem to get a query to
Im using the MongoDB Java driver in a java web app. One question I
Using Java is there an easy way to check whether a given file conforms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.