MIME Types
...Back to 5CS004
MIME stands for Multipurpose Internet Mail Extensions. The MIME type of a file tells you what sort of data is held inside it, and (sometimes) what sort of application can be used to open that file. On any Ubuntu machine you can look at the (plain text) file /etc/mime.types which has a list of all MIME types that Linux knows about and what file extensions are related to each type.
Common MIME types include:
MIME type | Common file extensions |
text/html | html htm shtml |
application/rss+xml | rss |
image/png | png |
audio/mpeg | mpga mpega mp2 mp3 mp4a |
video/mp4 | mp4 |
application/x-shockwave-flash | swf swfl |
text/x-java | java |
application/java-archive | jar |
application/zip | zip |
text/plain | asc txt text pot brf |
Estimating the MIME type of a file
There are various ways to try and find out what type of data is stored in a file. One way to do this would be to use the UNIX file command. For example:
$ file customer_landing.jsp customer_landing.jsp: HTML document text $
The file command uses a sophisticated algorithm to look at the first few bytes of data inside the file and match that against the list of file types it knows about. One way for you to estimate the type of a file would be to call the file command from your Scala program.
Another way, which might be less accurate, is to use the filename. We might assume that the file index.html contains data of MIME type text/html because the filename ends in .html. The following Java code returns the last part of any given filename (after the "."). Of course, this still needs to be matched against a MIME type:
/** * Estimate the type of a file from its filename. * * e.g. given a filename index.html will return: html. * * @param filename * given filename. * @return estimate of file type. */ public static final String getFileType(String filename) { String[] parts = filename.split("\\."); if (parts.length > 1) return parts[parts.length - 1]; else return null; // Better to throw an exception here! }
MimeTypes.java
If you want to use this method of estimating the MIME type of a file based on its file extension, the Java class below can do this for you. To use it in your project, include it in the same directory as your Scala code (add a package declaration if you need to) and compile and run your code as normal.
To use the Java class, import it and use it in the same way as its documentation describes, i.e.:
import MimeTypes ... MimeTypes.getType(filename) ...
// File MimeTypes.java import java.util.EnumSet; import java.util.Set; /** * MIME types and their corresponding file extensions. * * Given a file extension, obtain it's MIME type like this: * * <pre> * MimeType type = MimeTypes.getType("html"); * System.out.println(type.getType()); * // prints "text/html" * </pre> * * @author snim2 * @version 0.1 */ public class MimeTypes { /** * Enumeration of useful MIME types for web pages. * * @author snim2 * @version 0.1 */ enum MimeType { HTML("html", "text/html"), JPG("jpg", "image/jpg"), JPEG("jpeg", "image/jpg"), GIF("gif", "image/gif"), PNG("png", "image/png"), BMP("bmp", "image/x-ms-bnp"), MPG("mpg", "video/mpeg"), MPEG("mpeg", "video/mpeg"), AVI("avi", "video/x-msvideo"), SWF("swf", "application-x-shockwave-flash"), RSS("application/rss+xml", "rss"); /** File extension, e.g. "html" from the filename "index.html" */ private final String extension; /** MIME type, e.g. "text/html". */ private final String type; MimeType(String extension, String type) { this.extension = extension; this.type = type; } public String getExtension() { return this.extension; } public String getType() { return this.type; } @Override public String toString() { return this.type; } } /** Set of all known MIME types. */ private final static Set<MimeType> types = EnumSet.allOf(MimeType.class); /** * Given a file extension return its corresponding MIME type. * * @param fileExtension * e.g. "html" from the filename "index.html" * @return MIME type */ public static MimeType getType(String fileExtension) { for (MimeType type : MimeTypes.types) { if (type.getExtension().equals(fileExtension)) return type; } MimeType(String extension, String type) { this.extension = extension; this.type = type; } public String getExtension() { return this.extension; } public String getType() { return this.type; } @Override public String toString() { return this.type; } } /** Set of all known MIME types. */ private final static Set<MimeType> types = EnumSet.allOf(MimeType.class); /** * Given a file extension return its corresponding MIME type. * * @param fileExtension * e.g. "html" from the filename "index.html" * @return MIME type */ public static MimeType getType(String fileExtension) { for (MimeType type : MimeTypes.types) { if (type.getExtension().equals(fileExtension)) return type; } return null; } }
...Back to 5CS004