0

I have tried all the following:

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.nio.file.Files;

public class mimeDicom {
    public static void main(String[] argvs) throws IOException{
        String path = "Image003.dcm";
        String[] mime = new String[3];
        File file = new File(path);
        mime[0] = Files.probeContentType(file.toPath());
        mime[1] = URLConnection.guessContentTypeFromName(file.getName());
        InputStream is = new BufferedInputStream(new FileInputStream(file));
        mime[2] = URLConnection.guessContentTypeFromStream(is);
        for(String m: mime)
            System.out.println("mime: " + m);
    }
}

But the results are still: mime: null for each of the tried methods above and I really want to know if the file is a DICOM as sometimes they don't have the extension or have a different one.

How can I know if the file is a DICOM from the path?

Note: this is not a duplicate of How to accurately determine mime data from a file? because the excellent list of magic numbers doesn't cover DICOM files and the apache tika gives application/octet-stream as return which doesn't really identify it as an image and it's not useful as the NIfTI files (among others) get the exactly same MIME from Tika.

Community
  • 1
  • 1
Mansueli
  • 6,223
  • 8
  • 33
  • 57
  • possible duplicate of [How to accurately determine mime data from a file?](http://stackoverflow.com/questions/8488491/how-to-accurately-determine-mime-data-from-a-file) – Raedwald Jun 16 '14 at 19:52
  • 2
    Not a duplicate, I didn't find any DICOM-related info from the supplied question, or its links. – Chris O Jun 17 '14 at 11:26

1 Answers1

3

To determine if a file is Dicom, you best bet is to parse the file yourself and see if it contains the magic bytes "DICM" at the file offset 128.

The first 128 bytes are usually 0 but may contain anything.

Paolo Brandoli
  • 4,681
  • 26
  • 38