aspose file tools*
The moose likes I/O and Streams and the fly likes how to identify the file format from byte array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "how to identify the file format from byte array?" Watch "how to identify the file format from byte array?" New topic
Author

how to identify the file format from byte array?

Kishore Polsani
Greenhorn

Joined: May 20, 2012
Posts: 11
Hi,
i have a web service which reads any file format and returns a byte array. In client side I need to read that byte array and open the file. How can we identify the file format from that byte array???

Thanks in advance
Kishore
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 877
    
    5
It is possible to provide some content-type information along with the contents?
Kishore Polsani
Greenhorn

Joined: May 20, 2012
Posts: 11
Hi Ivan,
Thanks for reply. Can you please tell me how we can achieve this or please provide some code snippet for this.
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 877
    
    5
> i have a web service which reads any file format and returns a byte array

The Web service could provide besides the contents also the content type.
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 877
    
    5
There is by the way no such thing as "the content type" of a file per se.
There exists a funny file format: a portion of C code with data which can also be considered as an image. Is such a file a piece of plain text, C source, C++ source or an image file?

There exists the file naming convention that the last portion of a file's name (the one after the dot - also called "extension") hints on the content.
Web servers can be configured how to map these "extensions" to content-types.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to identify the file format from byte array?