一、源码说明
基于springframework的org.springframework.http.MediaType通过ServletRequest对象分别获取到媒体类型、字符集类型,具体工具类ContentTypeUtil实现代码如下import java.nio.charset.Charset;@b@import javax.servlet.ServletRequest;@b@import org.springframework.http.MediaType;@b@import org.springframework.util.StringUtils;@b@@b@public class ContentTypeUtil {@b@@b@public MediaType getMediaType(ServletRequest request) {@b@String contentType = request.getContentType();@b@if (!StringUtils.hasText(contentType)) {@b@contentType = "*";@b@}@b@if (StringUtils.hasText(contentType)) {@b@return MediaType.parseMediaType(contentType);@b@}@b@return null;@b@}@b@@b@public String getCharset(ServletRequest request, String defaultCharset) {@b@MediaType mediaType = getMediaType(request);@b@if (mediaType != null) {@b@Charset charSet = mediaType.getCharSet();@b@if (charSet != null) {@b@return charSet.displayName();@b@}@b@}@b@return defaultCharset;@b@}@b@@b@}