Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

how to determine extension name if I know the mime type?

Posted on 2006-04-27
18
Medium Priority
?
628 Views
Last Modified: 2008-02-26
if I know the mime type for a file, what is the best way to determine what the extension of the file should be?  (I'm getting bytes and a mimetype, and I want to write a temporary file with the correct extension).
0
Comment
Question by:jjacksn
  • 9
  • 6
  • 2
17 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16557778
You can find it out using the following class:


javax.activation.MimetypesFileTypeMap mm = new javax.activation.MimetypesFileTypeMap();
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16557781
javax.activation.MimetypesFileTypeMap
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16557786
0
Independent Software Vendors: 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!

 
LVL 5

Author Comment

by:jjacksn
ID: 16557794
right, i'm using that class to learn the mime type from the original file. Given the mime type, I don't see how to get the extension back out from that class.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16557862
Sorry. Yes - look at the file format here:

http://java.sun.com/products/javabeans/glasgow/javadocs/javax/activation/MimetypesFileTypeMap.html

and you should be able to extract the extension
0
 
LVL 5

Author Comment

by:jjacksn
ID: 16558002
I see, ok.

1.  The file .mime.types in the user's home directory.
2.  The file <java.home>/lib/mime.types.
3.  The file or resources named META-INF/mime.types.
4.  The file or resource named META-INF/mimetypes.default (usually found only in the activation.jar file).

for 1:  

String userHome = System.getProperty("user.home");
String userHomeFile = userHome + System.getProperty("file.separator") +".mime.types";

for 2:

String javaHome = System.getProperty("java.home");
String javaHomeFile = javaHome + System.getProperty("file.separator") + "lib" + System.getProperty("file.separator") + "mime.types";

for 3:

willing to ignore

for 4:

how do I get this resource stream properly?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16558016
Using the empty ctor should do all those steps for you automatically
0
 
LVL 5

Author Comment

by:jjacksn
ID: 16558060
sorry, i'm definitely missing something.  

Given that map, i do not see how to get out the actual file that i could parse to get the extensions....?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16558111
Sorry - my fault jj - i'm getting tired ;-) Yes you *do* need to get those resources yourself

META-INF/mimetypes.default

is inside activation.jar
0
 
LVL 5

Author Comment

by:jjacksn
ID: 16558138
hmmm... I seem to be missing a lot of mimetypes on its not correctly determining gif, bmp, pdf, etc.  Looks like I'll need to do a lot of this by hand, I might as well just create the dictionary in code.  
0
 
LVL 5

Author Comment

by:jjacksn
ID: 16558139
(everything is returning application/octet-stream)
0
 
LVL 92

Expert Comment

by:objects
ID: 16558140
you can't determine the extension from the mimetype.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16558204
The contents of the last thing discussed are


#
# A simple, old format, mime.types file
#
text/html               html htm
text/plain              txt text
image/gif               gif GIF
image/ief               ief
image/jpeg              jpeg jpg jpe JPG
image/tiff              tiff tif
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16558215
0
 
LVL 5

Author Comment

by:jjacksn
ID: 16559591
so once i have the URL of the property, what is the best mechanism to read the resource one line at a time.  i have completely forgot how StringTokenizer works.  

i basically want to skip all white space..... what would this look like?
0
 
LVL 92

Accepted Solution

by:
objects earned 1200 total points
ID: 16559605
     BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String line = null;
        while ((line = in.readLine()) != null) {
            String[] tokens = line.split(" ");
            String type = tokens[0];
            // tokens 1-tokens.length-1 are possible extensions
        }
        in.close();

up to you how to pick which extension to use for a particular type as it isn't a 1:1 mapping.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 800 total points
ID: 16560142
Properties p = new Properties();
p.load(url.openStream());
String mpegExtensions = p.getProperty("audio/x-mpeg");
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month15 days, 9 hours left to enroll

576 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question