Simple question: how can I get MIME type (or content type) of an InputStream
, without saving file, for a file that a user is uploading to my servlet?
It depends on where you are getting the input stream from. If you are getting it from a servlet then it is accessable through the HttpServerRequest object that is an argument of doPost. If you are using some sort of rest API like Jersey then the request can be injected by using @Context. If you are uploading the file through a socket it will be your responsibility to specify the MIME type as part of your protocol as you will not inherit the http headers.
If using a JAX-RS rest service you can get it from the MultipartBody.
@POST
@Path( "/<service_path>" )
@Consumes( "multipart/form-data" )
public Response importShapeFile( final MultipartBody body ) {
String filename = null;
String InputStream stream = null;
for ( Attachment attachment : body.getAllAttachments() )
{
ContentDisposition disposition = attachment.getContentDisposition();
if ( disposition != null && PARAM_NAME.equals( disposition.getParameter( "name" ) ) )
{
filename = disposition.getParameter( "filename" );
stream = attachment.getDataHandler().getInputStream();
break;
}
}
// Read extension from filename to get the file's type and
// read the stream accordingly.
}
Where PARAM_NAME is a string representing the name of the parameter holding the file stream.
You can just add the tika-app-1.x.jar to your classpath as long as you don't use slf4j logging anywhere else because it will cause a collision. If you use tika to detect an inputstream it has to be mark supported. Otherwise, calling tika will erase your input stream. However if you use the apache IO library to get around this and just turn the InputStream into a File in memory.
import org.apache.tika.*;
Tike tika = new Tika();
InputStream in = null;
FileOutputStream out = null;
try{
out = new FileOutputStream(c:/tmp.tmp);
IOUtils.copy(in, out);
String mimeType = tika.detect(out);
}catch(Exception e){
System.err.println(e);
} finally {
if(null != in)
in.close();
if(null != out)
out.close();
}
I think this solves problem:
public String readIt(InputStream is) {
if (is != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
is.close();
return sb.toString();
}
return "error: ";
}
What it returns? For example for png : "♦PNG\n\n♦♦♦.....", for xml:
Quite usefull, You cant try string.contains() to check what is it
According to Real Gagnon's excellent site, the better solution for your case would be to use Apache Tika.
You can check the Content-Type
header field and have a look at the extension of the filename used. For everything else, you have to run more complex routines, like checking by Tika
etc.