RestTemplate获取文件的contentType

codecraft

org/springframework/http/converter/AbstractHttpMessageConverter.java

protected void addDefaultHeaders(HttpHeaders headers, T t, MediaType contentType) throws IOException{
        if (headers.getContentType() == null) {
            MediaType contentTypeToUse = contentType;
            if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
                contentTypeToUse = getDefaultContentType(t);
            }
            else if (MediaType.APPLICATION_OCTET_STREAM.equals(contentType)) {
                MediaType mediaType = getDefaultContentType(t);
                contentTypeToUse = (mediaType != null ? mediaType : contentTypeToUse);
            }
            if (contentTypeToUse != null) {
                if (contentTypeToUse.getCharset() == null) {
                    Charset defaultCharset = getDefaultCharset();
                    if (defaultCharset != null) {
                        contentTypeToUse = new MediaType(contentTypeToUse, defaultCharset);
                    }
                }
                headers.setContentType(contentTypeToUse);
            }
        }
        if (headers.getContentLength() < 0 && !headers.containsKey(HttpHeaders.TRANSFER_ENCODING)) {
            Long contentLength = getContentLength(t, headers.getContentType());
            if (contentLength != null) {
                headers.setContentLength(contentLength);
            }
        }
    }

org/springframework/http/converter/ResourceHttpMessageConverter.java

@Override
    protected MediaType getDefaultContentType(Resource resource) {
        if (jafPresent) {
            return ActivationMediaTypeFactory.getMediaType(resource);
        }
        else {
            return MediaType.APPLICATION_OCTET_STREAM;
        }
    }

org/springframework/http/converter/ActivationMediaTypeFactory.java

class ActivationMediaTypeFactory {

    private static final FileTypeMap fileTypeMap;

    static {
        fileTypeMap = loadFileTypeMapFromContextSupportModule();
    }

    private static FileTypeMap loadFileTypeMapFromContextSupportModule() {
        // See if we can find the extended mime.types from the context-support module...
        Resource mappingLocation = new ClassPathResource("org/springframework/mail/javamail/mime.types");
        if (mappingLocation.exists()) {
            InputStream inputStream = null;
            try {
                inputStream = mappingLocation.getInputStream();
                return new MimetypesFileTypeMap(inputStream);
            }
            catch (IOException ex) {
                // ignore
            }
            finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    }
                    catch (IOException ex) {
                        // ignore
                    }
                }
            }
        }
        return FileTypeMap.getDefaultFileTypeMap();
    }

    public static MediaType getMediaType(Resource resource) {
        String filename = resource.getFilename();
        if (filename != null) {
            String mediaType = fileTypeMap.getContentType(filename);
            if (StringUtils.hasText(mediaType)) {
                return MediaType.parseMediaType(mediaType);
            }
        }
        return null;
    }
}

想获取最新内容,请关注微信公众号
图片描述

阅读 4.2k

code-craft
spring boot , docker and so on 欢迎关注微信公众号: geek_luandun

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下了或重如泰山或轻如鸿毛的几笔。

11.5k 声望
1.9k 粉丝
0 条评论
你知道吗?

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下了或重如泰山或轻如鸿毛的几笔。

11.5k 声望
1.9k 粉丝
宣传栏