Explorar o código

优化文件上传

zhouhao %!s(int64=7) %!d(string=hai) anos
pai
achega
e81e129926

+ 7 - 1
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/OAuth2Request.java

@@ -20,6 +20,8 @@ package org.hswebframework.web.authorization.oauth2.client.request;
 
 
 import org.hswebframework.web.authorization.oauth2.client.response.OAuth2Response;
 import org.hswebframework.web.authorization.oauth2.client.response.OAuth2Response;
 
 
+import java.io.FileInputStream;
+import java.io.InputStream;
 import java.util.Map;
 import java.util.Map;
 import java.util.function.Consumer;
 import java.util.function.Consumer;
 
 
@@ -43,7 +45,11 @@ public interface OAuth2Request {
      */
      */
     OAuth2Request param(String name, Object value);
     OAuth2Request param(String name, Object value);
 
 
-    OAuth2Request params(Map<String,String> params);
+    OAuth2Request params(Map<String, String> params);
+
+    OAuth2Response upload(String name, InputStream inputStream);
+
+    OAuth2Response upload(String name, InputStream inputStream,String fileName);
 
 
     /**
     /**
      * 设置请求体,将内容根据contentType(默认application/json)序列化为对应的请求数据
      * 设置请求体,将内容根据contentType(默认application/json)序列化为对应的请求数据

+ 2 - 0
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/response/OAuth2Response.java

@@ -21,6 +21,7 @@ package org.hswebframework.web.authorization.oauth2.client.response;
 import org.hswebframework.web.authorization.oauth2.client.exception.OAuth2RequestException;
 import org.hswebframework.web.authorization.oauth2.client.exception.OAuth2RequestException;
 import org.hswebframework.web.oauth2.core.ErrorType;
 import org.hswebframework.web.oauth2.core.ErrorType;
 
 
+import java.io.InputStream;
 import java.util.List;
 import java.util.List;
 import java.util.function.BiConsumer;
 import java.util.function.BiConsumer;
 
 
@@ -31,6 +32,7 @@ import java.util.function.BiConsumer;
  */
  */
 public interface OAuth2Response {
 public interface OAuth2Response {
 
 
+    InputStream asStream();
     /**
     /**
      * @return 结果转为字符串
      * @return 结果转为字符串
      */
      */

+ 20 - 8
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/SimpleOAuth2Request.java

@@ -27,6 +27,8 @@ import org.hswebframework.web.authorization.oauth2.client.request.TokenExpiredCa
 import org.hswebframework.web.authorization.oauth2.client.response.OAuth2Response;
 import org.hswebframework.web.authorization.oauth2.client.response.OAuth2Response;
 import org.hswebframework.web.oauth2.core.ErrorType;
 import org.hswebframework.web.oauth2.core.ErrorType;
 
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.Map;
 import java.util.Map;
 import java.util.function.Consumer;
 import java.util.function.Consumer;
 import java.util.function.Supplier;
 import java.util.function.Supplier;
@@ -59,8 +61,8 @@ public class SimpleOAuth2Request implements OAuth2Request {
     }
     }
 
 
     @Override
     @Override
-    public OAuth2Request onRefreshTokenExpired(TokenExpiredCallBack refreshTokenExpiredCallBack){
-        this.refreshTokenExpiredCallBack=refreshTokenExpiredCallBack;
+    public OAuth2Request onRefreshTokenExpired(TokenExpiredCallBack refreshTokenExpiredCallBack) {
+        this.refreshTokenExpiredCallBack = refreshTokenExpiredCallBack;
         return this;
         return this;
     }
     }
 
 
@@ -70,6 +72,16 @@ public class SimpleOAuth2Request implements OAuth2Request {
         return this;
         return this;
     }
     }
 
 
+    @Override
+    public OAuth2Response upload(String name, InputStream inputStream) {
+        return createUnCheckResponse(() -> request.upload(name, inputStream));
+    }
+
+    @Override
+    public OAuth2Response upload(String name, InputStream inputStream, String fileName) {
+        return createUnCheckResponse(() -> request.upload(name, inputStream, fileName));
+    }
+
     @Override
     @Override
     public OAuth2Request params(Map<String, String> params) {
     public OAuth2Request params(Map<String, String> params) {
         request.params(params);
         request.params(params);
@@ -120,17 +132,17 @@ public class SimpleOAuth2Request implements OAuth2Request {
     private volatile SimpleOAuth2Response auth2Response;
     private volatile SimpleOAuth2Response auth2Response;
 
 
     protected SimpleOAuth2Response createNativeResponse(Supplier<Response> responseSupplier) {
     protected SimpleOAuth2Response createNativeResponse(Supplier<Response> responseSupplier) {
-        SimpleOAuth2Response response=  new SimpleOAuth2Response(responseSupplier.get(), convertHandler, responseJudge);
+        SimpleOAuth2Response response = new SimpleOAuth2Response(responseSupplier.get(), convertHandler, responseJudge);
 
 
 
 
-        return auth2Response =response;
+        return auth2Response = response;
     }
     }
 
 
     protected OAuth2Response createResponse(Supplier<Response> responseSupplier) {
     protected OAuth2Response createResponse(Supplier<Response> responseSupplier) {
         createNativeResponse(responseSupplier);
         createNativeResponse(responseSupplier);
         if (null != expiredCallBack) {
         if (null != expiredCallBack) {
             //判定token是否过期,过期后先执行回调进行操作如更新token,并尝试重新请求
             //判定token是否过期,过期后先执行回调进行操作如更新token,并尝试重新请求
-            auth2Response.judgeError(ErrorType.EXPIRED_TOKEN,() -> {
+            auth2Response.judgeError(ErrorType.EXPIRED_TOKEN, () -> {
 
 
                 //调用回调,并指定重试的操作(重新请求)
                 //调用回调,并指定重试的操作(重新请求)
                 expiredCallBack.call(() -> createNativeResponse(responseSupplier));
                 expiredCallBack.call(() -> createNativeResponse(responseSupplier));
@@ -141,14 +153,14 @@ public class SimpleOAuth2Request implements OAuth2Request {
         }
         }
         if (null != refreshTokenExpiredCallBack) {
         if (null != refreshTokenExpiredCallBack) {
             //判定token是否有效,无效的token将重新申请token
             //判定token是否有效,无效的token将重新申请token
-            auth2Response.judgeError(ErrorType.INVALID_TOKEN,() -> {
+            auth2Response.judgeError(ErrorType.INVALID_TOKEN, () -> {
                 //调用回调,并指定重试的操作(重新请求)
                 //调用回调,并指定重试的操作(重新请求)
                 refreshTokenExpiredCallBack.call(() -> createNativeResponse(responseSupplier));
                 refreshTokenExpiredCallBack.call(() -> createNativeResponse(responseSupplier));
                 //返回重试后的response
                 //返回重试后的response
                 return auth2Response;
                 return auth2Response;
             });
             });
             //判定refresh_token是否过期,过期后先执行回调进行操作如更新token,并尝试重新请求
             //判定refresh_token是否过期,过期后先执行回调进行操作如更新token,并尝试重新请求
-            auth2Response.judgeError(ErrorType.EXPIRED_REFRESH_TOKEN,() -> {
+            auth2Response.judgeError(ErrorType.EXPIRED_REFRESH_TOKEN, () -> {
                 //调用回调,并指定重试的操作(重新请求)
                 //调用回调,并指定重试的操作(重新请求)
                 refreshTokenExpiredCallBack.call(() -> createNativeResponse(responseSupplier));
                 refreshTokenExpiredCallBack.call(() -> createNativeResponse(responseSupplier));
                 //返回重试后的response
                 //返回重试后的response
@@ -156,7 +168,7 @@ public class SimpleOAuth2Request implements OAuth2Request {
             });
             });
 
 
             //如果是invalid token 也将重新生成token
             //如果是invalid token 也将重新生成token
-            auth2Response.judgeError(ErrorType.INVALID_TOKEN,() -> {
+            auth2Response.judgeError(ErrorType.INVALID_TOKEN, () -> {
                 //调用回调,并指定重试的操作(重新请求)
                 //调用回调,并指定重试的操作(重新请求)
                 refreshTokenExpiredCallBack.call(() -> createNativeResponse(responseSupplier));
                 refreshTokenExpiredCallBack.call(() -> createNativeResponse(responseSupplier));
                 //返回重试后的response
                 //返回重试后的response

+ 15 - 3
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/SimpleOAuth2Response.java

@@ -19,6 +19,7 @@
 package org.hswebframework.web.authorization.oauth2.client.simple.request;
 package org.hswebframework.web.authorization.oauth2.client.simple.request;
 
 
 import org.hswebframework.expands.request.http.Response;
 import org.hswebframework.expands.request.http.Response;
+import org.hswebframework.web.authorization.oauth2.client.exception.OAuth2RequestException;
 import org.hswebframework.web.authorization.oauth2.client.request.ResponseConvertHandler;
 import org.hswebframework.web.authorization.oauth2.client.request.ResponseConvertHandler;
 import org.hswebframework.web.authorization.oauth2.client.request.ResponseJudge;
 import org.hswebframework.web.authorization.oauth2.client.request.ResponseJudge;
 import org.hswebframework.web.authorization.oauth2.client.response.OAuth2Response;
 import org.hswebframework.web.authorization.oauth2.client.response.OAuth2Response;
@@ -26,7 +27,10 @@ import org.hswebframework.web.authorization.oauth2.client.response.ResponseConve
 import org.hswebframework.web.oauth2.core.ErrorType;
 import org.hswebframework.web.oauth2.core.ErrorType;
 import org.slf4j.Logger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
+import org.springframework.util.StreamUtils;
 
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.List;
 import java.util.List;
 import java.util.function.BiConsumer;
 import java.util.function.BiConsumer;
 import java.util.function.Supplier;
 import java.util.function.Supplier;
@@ -50,8 +54,9 @@ public class SimpleOAuth2Response implements OAuth2Response {
 
 
     private OAuth2Response proxy = this;
     private OAuth2Response proxy = this;
 
 
+    private InputStream inputStream;
 
 
-    public void judgeError(ErrorType ifError,Supplier<OAuth2Response> expiredCallBack) {
+    public void judgeError(ErrorType ifError, Supplier<OAuth2Response> expiredCallBack) {
 
 
         if (errorType == ifError) {
         if (errorType == ifError) {
             //尝试执行认证过时回调进行重试,并返回重试的结果
             //尝试执行认证过时回调进行重试,并返回重试的结果
@@ -64,7 +69,7 @@ public class SimpleOAuth2Response implements OAuth2Response {
 
 
                 if (type == ifError) {
                 if (type == ifError) {
                     //重试后依然是相同的错误,可能是错误类型判断错误或者服务端的问题?
                     //重试后依然是相同的错误,可能是错误类型判断错误或者服务端的问题?
-                    logger.error("still error [{}], maybe judge error or auth server error! {}",ifError,retryResponse,Thread.currentThread().getStackTrace());
+                    logger.error("still error [{}], maybe judge error or auth server error! {}", ifError, retryResponse, Thread.currentThread().getStackTrace());
                 } else {
                 } else {
                     errorType = type;
                     errorType = type;
                 }
                 }
@@ -78,11 +83,15 @@ public class SimpleOAuth2Response implements OAuth2Response {
                                 ResponseConvertHandler convertHandler,
                                 ResponseConvertHandler convertHandler,
                                 ResponseJudge responseJudge) {
                                 ResponseJudge responseJudge) {
         this.convertHandler = convertHandler;
         this.convertHandler = convertHandler;
-        data = UnCheck.unCheck(response::asBytes);
+        inputStream = UnCheck.unCheck(response::asStream);
         status = response.getCode();
         status = response.getCode();
         errorType = responseJudge.judge(this);
         errorType = responseJudge.judge(this);
     }
     }
 
 
+    public InputStream asStream() {
+        return inputStream;
+    }
+
     @Override
     @Override
     public String asString() {
     public String asString() {
         if (asBytes() == null) {
         if (asBytes() == null) {
@@ -93,6 +102,9 @@ public class SimpleOAuth2Response implements OAuth2Response {
 
 
     @Override
     @Override
     public byte[] asBytes() {
     public byte[] asBytes() {
+        if (data == null) {
+            data = UnCheck.unCheck(() -> StreamUtils.copyToByteArray(inputStream));
+        }
         return data;
         return data;
     }
     }