Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 6 of 7
JAF's javax.activation.FileDataSource class implements the javax.activation.DataSource interface to expose a simple object representing a file resource.
Listing 6 shows a specific implementation of the Representation interface that encapsulates file resources.
public class FileRepresentation extends Representation
{
public FileRepresentation(File file)
{
super(new FileDataSource(file));
}
public FileDataSource getDataSource()
{
try
{
return this.getDataSource();
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
public File getFile()
{
try
{
return this.getDataSource().getFile();
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
The FileDataSource class provides content-typing using a FileTypeMap object. The FileTypeMap class relies on a default, MIME-based map to determine content types. Customization of the default content types can be facilitated
using the setFileTypeMap method.
Implementations of the FileTypeMap class implement the abstract getContentType methods to return the content type of a resource using whatever mechanism is appropriate for the resource. This might entail
reading the file extension of a file resource or opening the file and reading its contents to determine the content type.
The FileDataSource class uses the default FileTypeMap to determine the content type of file resources. The default FileTypeMap is an instance of the MimetypesFileTypeMap class.
The MimetypesFileTypeMap class extends FileTypeMap and determines the content type of files by mapping their file extensions. The default mechanism employed by the MimetypesFileTypeMap to find the extension mapping is as follows:
MimetypesFileTypeMap instance.
Listing 7 is an example of the MIME-types mapping file.
MIME Type File Extension
========================================
text/html html htm HTML HTM
text/plain txt text TXT TEXT
image/gif gif GIF
image/ief ief
image/jpeg jpeg jpg jpe JPG
image/tiff tiff tif
image/png png PNG
image/x-xwindowdump xwd
application/postscript ai eps ps
application/rtf rtf
application/x-tex tex
application/x-texinfo texinfo texi
application/x-troff t tr roff
audio/basic au
audio/midi midi mid
audio/x-aifc aifc
audio/x-aiff aif aiff
audio/x-mpeg mpeg mpg
audio/x-wav wav
video/mpeg mpeg mpg mpe
video/quicktime qt mov
video/x-msvideo avi