返回文件输出流

This commit is contained in:
zhangyx
2021-03-25 15:07:48 +08:00
parent 47d1f3761e
commit 15cde06082
12 changed files with 316 additions and 12 deletions

View File

@ -0,0 +1,39 @@
package com.chinaunicom.mall.ebtp.extend.feign.client;
import com.chinaunicom.mall.ebtp.extend.feign.factory.FeignConfiguration;
import com.chinaunicom.mall.ebtp.extend.feign.factory.DocumentCenterServiceFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
* 连接山分的文档中心服务
*
* @author Ajaxfan
*/
@FeignClient(value = "${mconfig.feign.name.documentcenter}",
fallbackFactory = DocumentCenterServiceFallbackFactory.class,
configuration = FeignConfiguration.class)
public interface DocumentCenterService {
/**
* 通过附件id查询明细
*
* @param fileId
* @return
*/
@RequestMapping(method = RequestMethod.POST, value = "v1.0/files/downloadFileAllStream")
String getObjectDetail(@RequestParam("fileId") String fileId);
/**
* 通过附件id查询明细
*
* @param fileId
* @return
*/
@RequestMapping(method = RequestMethod.POST, value = "v1.0/files/downloadStream")
String getFileObjectDetail(@RequestParam("fileId") String fileId);
}

View File

@ -0,0 +1,22 @@
package com.chinaunicom.mall.ebtp.extend.feign.factory;
import com.chinaunicom.mall.ebtp.extend.feign.client.DocumentCenterService;
import com.chinaunicom.mall.ebtp.extend.feign.fallback.DocumentCenterServiceFallbackImpl;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
/**
* 断路器
*
* @author dino
* @date 2020/11/26 9:39
*/
@Component
public class DocumentCenterServiceFallbackFactory implements FallbackFactory<DocumentCenterService> {
@Override
public DocumentCenterService create(Throwable throwable) {
DocumentCenterServiceFallbackImpl remoteProcessServiceFallback = new DocumentCenterServiceFallbackImpl();
remoteProcessServiceFallback.setCause(throwable);
return remoteProcessServiceFallback;
}
}

View File

@ -0,0 +1,92 @@
package com.chinaunicom.mall.ebtp.extend.feign.factory;
import com.alibaba.fastjson.JSONObject;
import com.chinaunicom.mall.ebtp.extend.feign.utils.UrlConstants;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
@Slf4j
@Configuration
public class FeignConfiguration implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if(attributes!=null){
HttpServletRequest request = attributes.getRequest();
log.info("token=======requestTemplate.headers"+requestTemplate.headers());
String access_token = request.getHeader("Authorization");
log.info("token======="+access_token);
if(access_token==null || StringUtils.isBlank(access_token)){
access_token = getAccessToken();
log.info("token=======access_token==="+access_token);
requestTemplate.header(HttpHeaders.AUTHORIZATION, "Bearer "+access_token);
}
}else{
String access_token = getAccessToken();
log.info("token=======attributes==null==="+access_token);
requestTemplate.header(HttpHeaders.AUTHORIZATION, "Bearer "+access_token);
}
}
public static String getAccessToken () {
StringBuffer strBf = new StringBuffer();
try {
URL realUrl = new URL(UrlConstants.clientHttpUrl);
//将realUrl以 open方法返回的urlConnection 连接强转为HttpURLConnection连接 (标识一个url所引用的远程对象连接)
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();// 此时cnnection只是为一个连接对象,待连接中
//设置连接输出流为true,默认false (post请求是以流的方式隐式的传递参数)
connection.setDoOutput(true);
//设置连接输入流为true
connection.setDoInput(true);
//设置请求方式为post
connection.setRequestMethod("POST");
//post请求缓存设为false
connection.setUseCaches(false);
//设置该HttpURLConnection实例是否自动执行重定向
connection.setInstanceFollowRedirects(true);
//设置请求头里面的各个属性 (以下为设置内容的类型,设置为经过urlEncoded编码过的from参数)
connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
//建立连接 (请求未开始,直到connection.getInputStream()方法调用时才发起,以上各个参数设置需在此方法之前进行)
connection.connect();
//创建输入输出流,用于往连接里面输出携带的参数,(输出内容为?后面的内容)
DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
// String query = data.toString();
// //将参数输出到连接
// dataout.write(query.getBytes("UTF-8"));
// 输出完成后刷新并关闭流
dataout.flush();
dataout.close(); // 重要且易忽略步骤 (关闭流,切记!)
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String lines;
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
strBf.append(lines);
}
reader.close();
connection.disconnect();
log.info("toke返回数据---------------------- "+strBf.toString());
} catch (Exception e) {
log.info("toke返回数据---------------------- "+e.getMessage());
}
JSONObject json=JSONObject.parseObject(strBf.toString());
if((boolean)json.get("success")){
return ((JSONObject)json.get("data")).get("value").toString();
}
return null;
}
}

