feat:download file from kingdee
This commit is contained in:
parent
1df2d374c7
commit
a370cf0873
@ -0,0 +1,43 @@
|
||||
package com.ensign.ensigngateway.conf;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* @Description: TODO
|
||||
* @Date: 5/11/2024 2:25 pm
|
||||
* @Created: by ZZSLL
|
||||
*/
|
||||
|
||||
@Component
|
||||
public class LoggingGatewayFilterFactory extends AbstractGatewayFilterFactory<LoggingGatewayFilterFactory.Config> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(LoggingGatewayFilterFactory.class);
|
||||
|
||||
public LoggingGatewayFilterFactory() {
|
||||
super(Config.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GatewayFilter apply(Config config) {
|
||||
return (exchange, chain) -> {
|
||||
logger.info("Request: {} {}", exchange.getRequest().getMethod(), exchange.getRequest().getURI());
|
||||
|
||||
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
|
||||
logger.info("Response status code: {}", exchange.getResponse().getStatusCode());
|
||||
}));
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "LoggingGatewayFilter";
|
||||
}
|
||||
|
||||
public static class Config {
|
||||
// 可以添加过滤器的配置信息
|
||||
}
|
||||
}
|
@ -36,7 +36,7 @@ public class ProxyFilter implements GlobalFilter {
|
||||
log.info("path {}", path);
|
||||
|
||||
String userId = exchange.getRequest().getHeaders().getFirst("userid");
|
||||
if (path.startsWith("/ierp/kapi/")) {
|
||||
if (path.startsWith("/ierp/")) {
|
||||
log.info("userId: {}", userId);
|
||||
List<String> unauthorizedPaths = Arrays.asList(
|
||||
"/ierp/kapi/v2/yem/yem_receipt/yem_crm_marketactivity/getMarketactivities",
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.ensign.crm.module.crm.controller.crm;
|
||||
|
||||
import com.ensign.crm.framework.common.pojo.CommonResult;
|
||||
import com.ensign.crm.module.crm.exception.AllKingdeeException;
|
||||
import com.ensign.crm.module.crm.service.ProxyService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
@ -9,10 +10,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.security.PermitAll;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@ -43,12 +42,12 @@ public class ProxyController {
|
||||
proxyService.doProxy(request, response);
|
||||
}
|
||||
|
||||
// @PostMapping(value = "/file/do/**")
|
||||
// @Operation(summary = "转发接口_文件")
|
||||
// @PreAuthorize("@ss.hasPermission('crm:proxy:all')")
|
||||
// public CommonResult<Object> proxyFile(HttpServletRequest request, MultipartFile file) throws IOException, URISyntaxException, AllKingdeeException {
|
||||
// return CommonResult.success(proxyService.doProxyFile(request, file));
|
||||
// }
|
||||
@PostMapping(value = "/file/do/**")
|
||||
@Operation(summary = "转发接口_文件")
|
||||
@PreAuthorize("@ss.hasPermission('crm:proxy:all')")
|
||||
public CommonResult<Object> proxyFile(HttpServletRequest request, MultipartFile file) throws IOException, URISyntaxException, AllKingdeeException {
|
||||
return CommonResult.success(proxyService.doProxyFile(request, file));
|
||||
}
|
||||
|
||||
@GetMapping("/file/read/do")
|
||||
@PermitAll
|
||||
@ -57,6 +56,13 @@ public class ProxyController {
|
||||
proxyService.doGetImage(path, response);
|
||||
}
|
||||
|
||||
@GetMapping("/file/read.file/do")
|
||||
@PermitAll
|
||||
@Operation(summary = "获取图片")
|
||||
public void proxyReadFile(@RequestParam String url, @RequestParam String file_name, HttpServletResponse response) throws AllKingdeeException, IOException, URISyntaxException {
|
||||
proxyService.doGetFile(url, file_name, response);
|
||||
}
|
||||
|
||||
// @RequestMapping(value = "/unauth/do/**", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
// @Operation(summary = "转发接口")
|
||||
// @PermitAll
|
||||
|
@ -1,9 +1,6 @@
|
||||
package com.ensign.crm.module.crm.model;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import org.springframework.boot.context.properties.ConstructorBinding;
|
||||
|
||||
/**
|
||||
* @Description: TODO
|
||||
@ -12,7 +9,7 @@ import org.springframework.boot.context.properties.ConstructorBinding;
|
||||
*/
|
||||
|
||||
@Data
|
||||
public class ImageStorage {
|
||||
public class FileStorage {
|
||||
private String fileName;
|
||||
private Long fileSize;
|
||||
private String mediaType;
|
@ -1,7 +1,7 @@
|
||||
package com.ensign.crm.module.crm.service;
|
||||
|
||||
import com.ensign.crm.module.crm.exception.AllKingdeeException;
|
||||
import com.ensign.crm.module.crm.model.ImageStorage;
|
||||
import com.ensign.crm.module.crm.model.FileStorage;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
@ -25,20 +25,20 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class ImageService {
|
||||
public class KingdeeFileService {
|
||||
|
||||
@Resource
|
||||
private RedisTemplate<String, ImageStorage> redisTemplate;
|
||||
private RedisTemplate<String, FileStorage> redisTemplate;
|
||||
|
||||
private final String CACHE_PREFIX = "kingdee:temp_file";
|
||||
|
||||
public byte[] downloadImage(String imageUrl) throws IOException, AllKingdeeException {
|
||||
public byte[] downloadFile(String imageUrl) throws IOException, AllKingdeeException {
|
||||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
HttpGet httpGet = new HttpGet(imageUrl);
|
||||
CloseableHttpResponse response = httpClient.execute(httpGet);
|
||||
if (response.getStatusLine().getStatusCode() != 200) {
|
||||
String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
|
||||
throw new AllKingdeeException("下载图片失败!" + responseBody);
|
||||
throw new AllKingdeeException("下载文件失败!" + responseBody);
|
||||
}
|
||||
try {
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
@ -50,11 +50,19 @@ public class ImageService {
|
||||
}
|
||||
}
|
||||
|
||||
public void saveImage(String key, ImageStorage imageData) {
|
||||
public void saveImage(String key, FileStorage imageData) {
|
||||
redisTemplate.opsForValue().set(CACHE_PREFIX + key, imageData, 2, TimeUnit.HOURS);
|
||||
}
|
||||
|
||||
public ImageStorage getImage(String key) {
|
||||
public FileStorage getImage(String key) {
|
||||
return redisTemplate.opsForValue().get(CACHE_PREFIX + key);
|
||||
}
|
||||
|
||||
public FileStorage getFile(String key) {
|
||||
return redisTemplate.opsForValue().get(CACHE_PREFIX + key);
|
||||
}
|
||||
|
||||
public void saveFile(String key, FileStorage fileData) {
|
||||
redisTemplate.opsForValue().set(CACHE_PREFIX + key, fileData, 2, TimeUnit.HOURS);
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@ import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.ensign.crm.module.crm.exception.AllKingdeeException;
|
||||
import com.ensign.crm.module.crm.model.ImageStorage;
|
||||
import com.ensign.crm.module.crm.model.FileStorage;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpEntity;
|
||||
@ -81,7 +81,7 @@ public class ProxyService {
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
|
||||
@Autowired
|
||||
private ImageService imageService;
|
||||
private KingdeeFileService kingdeeFileService;
|
||||
|
||||
|
||||
public void doProxy(HttpServletRequest request, HttpServletResponse response) throws IOException, URISyntaxException {
|
||||
@ -205,10 +205,8 @@ public class ProxyService {
|
||||
|
||||
log.info("receive json response: {}", jsonStr);
|
||||
JSONObject jsonObject = JSON.parseObject(jsonStr);
|
||||
if (jsonObject.containsKey("data")) {
|
||||
String realPath = jsonObject.getString("data");
|
||||
doDownloadImage(realPath);
|
||||
return realPath;
|
||||
if (jsonObject.containsKey("url")) {
|
||||
return jsonObject.getString("url");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -221,7 +219,7 @@ public class ProxyService {
|
||||
* @throws AllKingdeeException
|
||||
* @throws IOException
|
||||
*/
|
||||
private ImageStorage doDownloadImage(String realPath) throws AllKingdeeException, IOException {
|
||||
private FileStorage doDownloadImage(String realPath) throws AllKingdeeException, IOException {
|
||||
String url;
|
||||
if (realPath.startsWith("/")) {
|
||||
url = initBasePath() + "/ierp/attachment/downloadImage" + realPath;
|
||||
@ -230,16 +228,59 @@ public class ProxyService {
|
||||
}
|
||||
|
||||
String downloadUrl = url + "?v=1.0&access_token=" + initAccessToken();
|
||||
byte[] bytes = imageService.downloadImage(downloadUrl);
|
||||
ImageStorage imageStorage = new ImageStorage();
|
||||
byte[] bytes = kingdeeFileService.downloadFile(downloadUrl);
|
||||
FileStorage fileStorage = new FileStorage();
|
||||
String filename = getFileNameFromPath(realPath);
|
||||
imageStorage.setMediaType(getFileMediaType(filename));
|
||||
imageStorage.setFileName(filename);
|
||||
imageStorage.setFileSize((long) bytes.length);
|
||||
imageStorage.setFileContent(transformToBase64(bytes));
|
||||
fileStorage.setMediaType(getFileMediaType(filename));
|
||||
fileStorage.setFileName(filename);
|
||||
fileStorage.setFileSize((long) bytes.length);
|
||||
fileStorage.setFileContent(transformToBase64(bytes));
|
||||
|
||||
imageService.saveImage(realPath, imageStorage);
|
||||
return imageStorage;
|
||||
kingdeeFileService.saveImage(realPath, fileStorage);
|
||||
return fileStorage;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 从苍穹下载文件
|
||||
*
|
||||
* @param url 文件下载地址
|
||||
* @param fileName
|
||||
* @return
|
||||
* @throws AllKingdeeException
|
||||
* @throws IOException
|
||||
*/
|
||||
private FileStorage doDownloadFile(String url, String fileName) throws AllKingdeeException, IOException, URISyntaxException {
|
||||
|
||||
URI uri = new URI(url);
|
||||
|
||||
String downloadUrl = url + "&access_token=" + initAccessToken();
|
||||
byte[] bytes = kingdeeFileService.downloadFile(downloadUrl);
|
||||
FileStorage fileStorage = new FileStorage();
|
||||
String query = uri.getQuery();
|
||||
|
||||
String pathValue = getQueryParamValue(query, "path");
|
||||
fileStorage.setMediaType("application/octet-stream;charset=utf-8");
|
||||
fileStorage.setFileName(fileName);
|
||||
fileStorage.setFileSize((long) bytes.length);
|
||||
fileStorage.setFileContent(transformToBase64(bytes));
|
||||
|
||||
kingdeeFileService.saveFile(pathValue, fileStorage);
|
||||
return fileStorage;
|
||||
}
|
||||
|
||||
// 获取指定参数名的值
|
||||
private static String getQueryParamValue(String query, String param) {
|
||||
if (query != null) {
|
||||
String[] pairs = query.split("&");
|
||||
for (String pair : pairs) {
|
||||
String[] keyValue = pair.split("=");
|
||||
if (keyValue.length > 1 && keyValue[0].equals(param)) {
|
||||
return keyValue[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
return null; // 如果没有找到,返回 null
|
||||
}
|
||||
|
||||
/**
|
||||
@ -439,7 +480,7 @@ public class ProxyService {
|
||||
}
|
||||
|
||||
public void doGetImage(String path, HttpServletResponse response) throws AllKingdeeException, IOException {
|
||||
ImageStorage image = imageService.getImage(path);
|
||||
FileStorage image = kingdeeFileService.getImage(path);
|
||||
if (image == null) {
|
||||
image = doDownloadImage(path);
|
||||
}
|
||||
@ -458,4 +499,34 @@ public class ProxyService {
|
||||
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从苍穹下载文件
|
||||
*
|
||||
* @param url 文件下载地址
|
||||
* @param file_name
|
||||
* @param response 传输文件流
|
||||
* @throws AllKingdeeException
|
||||
* @throws IOException
|
||||
*/
|
||||
public void doGetFile(String url, String file_name, HttpServletResponse response) throws AllKingdeeException, IOException, URISyntaxException {
|
||||
FileStorage file = kingdeeFileService.getFile(url);
|
||||
if (file == null) {
|
||||
file = doDownloadFile(url, file_name);
|
||||
}
|
||||
byte[] bytes = Base64.decode(file.getFileContent());
|
||||
|
||||
try {
|
||||
response.setContentType(file.getMediaType());
|
||||
response.setContentLength(file.getFileSize().intValue());
|
||||
response.setHeader("Content-Disposition", "inline; filename=\"" + file.getFileName() + "\"");
|
||||
|
||||
ServletOutputStream outputStream = response.getOutputStream();
|
||||
outputStream.write(bytes);
|
||||
outputStream.flush();
|
||||
outputStream.close();
|
||||
} catch (IOException e) {
|
||||
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user