java mediatype属性_基于ServletRequest实现请求获取内容类型的媒体类型MediaType、字符集Charset的工具类ContentTypeUtil...

一、源码说明

基于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@}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值