View File

@ -0,0 +1,32 @@
package com.chinaunicom.mall.ebtp.extend.feign.fallback;
import com.chinaunicom.mall.ebtp.extend.feign.client.DocumentCenterService;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* 熔断
*
* @author dino
* @date 2020/11/26 9:39
*/
@Slf4j
@Component
public class DocumentCenterServiceFallbackImpl implements DocumentCenterService {
@Setter
private Throwable cause;
@Override
public String getObjectDetail(String fileId) {
return null;
}
@Override
public String getFileObjectDetail(String fileId) {
return null;
}
}

View File

@ -0,0 +1,17 @@
package com.chinaunicom.mall.ebtp.extend.feign.utils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "document")
public class UrlConstants {
public static String clientHttpUrl;
@Value("${document.clientHttpUrl}")
public void setClientHttpUrl(String confPath) {
clientHttpUrl = confPath;
}
}

View File

@ -11,6 +11,8 @@ import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.io.IOException;
@RestController
@Api(tags = "模板仓库")
@ -56,10 +58,11 @@ public class TemplateWarehouseController {
*/
@ApiOperation("获得客户端配置文件文件id")
@GetMapping("/client/file")
public BaseResponse<BizBidTemplateWarehouse> getClientFile(){
public BaseResponse<FileOutputStream> getClientFile() throws IOException {
String type = "ipassConfigFile";
BizBidTemplateWarehouse templateWarehouse = templateWarehouseService.getTemplateByType(type);
return BaseResponse.success(templateWarehouse);
FileOutputStream clientVersion = bizBidClientVersionService.downloadFileByObjectId(templateWarehouse.getDocumentCenterId());
return BaseResponse.success(clientVersion);
}
/**

View File

@ -4,6 +4,9 @@ package com.chinaunicom.mall.ebtp.extend.templatewarehouse.sevice;
import com.chinaunicom.mall.ebtp.common.base.service.IBaseService;
import com.chinaunicom.mall.ebtp.extend.templatewarehouse.entity.BizBidClientVersion;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 对数据表 biz_bid_client_version 操作的 service
* @author Auto create
@ -18,5 +21,5 @@ public interface BizBidClientVersionService extends IBaseService<BizBidClientVer
*/
BizBidClientVersion getClientByVersion(String version);
FileOutputStream downloadFileByObjectId(String oId) throws IOException;
}

View File

@ -2,23 +2,52 @@ package com.chinaunicom.mall.ebtp.extend.templatewarehouse.sevice.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.convertor.ModelConvertor;
import com.chinaunicom.mall.ebtp.extend.feign.client.DocumentCenterService;
import com.chinaunicom.mall.ebtp.extend.templatewarehouse.entity.BizBidClientVersion;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.chinaunicom.mall.ebtp.extend.templatewarehouse.dao.BizBidClientVersionMapper;
import com.chinaunicom.mall.ebtp.extend.templatewarehouse.sevice.BizBidClientVersionService;
import com.chinaunicom.mall.ebtp.common.base.service.impl.BaseServiceImpl;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Optional;
/**
* 对数据表 biz_bid_client_version 操作的 serviceImpl
* @author Auto create
*
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class BizBidClientVersionServiceImpl extends BaseServiceImpl<BizBidClientVersionMapper, BizBidClientVersion> implements BizBidClientVersionService {
private final DocumentCenterService documentCenterService;
private final ModelConvertor modelConvertor;
@Override
public BizBidClientVersion getClientByVersion(String version) {
QueryWrapper<BizBidClientVersion> query = new QueryWrapper<>(new BizBidClientVersion().setIpassVersion(version));
return this.getOne(query);
}
@Override
public FileOutputStream downloadFileByObjectId(String objectId) throws IOException {
Optional<byte[]> optionalBytes = modelConvertor.toByteArray(documentCenterService.getObjectDetail(objectId));
FileOutputStream fileOutputStream = null;
if(optionalBytes.isPresent()){
ByteArrayOutputStream fileStream = new ByteArrayOutputStream();
fileOutputStream.write(fileStream.toByteArray());
}
return fileOutputStream;
}
}

View File

@ -116,7 +116,19 @@ mybatis-plus:
db-config:
# logic-delete-field: flag # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2) @TableLogic
logic-delete-value: deleted # 逻辑已删除值
logic-not-delete-value: normal # 逻辑未删除
logic-not-delete-value: normal # 逻辑未删除
# --------------feign ------------
feign:
httpclient:
enabled: false
okhttp:
enabled: true
client:
config:
default:
connect-timeout: 20000
read-timeout: 20000
hystrix:
command:
@ -150,7 +162,12 @@ mconfig:
resps: biz-service-ebtp-resps #应答结构化服务
rsms: biz-service-ebtp-rsms #评审结构化服务
tender: biz-service-ebtp-tender #投标服务
documentcenter: core-service-document-center #文档中心
document:
clientHttpUrl: http://10.242.31.158:8100/auth/oauth/token?grant_type=client_credentials&client_id=bVS46ElU&client_secret=58ea04ba02475c8da2321cc99849d2a10f15b749
# 用户暴露给 prometheus 的健康数据
management:
endpoints:

View File

@ -120,7 +120,20 @@ mybatis-plus:
# logic-delete-field: flag # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2) @TableLogic
logic-delete-value: deleted # 逻辑已删除值
logic-not-delete-value: normal # 逻辑未删除值
# --------------feign ------------
feign:
httpclient:
enabled: false
okhttp:
enabled: true
client:
config:
default:
connect-timeout: 20000
read-timeout: 20000
hystrix:
command:
default:
@ -153,8 +166,13 @@ mconfig:
resps: biz-service-ebtp-resps #应答结构化服务
rsms: biz-service-ebtp-rsms #评审结构化服务
tender: biz-service-ebtp-tender #投标服务
# 用户暴露给 prometheus 的健康数据
documentcenter: core-service-document-center #文档中心
document:
clientHttpUrl: http://10.242.31.158:8100/auth/oauth/token?grant_type=client_credentials&client_id=bVS46ElU&client_secret=58ea04ba02475c8da2321cc99849d2a10f15b749
# 用户暴露给 prometheus 的健康数据
management:
endpoints:
web:

View File

@ -125,7 +125,19 @@ mybatis-plus:
# logic-delete-field: flag # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2) @TableLogic
logic-delete-value: deleted # 逻辑已删除值
logic-not-delete-value: normal # 逻辑未删除值
# --------------feign ------------
feign:
httpclient:
enabled: false
okhttp:
enabled: true
client:
config:
default:
connect-timeout: 20000
read-timeout: 20000
hystrix:
command:
default:
@ -158,7 +170,11 @@ mconfig:
resps: biz-service-ebtp-resps #应答结构化服务
rsms: biz-service-ebtp-rsms #评审结构化服务
tender: biz-service-ebtp-tender #投标服务
documentcenter: core-service-document-center #文档中心
document:
clientHttpUrl: http://10.242.31.158:8100/auth/oauth/token?grant_type=client_credentials&client_id=bVS46ElU&client_secret=58ea04ba02475c8da2321cc99849d2a10f15b749
# 用户暴露给 prometheus 的健康数据
management:
endpoints:

View File

@ -123,7 +123,19 @@ mybatis-plus:
# logic-delete-field: flag # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2) @TableLogic
logic-delete-value: deleted # 逻辑已删除值
logic-not-delete-value: normal # 逻辑未删除值
# --------------feign ------------
feign:
httpclient:
enabled: false
okhttp:
enabled: true
client:
config:
default:
connect-timeout: 20000
read-timeout: 20000
hystrix:
command:
default:
@ -156,7 +168,11 @@ mconfig:
resps: biz-service-ebtp-resps #应答结构化服务
rsms: biz-service-ebtp-rsms #评审结构化服务
tender: biz-service-ebtp-tender #投标服务
documentcenter: core-service-document-center #文档中心
document:
clientHttpUrl: http://10.242.31.158:8100/auth/oauth/token?grant_type=client_credentials&client_id=bVS46ElU&client_secret=58ea04ba02475c8da2321cc99849d2a10f15b749
# 用户暴露给 prometheus 的健康数据
management:
endpoints: