Skip to content

Instantly share code, notes, and snippets.

@madan712
Last active December 10, 2015 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madan712/4395954 to your computer and use it in GitHub Desktop.
Save madan712/4395954 to your computer and use it in GitHub Desktop.
Java - Get content type of a particular file
/* ContentTypeChecker.java */
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
public class ContentTypeChecker {
public static void main(String[] args) throws IOException {
File file = new File("C:/image.jpg");
URL u = new URL("file:"+file);
URLConnection uc = u.openConnection();
String type = uc.getContentType();
System.out.println("Content type of file '"+file+"' is "+type);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment