import com.guahao.convention.exception.ServiceException;
import com.guahao.csb.biz.core.utils.Assert;
import com.guahao.csb.biz.manager.config.KanoProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author
* @version V1.0
* @since 2021/9/2 18:01
*/
@Slf4j
@Service
public class KanoFileHelper {
@Resource
private RestTemplate restTemplate;
@Autowired(required = false)
private KanoProperties kanoProperties;
public String uploadImplementationFile(MultipartFile file) throws IOException {
// http header
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("orginFileName", file.getOriginalFilename());
params.add("bizCode", KanoProperties.BIZ_IMPLEMENTATION);
String signStr = params.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.map(e -> e.getKey() + "=" + e.getValue().iterator().next())
.collect(Collectors.joining("&")) + kanoProperties.getSignKey();
String sign = DigestUtils.md5Hex(signStr.getBytes(StandardCharsets.UTF_8));
params.add("sign", sign);
// 构建文件的 multi request
HttpHeaders bodyHeader = new HttpHeaders();
MediaType mediaType = MediaType.parseMediaType(file.getContentType());
bodyHeader.setContentType(mediaType);
bodyHeader.setContentDispositionFormData("file", file.getOriginalFilename());
HttpEntity<byte[]> bodyEntity = new HttpEntity<>(file.getBytes(), bodyHeader);
// 文件数据
params.add("file", bodyEntity);
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(params, headers);
Map<String, Object> res = restTemplate.postForObject(kanoProperties.getUploadUrl(), httpEntity, Map.class);
log.info("res {}", res);
Integer code = (Integer) res.get("code");
String message = (String) res.get("message");
Assert.isTrue(Integer.valueOf(0).equals(code), StringUtils.isBlank(message) ? "文件上传失败!" : message);
//noinspection unchecked
Map<String, String> kanoResult = (Map<String, String>) res.get("data");
return kanoProperties.getViewUrl() + "/" + kanoResult.get("kanoFileId")
+ "?token=" + kanoResult.get("token")
+ "&v=1.0&fileName=" + file.getOriginalFilename();
}
/**
* 从kano文件转成base64编码字符串
* @param kanoUrl
* @return
*/
public String getBase64FromUrl(String kanoUrl) {
byte[] file = downloadFile(kanoUrl);
return Base64.getEncoder().encodeToString(file);
}
public byte[] downloadFile(String kanoUrl) {
Assert.hasText(kanoUrl, "文件url 不能为空");
try {
ResponseEntity<byte[]> responseEntity = restTemplate.getForEntity(kanoUrl, byte[].class);
return responseEntity.getBody();
} catch (Exception ex) {
throw new ServiceException("", "获取kano文件异常");
}
}
}
|