From 95f2ecf7dc1c4bb06e56a922bcd970834aee35ae Mon Sep 17 00:00:00 2001 From: tanghc Date: Mon, 14 Sep 2020 13:27:13 +0800 Subject: [PATCH] 4.0.3 --- .../gitee/sop/servercommon/util/UploadUtil.java | 15 ++++++++++----- .../gitee/sop/websiteserver/util/UploadUtil.java | 15 ++++++++++----- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/UploadUtil.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/UploadUtil.java index 201d3d03..5bf4dbfd 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/UploadUtil.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/UploadUtil.java @@ -1,6 +1,6 @@ package com.gitee.sop.servercommon.util; -import org.apache.commons.fileupload.servlet.ServletFileUpload; +import org.springframework.util.MultiValueMap; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; @@ -9,6 +9,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; /** * 文件上传工具类 @@ -24,15 +25,19 @@ public class UploadUtil { * @return */ public static Collection getUploadFiles(HttpServletRequest request) { - Map fileMap = null; + MultiValueMap fileMap = null; //检查form中是否有enctype="multipart/form-data" - if (ServletFileUpload.isMultipartContent(request)) { + String contentType = request.getContentType(); + if (contentType != null && contentType.toLowerCase().contains("multipart")) { //将request变成多部分request MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; - fileMap = multiRequest.getFileMap(); + fileMap = multiRequest.getMultiFileMap(); } return Optional.ofNullable(fileMap) - .map(Map::values) + .map(Map::entrySet) + .map(entry -> entry.stream() + .flatMap(e -> e.getValue().stream()) + .collect(Collectors.toList())) .orElse(Collections.emptyList()); } } diff --git a/sop-website/src/main/java/com/gitee/sop/websiteserver/util/UploadUtil.java b/sop-website/src/main/java/com/gitee/sop/websiteserver/util/UploadUtil.java index 565d4cf4..5cc2729f 100644 --- a/sop-website/src/main/java/com/gitee/sop/websiteserver/util/UploadUtil.java +++ b/sop-website/src/main/java/com/gitee/sop/websiteserver/util/UploadUtil.java @@ -1,6 +1,6 @@ package com.gitee.sop.websiteserver.util; -import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload; +import org.springframework.util.MultiValueMap; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; @@ -9,6 +9,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; /** * 文件上传工具类 @@ -24,15 +25,19 @@ public class UploadUtil { * @return */ public static Collection getUploadFiles(HttpServletRequest request) { - Map fileMap = null; + MultiValueMap fileMap = null; //检查form中是否有enctype="multipart/form-data" - if (ServletFileUpload.isMultipartContent(request)) { + String contentType = request.getContentType(); + if (contentType != null && contentType.toLowerCase().contains("multipart")) { //将request变成多部分request MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; - fileMap = multiRequest.getFileMap(); + fileMap = multiRequest.getMultiFileMap(); } return Optional.ofNullable(fileMap) - .map(map -> map.values()) + .map(Map::entrySet) + .map(entry -> entry.stream() + .flatMap(e -> e.getValue().stream()) + .collect(Collectors.toList())) .orElse(Collections.emptyList()); } }