Java tutorial
//package com.java2s; import java.io.File; public class Main { public static String getContentType(File f) { int dotIndex = f.getAbsolutePath().lastIndexOf("."); if (dotIndex < 0) { return "application/octet-stream"; } String suffix = f.getAbsolutePath().substring(dotIndex).toLowerCase(); if ("jpg".equals(suffix) || "jpeg".equals(suffix)) return "image/jpeg"; else if ("png".equals(suffix) || "gif".equals(suffix)) return "image/" + suffix; else if ("mp3".equals(suffix) || "mpeg".equals(suffix)) return "audio/mpeg"; else if ("mp4".equals(suffix) || "ogg".equals(suffix)) return "audio/" + suffix; return "application/octet-stream"; } }