gateway支持文件上传

1.x
tanghc 5 years ago
parent 738572b217
commit 9d9c5ced4c
  1. 17
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/ServerWebExchangeUtil.java
  2. 20
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/common/ServletFileUploadUtil.java

@ -2,7 +2,7 @@ package com.gitee.sop.gatewaycommon.gateway;
import com.alibaba.fastjson.JSON;
import com.gitee.sop.gatewaycommon.bean.SopConstants;
import com.gitee.sop.gatewaycommon.gateway.common.ServletFileUploadUtil;
import com.gitee.sop.gatewaycommon.gateway.common.FileUploadHttpServletRequest;
import com.gitee.sop.gatewaycommon.param.ApiParam;
import com.gitee.sop.gatewaycommon.util.RequestUtil;
import org.apache.commons.lang3.StringUtils;
@ -14,6 +14,7 @@ import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -69,7 +70,7 @@ public class ServerWebExchangeUtil {
params = JSON.parseObject(cachedBody);
} else if (StringUtils.containsIgnoreCase(contentTypeStr, "multipart")) {
// 如果是文件上传请求
HttpServletRequest fileUploadRequest = ServletFileUploadUtil.getFileUploadRequest(exchange, cachedBody);
HttpServletRequest fileUploadRequest = getFileUploadRequest(exchange, cachedBody);
params = RequestUtil.convertMultipartRequestToMap(fileUploadRequest);
} else {
params = RequestUtil.parseQueryToMap(cachedBody);
@ -112,5 +113,17 @@ public class ServerWebExchangeUtil {
.build();
}
/**
* 获取一个文件上传request
*
* @param exchange 当前ServerWebExchange
* @param requestBody 上传文件请求体内容
* @return 返回文件上传request
*/
public static HttpServletRequest getFileUploadRequest(ServerWebExchange exchange, String requestBody) {
byte[] data = requestBody.getBytes(StandardCharsets.UTF_8);
return new FileUploadHttpServletRequest(exchange.getRequest(), data);
}
}

@ -1,20 +0,0 @@
package com.gitee.sop.gatewaycommon.gateway.common;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.server.ServerWebExchange;
import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
/**
* @author tanghc
*/
@Slf4j
public class ServletFileUploadUtil {
public static HttpServletRequest getFileUploadRequest(ServerWebExchange exchange, String requestBody) {
byte[] data = requestBody.getBytes(StandardCharsets.UTF_8);
return new FileUploadHttpServletRequest(exchange.getRequest(), data);
}
}
Loading…
Cancel
Save