0

I have a method which takes a file and upload it on given path.

Here is my service

public String fileUpload(MultipartFile file) throws IOException {
        log.debug("uploading video");
        File fileUpload = new File(file.getOriginalFilename());
        if (!file.isEmpty()) {

            InputStream inputStream = file.getInputStream();
            byte[] buf = new byte[1024];
            FileOutputStream fileOutputStream = new FileOutputStream(new File(
                    fileUploadPath + File.separator
                            + file.getOriginalFilename()));
            int numRead = 0;
            while ((numRead = inputStream.read(buf)) >= 0) {
                fileOutputStream.write(buf, 0, numRead);
            }
            inputStream.close();
            fileOutputStream.close();
}
else {
            return Constants.EMPTY_FILE;
        }
}

After uploading the file i have to save it information in my database.File size could be 1GB or 2GB.My problem is how would i know the file is fully uploaded or not.So that i can save it status uploaded successfully in my db. Anyone please help me looking into this ?

Abhishek saini
  • 507
  • 1
  • 8
  • 29

1 Answers1

0
  1. You can create a MD5 hash before uploading the file. Take a look at this on creating MD5 hash with JavaScript via How to calculate md5 hash of a file using javascript.
  2. And after the file is completely uploaded, you can use MessageDigest to create another MD5 hash to compare it again the one before the upload. (See example: http://javarevisited.blogspot.com/2013/06/how-to-generate-md5-checksum-for-files.html)
Community
  • 1
  • 1
shokulei
  • 85
  • 7