Java tutorial
//package com.java2s; import java.io.File; public class Main { public static String generateFileContentType(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; else return "application/octet-stream"; } }