• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 541
  • Last Modified:

File type / Mime Type detection

I'm going to upload and download a file, without knowing its type.  I can tag the type of the file when it is uploaded manually through the UI.  But, if this is not done, I would like to attempt to determine the type / mime type when downloaded.  

also, i'm not entirely sure what the difference between the file type and mime type are.  

my method currently is this:

1.  take the raw file bytes:
2.  convert to base64
3.  upload
4.  download
5.  convert back to bytes
6.  atttempt to display.  

i'm only using the mime type labels because they are standard, the actual data is not going through e-mail (but is going through xml, thus it needs to be converted anyways).
0
jjacksn
Asked:
jjacksn
  • 4
  • 3
  • 3
  • +1
2 Solutions
 
Jim CakalicSenior Developer/ArchitectCommented:
You could either use javax.activation.MimetypesFileTypeMap in the activation.jar (available at http://java.sun.com/products/javabeans/glasgow/jaf.html). Alternatively, you could try the Java Mime Magic Library (http://sourceforge.net/projects/jmimemagic/).

More details here: http://www.rgagnon.com/javadetails/java-0487.html

Regards,
Jim
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
0
 
CEHJCommented:
Can you explain a bit more about *why* you need to know the mimetype?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jjacksnAuthor Commented:
to display the file.  I'm only properly handling text and images right now.  
0
 
CEHJCommented:
If you're using Windows then the start command will automatically find the mimetype for you
0
 
jjacksnAuthor Commented:
I don't know the original file name.  I'm getting the bytes from the server, but that might be a much better way to handler this...

1.  what a good way to write a temp file (where should it go)
2.  is there a mapping of mimetype to the default file extensions?
3.  how do I mimic would occur if someone double clicked the file in windows?
0
 
Jim CakalicSenior Developer/ArchitectCommented:
To create a temp file you can use File.createTempFile and then open a FileOutputStream on the returned File. (Don't use FileWriter or you'll corrupt the binary stream.)

To find the application path and mime type for registered extensions you'll have to look in the registry. You can use jRegistryKey to do that (http://www.bayequities.com/tech/Products/jreg_key.shtml). It's JNI based, of course. Here's an example from a previous post that looks up the application path based on the file extension. Hope it helps.

import ca.beq.util.win32.registry.RegistryException;
import ca.beq.util.win32.registry.RegistryKey;
import ca.beq.util.win32.registry.RegistryValue;
import ca.beq.util.win32.registry.RootKey;

import java.util.Iterator;

public class WinRegPath {
    private static final RootKey _hkcr = RootKey.HKEY_CLASSES_ROOT;
    private String _filename;
    private String _extension;

    private WinRegPath(String filename) {
        _filename = filename;
        _extension = _filename.substring(_filename.indexOf("."));
    }

    private String getAppIdentifier() throws RegistryException {
        RegistryKey key = new RegistryKey(_hkcr, _extension);
        if (key.exists() && key.hasValues()) {
            // the data of the default value is the registered application identifier
            Iterator iter = key.values();
            while (iter.hasNext()) {
                RegistryValue value = (RegistryValue) iter.next();
                if (value.getName().length() == 0) {
                    return (String) value.getData();
                }
            }
        }
        return null;
    }

    public String getContentType() {
        try {
            RegistryKey key = new RegistryKey(_hkcr, _extension);
            if (key.exists() && key.hasValues()) {
                // the data of the "Content Type" value is the mime type
                Iterator iter = key.values();
                while (iter.hasNext()) {
                    RegistryValue value = (RegistryValue) iter.next();
                    if (value.getName().equalsIgnoreCase("Content Type")) {
                        return (String) value.getData();
                    }
                }
            }
        } catch (RegistryException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getApplicationPath() {
        try {
            String identifier = getAppIdentifier();
            RegistryKey key = new RegistryKey(_hkcr, identifier + "\\shell\\open\\command");
            if (key.exists() && key.hasValues()) {
                // the path is the data of the default value of the shell\open\command key
                Iterator iter = key.values();
                while (iter.hasNext()) {
                    RegistryValue value = (RegistryValue) iter.next();
                    if (value.getName().length() == 0) {
                        return (String) value.getData();
                    }
                }
            }
        } catch (RegistryException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws Exception {
        WinRegPath win = new WinRegPath(args[0]);
        System.out.println("path=" + win.getApplicationPath());
        System.out.println("type=" + win.getContentType());
    }
}

Given the name of a file, the class will determine the application path and the mime type (aka content type in the registry) for the specified extension if registered.

Regards,
Jim Cakalic
0
 
jjacksnAuthor Commented:
Hi Jim,

if I write the temp file, is there some way to mimic what occurs when i double click on the file in the windows shell (I assume it is basically what you posted, but happens in the os)?

0
 
CEHJCommented:
The above could be done more easily by doing

Runtime.getRuntime().exec("cmd.exe /c start " + filename);

could it not?
0
 
CEHJCommented:
:-)
0
 
Jim CakalicSenior Developer/ArchitectCommented:
Assuming you already know that the file should have an extension of .doc (i.e., an MSWord file):
    File file = File.createTempFile("bin", "doc");
    OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
    // code here to write the file
    WinRegPath win = new WinRegPath(file.getName());
    Runtime.getRuntime().exec(win.getApplicationPath() + " " + file.getAbsolutePath());

As for using simply:
    Runtime.getRuntime().exec("cmd.exe /c start " + filename);

That should work. I borrowed this solution (above) from an earlier post of mine where the problem was simply running a program  knowing the name of the executable but not knowing it's install location. For example, if you just go to the command line and type winword you get nothing. But when you have a filename the shell /can/ find and start the associated program for the registered file extension.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now