diff --git a/spring-web/src/main/java/org/springframework/http/MediaTypeFactory.java b/spring-web/src/main/java/org/springframework/http/MediaTypeFactory.java index 7ad0969f2b9..8487ab5b886 100644 --- a/spring-web/src/main/java/org/springframework/http/MediaTypeFactory.java +++ b/spring-web/src/main/java/org/springframework/http/MediaTypeFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -112,10 +112,12 @@ public final class MediaTypeFactory { * @return the corresponding media types, or an empty list if none found */ public static List getMediaTypes(@Nullable String filename) { - return Optional.ofNullable(StringUtils.getFilenameExtension(filename)) - .map(s -> s.toLowerCase(Locale.ENGLISH)) - .map(fileExtensionToMediaTypes::get) - .orElse(Collections.emptyList()); + List mediaTypes = null; + String ext = StringUtils.getFilenameExtension(filename); + if (ext != null) { + mediaTypes = fileExtensionToMediaTypes.get(ext.toLowerCase(Locale.ENGLISH)); + } + return (mediaTypes != null ? mediaTypes : Collections.emptyList()); } } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java index 0cd981b6dea..03d5ca02502 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java @@ -736,7 +736,10 @@ public class ResourceHttpRequestHandler extends WebContentGenerator mediaType = this.mediaTypes.get(ext.toLowerCase(Locale.ENGLISH)); } if (mediaType == null) { - mediaType = MediaTypeFactory.getMediaType(filename).orElse(null); + List mediaTypes = MediaTypeFactory.getMediaTypes(filename); + if (!CollectionUtils.isEmpty(mediaTypes)) { + mediaType = mediaTypes.get(0); + } } if (mediaType != null) { result = mediaType;