Powered By Blogger

Thursday 9 April 2009

How to get mimeType in Java

Recently, I tried to write a piece of code that uploads my file directly to Google docs (I'm so in love with Google :-x) and I needed the mime type of the file I intended to upload , to my surprise, default packages in JDK does not return correct mimeTypes e.g java.net.FileNameMap.

Java by default returns 'application/octet-stream' as Microsoft word document mime type instead of 'application/msword'. This however causes an exception

ServiceException: com.google.gdata.util.InvalidEntryException: Bad Request
Content-Type application/octet-stream is not a valid media type

Strange isn't it? I also tried a couple of Classes that uses JAVA_HOME/lib/content-types.properties file. The is my solution perhaps you alsi have similar troubles like me.

1. Grab a copy of install-ocutil-2.4.2

2. Open a command prompt and type java -jar install-ocutil-2.4.2.jar

3. Follow the instructions and choose default installation path

4. Add C:\Program Files\clapper.org\util\lib\ocutil-2.4.2.jar to your application- In Netbeans IDE 6.5 you may follow these steps

5. Right click on Library of your project node

6. Click on Add JAR/Folder... and select C:\Program Files\clapper.org\util\lib\ocutil-2.4.2.jar

Please when adding hefty lib like gdata client libary you may want to create a library using library manager and then import. See getting started with Java and google soon in my next blog :-)

7. You may test using this sample code I have written, Note that org.clapper.util.misc.MIMETypeUtil was imported.

All the best and happy coding!!! :-)

No comments:

Post a Comment