pull/3/head
tanghc 4 years ago
parent c6e5a20a81
commit 95f2ecf7dc
  1. 15
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/UploadUtil.java
  2. 15
      sop-website/src/main/java/com/gitee/sop/websiteserver/util/UploadUtil.java

@ -1,6 +1,6 @@
package com.gitee.sop.servercommon.util; 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.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.MultipartHttpServletRequest;
@ -9,6 +9,7 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
/** /**
* 文件上传工具类 * 文件上传工具类
@ -24,15 +25,19 @@ public class UploadUtil {
* @return * @return
*/ */
public static Collection<MultipartFile> getUploadFiles(HttpServletRequest request) { public static Collection<MultipartFile> getUploadFiles(HttpServletRequest request) {
Map<String, MultipartFile> fileMap = null; MultiValueMap<String, MultipartFile> fileMap = null;
//检查form中是否有enctype="multipart/form-data" //检查form中是否有enctype="multipart/form-data"
if (ServletFileUpload.isMultipartContent(request)) { String contentType = request.getContentType();
if (contentType != null && contentType.toLowerCase().contains("multipart")) {
//将request变成多部分request //将request变成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
fileMap = multiRequest.getFileMap(); fileMap = multiRequest.getMultiFileMap();
} }
return Optional.ofNullable(fileMap) 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()); .orElse(Collections.emptyList());
} }
} }

@ -1,6 +1,6 @@
package com.gitee.sop.websiteserver.util; 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.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.MultipartHttpServletRequest;
@ -9,6 +9,7 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
/** /**
* 文件上传工具类 * 文件上传工具类
@ -24,15 +25,19 @@ public class UploadUtil {
* @return * @return
*/ */
public static Collection<MultipartFile> getUploadFiles(HttpServletRequest request) { public static Collection<MultipartFile> getUploadFiles(HttpServletRequest request) {
Map<String, MultipartFile> fileMap = null; MultiValueMap<String, MultipartFile> fileMap = null;
//检查form中是否有enctype="multipart/form-data" //检查form中是否有enctype="multipart/form-data"
if (ServletFileUpload.isMultipartContent(request)) { String contentType = request.getContentType();
if (contentType != null && contentType.toLowerCase().contains("multipart")) {
//将request变成多部分request //将request变成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
fileMap = multiRequest.getFileMap(); fileMap = multiRequest.getMultiFileMap();
} }
return Optional.ofNullable(fileMap) 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()); .orElse(Collections.emptyList());
} }
} }

Loading…
Cancel
Save