Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How To Get FileType and MIME Type from a File

807588Apr 15 2009 — edited Apr 15 2009
Hi,

I am using following ways to get FileType and MIME Type.

I am able to get file type, but I am getting MIME Type as */*
Can any one please let me know how to get MIME Type, but it should not be time consuming process.

For File Type I am using following Code:

private String getFileData(InputStream inStream) {
fileName = C:\temp\temp.tiff

String filePath = Util.createTempFile(fileName);
RandomAccessFile raf = new RandomAccessFile(filePath, "rw");

while (inStream.available() > 0) {
byte[] buf = new byte[inStream.available()];
inStream.read(buf);
raf.write(buf);
}

inStream.close();
raf.close();

File file = new File(filePath);

JFileChooser chooser = new JFileChooser();
String fileType = chooser.getTypeDescription(file);
file.delete();

//MIME Type I am getting from

DataHandler data = new DataHandler(new DocumentSource(inStream));
data.getContentType();
}

data.getContentType() is not working well in all cases.

Can any one help me on this.

Thanks in advance,
Ram.

Edited by: javausers07 on Apr 15, 2009 4:25 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 13 2009
Added on Apr 15 2009
1 comment
556 views