?
Solved

Is it possible to get the content-type from a file represented as a byte-array?

Posted on 2008-11-05
12
Medium Priority
?
266 Views
Last Modified: 2013-11-05
I am storing images in a database and they can be in any of the generally accepted online formats (jpg, gif, png...).
I retrieve the images from online sources. I get them in as an array of bytes through the use of an inputstream and they are then stored in the database as BLOB's.
I would like to know if I can identify the content-type/mime-type from just the array of bytes. I know I could get the content-type when I retrieve the image by asking URLConnection for it, that is my back-up plan.
I've tried sending images to the browser without specifying the content-type and it works in the browsers I've tested with, but I realize this would essentially be incorrect.

What is actually stored anyway? Is it just the data that makes up the image (the data for the pixels), or is meta data also stored (like the mime-type).
0
Comment
Question by:PHPaul
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 2

Accepted Solution

by:
MortenSlotKristensen earned 1000 total points
ID: 22892948
If you would like to get the content type from the byte array it would be a painful process.. Why not just go for the plan b? It would be, as you say, quite easy to implement anyway. :)
0
 
LVL 2

Expert Comment

by:MortenSlotKristensen
ID: 22892958
But if you still would like to know. Try looking at how the unix command 'file' does it.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 22892998
>>I know I could get the content-type when I retrieve the image by asking URLConnection for it, that is my back-up plan.

I'm wondering why you want the content-type?

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 6

Expert Comment

by:javaexperto
ID: 22893008
the format in an image file is the way the bits are stored in the byte array so I don't think there is a mime type saved  with the bytes in the data base.
0
 
LVL 2

Author Comment

by:PHPaul
ID: 22893160
To MortenSlotKristensen:
It seems I would have to look at patterns to figure out the mime-type. Not willing to do that :-).

To CEHJ:
I need the mime-type because these images are going to browsers. Therefore the Content-Type header needs to be set.

To javaexperto:
That is pretty much what I am finding out. There is meta data stored, I believe, but no mime-type.

I believe one elegant solution might be to create a special object that will hold the binary data as well as the mime-type and maybe more meta data if needed. You see I don't want to have multiple fields in my database describing one object (the image). That is bad design in my eyes.

What do you think?

Thanks for your answers so far.
0
 
LVL 4

Assisted Solution

by:pellep
pellep earned 400 total points
ID: 22893455
There is no reliable way to determine content type from the data itself (ie, the byte array). Some formats have a "header" section in the actual content that you can parse, but you cant really rely on that.

I would get the content-type from the URLConnection, like you mentioned, and store that information associated with the actual byte data in the database (your plan B). In the table where you have your BLOB column, add a foreign key column that can reference an entry in a lookup-table where you have entries describing content and mime types. This has already been suggested by other posters, I'm just chiming in with them.
0
 
LVL 4

Expert Comment

by:pellep
ID: 22893479
>>I believe one elegant solution might be to create a special object that will hold the binary data as well as the mime-type and maybe more meta data if needed. You see I don't want to have multiple fields in my database describing one object (the image). That is bad design in my eyes.

I tend to disagree. Using a custom type on the db side to store your data is viable, but you make your model platform dependant to a larger extent. Plus, working with custom types through JDBC or other abstraction frameworks in JAVA creates an additional level of complexity that's not really needed. Adding extra fields to your table, or even better, normalize it by using two tables IS good design.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 22893923
A weakness here is *just* having the blob i think. It will would be stronger were you to store the file name too, in which case, getting the content type would be a piece of cake in most cases
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 600 total points
ID: 22894978
As it happens, you can use the API here:

https://jmimeinfo.dev.java.net/
0
 
LVL 2

Author Comment

by:PHPaul
ID: 22899106
I'm going for the two table approach.

My existing table will hold a foreign key instead of the actual BLOB. The new table will hold the BLOB, mime-type and a primary key. I will retrieve the content-type from URLConnection when I initially retrieve the image and if needed I could even look at the extension or use the API that CEHJ mentioned.

Thanks all for your help. I'll divide the points later today.
0
 
LVL 2

Author Closing Comment

by:PHPaul
ID: 31513787
Sorry for the long delay. Got side-tracked.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23516065
:-)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

809 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