1 48 49 package org.jpublish.util.mime; 50 51 import java.util.HashMap ; 52 import java.util.Iterator ; 53 54 59 public class MimeTypeMap extends HashMap { 60 61 64 public static final String DEFAULT_MIME_TYPE = "application/octet-stream"; 65 66 private String defaultMimeType = DEFAULT_MIME_TYPE; 67 68 71 public MimeTypeMap() { 72 init(); 73 } 74 75 80 public String getMimeType(String extension) { 81 String mimeType = (String ) get(extension); 82 if (mimeType == null) { 83 return defaultMimeType; 84 } else { 85 return mimeType; 86 } 87 } 88 89 95 public String getExtension(String mimeType) { 96 Iterator iter = keySet().iterator(); 97 while (iter.hasNext()) { 98 String extension = (String ) iter.next(); 99 if (get(extension).equals(mimeType)) { 100 return extension; 101 } 102 } 103 return null; 104 } 105 106 111 public String getDefaultMimeType() { 112 return defaultMimeType; 113 } 114 115 120 public void setDefaultMimeType(String defaultMimeType) { 121 if (defaultMimeType != null) { 122 this.defaultMimeType = defaultMimeType; 123 } else { 124 this.defaultMimeType = DEFAULT_MIME_TYPE; 125 } 126 } 127 128 143 protected void init() { 144 put("html", "text/html"); 145 put("txt", "text/plain"); 146 put("xml", "text/xml"); 147 put("css", "text/css"); 148 put("gif", "image/gif"); 149 put("jpg", "image/jpeg"); 150 put("png", "image/png"); 151 } 152 153 } 154 | Popular Tags |