|
@@ -20,6 +20,7 @@ import java.util.function.BiFunction;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
import java.util.stream.Collectors;
|
|
|
+import javax.servlet.ServletRequest;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
@@ -32,6 +33,7 @@ import com.chelvc.framework.common.util.StringUtils;
|
|
|
import com.google.common.collect.Maps;
|
|
|
import lombok.NonNull;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.http.entity.ContentType;
|
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
@@ -173,6 +175,17 @@ public final class HttpUtils {
|
|
|
return HostUtils.DEFAULT_LOCAL_ADDRESS_IPV6.equals(ip) ? HostUtils.LOCAL_ADDRESS : ip;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 判断ContentType是否是multipart/form-data
|
|
|
+ *
|
|
|
+ * @param request 请求对象
|
|
|
+ * @return true/false
|
|
|
+ */
|
|
|
+ public static boolean isMultipart(@NonNull ServletRequest request) {
|
|
|
+ String type = request.getContentType();
|
|
|
+ return type != null && type.contains(ContentType.MULTIPART_FORM_DATA.getMimeType());
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取Body请求参数
|
|
|
*
|
|
@@ -180,10 +193,10 @@ public final class HttpUtils {
|
|
|
* @return 请求体字节数组
|
|
|
* @throws IOException I/O异常
|
|
|
*/
|
|
|
- public static byte[] getBody(@NonNull HttpServletRequest request) throws IOException {
|
|
|
+ public static byte[] getBody(@NonNull ServletRequest request) throws IOException {
|
|
|
try (InputStream input = request.getInputStream()) {
|
|
|
if (input instanceof BufferedRequestWrapper.BufferedServletInputStream) {
|
|
|
- return ((BufferedRequestWrapper.BufferedServletInputStream) input).body();
|
|
|
+ return ((BufferedRequestWrapper.BufferedServletInputStream) input).bytes();
|
|
|
}
|
|
|
return FileUtils.getBytes(input);
|
|
|
}
|
|
@@ -195,7 +208,7 @@ public final class HttpUtils {
|
|
|
* @param request Http请求对象
|
|
|
* @return 参数键/值映射表
|
|
|
*/
|
|
|
- public static Map<String, String[]> getParameters(@NonNull HttpServletRequest request) {
|
|
|
+ public static Map<String, String[]> getParameters(@NonNull ServletRequest request) {
|
|
|
// 获取表单参数
|
|
|
Map<String, String[]> parameters = Maps.newHashMap();
|
|
|
Enumeration<String> enumeration = request.getParameterNames();
|
|
@@ -379,9 +392,8 @@ public final class HttpUtils {
|
|
|
*
|
|
|
* @param request Http请求对象
|
|
|
* @return 请求参数
|
|
|
- * @throws IOException I/O异常
|
|
|
*/
|
|
|
- public static String serialize(@NonNull HttpServletRequest request) throws IOException {
|
|
|
+ public static String serialize(@NonNull ServletRequest request) {
|
|
|
return serialize(request, false);
|
|
|
}
|
|
|
|
|
@@ -391,13 +403,16 @@ public final class HttpUtils {
|
|
|
* @param request Http请求对象
|
|
|
* @param ordering 参数是否排序
|
|
|
* @return 请求参数
|
|
|
- * @throws IOException I/O异常
|
|
|
*/
|
|
|
- public static String serialize(@NonNull HttpServletRequest request, boolean ordering) throws IOException {
|
|
|
+ public static String serialize(@NonNull ServletRequest request, boolean ordering) {
|
|
|
String type = request.getContentType();
|
|
|
if (StringUtils.notEmpty(type) && type.contains(MediaType.APPLICATION_JSON_VALUE)) {
|
|
|
// Body参数
|
|
|
- return new String(getBody(request));
|
|
|
+ try {
|
|
|
+ return new String(getBody(request));
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 表单参数 + URL参数
|