11 changed files with 2490 additions and 416 deletions
File diff suppressed because it is too large
@ -1,16 +0,0 @@ |
|||
package com.threecloud.dataserviceyy.config; |
|||
|
|||
import lombok.Data; |
|||
import org.springframework.boot.context.properties.ConfigurationProperties; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
@Data |
|||
@Component |
|||
@ConfigurationProperties(prefix = "file-upload") |
|||
public class FileUploadConfig { |
|||
private String uploadUrl; |
|||
private Map<String, String> extraParams = new HashMap<>(); |
|||
} |
|||
@ -0,0 +1,83 @@ |
|||
package com.threecloud.dataserviceyy.entity; |
|||
|
|||
/** |
|||
* OSS文件上传响应实体 |
|||
* 对应新OSS文件存储服务的上传接口返回格式 |
|||
* |
|||
* 响应示例: |
|||
* { |
|||
* "fileName": "demo.mp4", |
|||
* "fileUrl": "http://ip:8088/upload/public/common/abc123_20260629.mp4", |
|||
* "fileKey": "public/common/abc123_20260629.mp4", |
|||
* "fileSize": 1024000, |
|||
* "fileType": "mp4" |
|||
* } |
|||
*/ |
|||
public class OssFileResponse { |
|||
|
|||
/** 文件原始名称 */ |
|||
private String fileName; |
|||
|
|||
/** 文件访问地址,浏览器可直接打开播放 */ |
|||
private String fileUrl; |
|||
|
|||
/** 文件唯一标识,用于后续下载/预览/删除等操作 */ |
|||
private String fileKey; |
|||
|
|||
/** 文件大小(字节) */ |
|||
private Long fileSize; |
|||
|
|||
/** 文件扩展名 */ |
|||
private String fileType; |
|||
|
|||
public String getFileName() { |
|||
return fileName; |
|||
} |
|||
|
|||
public void setFileName(String fileName) { |
|||
this.fileName = fileName; |
|||
} |
|||
|
|||
public String getFileUrl() { |
|||
return fileUrl; |
|||
} |
|||
|
|||
public void setFileUrl(String fileUrl) { |
|||
this.fileUrl = fileUrl; |
|||
} |
|||
|
|||
public String getFileKey() { |
|||
return fileKey; |
|||
} |
|||
|
|||
public void setFileKey(String fileKey) { |
|||
this.fileKey = fileKey; |
|||
} |
|||
|
|||
public Long getFileSize() { |
|||
return fileSize; |
|||
} |
|||
|
|||
public void setFileSize(Long fileSize) { |
|||
this.fileSize = fileSize; |
|||
} |
|||
|
|||
public String getFileType() { |
|||
return fileType; |
|||
} |
|||
|
|||
public void setFileType(String fileType) { |
|||
this.fileType = fileType; |
|||
} |
|||
|
|||
@Override |
|||
public String toString() { |
|||
return "OssFileResponse{" + |
|||
"fileName='" + fileName + '\'' + |
|||
", fileUrl='" + fileUrl + '\'' + |
|||
", fileKey='" + fileKey + '\'' + |
|||
", fileSize=" + fileSize + |
|||
", fileType='" + fileType + '\'' + |
|||
'}'; |
|||
} |
|||
} |
|||
@ -1,290 +0,0 @@ |
|||
package com.threecloud.dataserviceyy.util; |
|||
|
|||
import com.threecloud.dataserviceyy.entity.ResultEntity; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.core.io.ByteArrayResource; |
|||
import org.springframework.http.*; |
|||
import org.springframework.http.client.SimpleClientHttpRequestFactory; |
|||
import org.springframework.stereotype.Component; |
|||
import org.springframework.util.LinkedMultiValueMap; |
|||
import org.springframework.util.MultiValueMap; |
|||
import org.springframework.web.client.RestTemplate; |
|||
|
|||
import javax.annotation.PostConstruct; |
|||
import javax.net.ssl.HttpsURLConnection; |
|||
import java.io.IOException; |
|||
import java.net.HttpURLConnection; |
|||
|
|||
/** |
|||
* 文件上传工具类 |
|||
* 负责上传录音文件到 OSS,并返回完整的访问 URL |
|||
* |
|||
* 【配置说明】 |
|||
* vaa-sync.oss.base-url: OSS服务基础地址,用于拼接完整URL |
|||
* vaa-sync.oss.upload-url: OSS上传接口地址 |
|||
* vaa-sync.oss.appcode/appid/appsecret: OSS认证信息 |
|||
*/ |
|||
@Component |
|||
public class FileUploadUtil { |
|||
|
|||
private static final Logger logger = LoggerFactory.getLogger(FileUploadUtil.class); |
|||
|
|||
// OSS配置(从配置文件读取)
|
|||
@Value("${vaa-sync.oss.base-url:http://53.1.194.59:9090}") |
|||
private String ossBaseUrl; |
|||
|
|||
@Value("${vaa-sync.oss.upload-url:http://53.1.194.59:9090/apiOss/oss/fileUpload}") |
|||
private String ossUploadUrl; |
|||
|
|||
@Value("${vaa-sync.oss.appcode:dataservice-yy}") |
|||
private String ossAppcode; |
|||
|
|||
@Value("${vaa-sync.oss.appid:371a3368-e28e-4ba3-95a3-c31c19cf0ad0}") |
|||
private String ossAppid; |
|||
|
|||
@Value("${vaa-sync.oss.appsecret:06a6a80e-f9d2-4b3b-acc0-8d182c876074}") |
|||
private String ossAppsecret; |
|||
|
|||
private RestTemplate restTemplate; |
|||
|
|||
@PostConstruct |
|||
public void init() { |
|||
restTemplate = createRestTemplate(); |
|||
logger.info("文件上传工具初始化完成"); |
|||
logger.info(" OSS基础地址: {}", ossBaseUrl); |
|||
logger.info(" 上传接口: {}", ossUploadUrl); |
|||
} |
|||
|
|||
/** |
|||
* 上传WAV录音文件到OSS |
|||
* |
|||
* 存储路径格式: voice/{cityName}/{cityCode}/{date}/{filename} |
|||
* 示例: voice/合肥/340100/20240101/IN-xxx.wav |
|||
* |
|||
* 返回完整URL格式: {ossBaseUrl}/voice/{cityName}/{cityCode}/{date}/{filename} |
|||
* 示例: http://53.1.194.59:9090/voice/合肥/340100/20240101/IN-xxx.wav
|
|||
* |
|||
* @param cityName 地市名称(如 合肥) |
|||
* @param ossPath OSS存储路径(格式: cityCode/date/filename) |
|||
* @param wavFileName WAV文件名 |
|||
* @param wavData 文件字节数组 |
|||
* @return 完整的文件访问URL |
|||
*/ |
|||
public String uploadWav(String cityName, String ossPath, String wavFileName, byte[] wavData) { |
|||
// 构建OSS对象名: voice/{cityName}/{cityCode}/{date}/{filename}
|
|||
String objectName = "voice/" + cityName + "/" + ossPath; |
|||
logger.debug("构建OSS对象名: {}", objectName); |
|||
|
|||
// 上传文件
|
|||
String relativeUrl = uploadFile(objectName, wavData); |
|||
|
|||
// 返回完整URL
|
|||
String fullUrl = buildFullUrl(relativeUrl); |
|||
logger.info("文件上传完成,完整URL: {}", fullUrl); |
|||
|
|||
return fullUrl; |
|||
} |
|||
|
|||
/** |
|||
* 构建完整的文件访问URL |
|||
* |
|||
* 如果返回的是相对路径,拼接 base-url |
|||
* 如果返回的已经是完整URL,直接返回 |
|||
* |
|||
* @param url 从OSS返回的URL(可能是相对路径或完整URL) |
|||
* @return 完整的访问URL |
|||
*/ |
|||
private String buildFullUrl(String url) { |
|||
if (url == null || url.isEmpty()) { |
|||
return ""; |
|||
} |
|||
|
|||
// 如果已经是完整URL,直接返回
|
|||
if (url.startsWith("http://") || url.startsWith("https://")) { |
|||
return url; |
|||
} |
|||
|
|||
// 拼接基础URL
|
|||
String baseUrl = ossBaseUrl.endsWith("/") ? ossBaseUrl.substring(0, ossBaseUrl.length() - 1) : ossBaseUrl; |
|||
String relativePath = url.startsWith("/") ? url : "/" + url; |
|||
|
|||
return baseUrl + relativePath; |
|||
} |
|||
|
|||
/** |
|||
* 上传文件到OSS |
|||
* |
|||
* @param fileName 文件在OSS中的路径(如 voice/合肥/340100/...) |
|||
* @param fileData 文件字节数组 |
|||
* @return OSS返回的文件路径或URL |
|||
*/ |
|||
private String uploadFile(String fileName, byte[] fileData) { |
|||
try { |
|||
HttpHeaders headers = new HttpHeaders(); |
|||
headers.setContentType(MediaType.MULTIPART_FORM_DATA); |
|||
|
|||
ByteArrayResource byteResource = new ByteArrayResource(fileData) { |
|||
@Override |
|||
public String getFilename() { |
|||
return fileName; |
|||
} |
|||
}; |
|||
|
|||
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); |
|||
body.add("files", byteResource); |
|||
body.set("appcode", ossAppcode); |
|||
body.set("appid", ossAppid); |
|||
body.set("appsecret", ossAppsecret); |
|||
|
|||
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers); |
|||
|
|||
logger.debug("开始上传文件到OSS: {}, 大小: {} bytes", fileName, fileData.length); |
|||
ResponseEntity<ResultEntity> response = restTemplate.exchange( |
|||
ossUploadUrl, |
|||
HttpMethod.POST, |
|||
requestEntity, |
|||
ResultEntity.class |
|||
); |
|||
|
|||
if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) { |
|||
ResultEntity result = response.getBody(); |
|||
if (result.getCode() == ResultEntity.StatusCode.SUCCESS.getCode()) { |
|||
String url = parseUrlFromContent(result.getContent()); |
|||
logger.debug("OSS返回URL: {}", url); |
|||
return url; |
|||
} else { |
|||
throw new RuntimeException("上传失败, code=" + result.getCode() + ", msg=" + result.getMsg()); |
|||
} |
|||
} else { |
|||
throw new RuntimeException("上传失败, HTTP状态码: " + response.getStatusCode()); |
|||
} |
|||
} catch (Exception e) { |
|||
logger.error("文件上传异常: {}", fileName, e); |
|||
throw new RuntimeException("文件上传失败: " + e.getMessage(), e); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 从 ResultEntity.content 中解析文件URL |
|||
* |
|||
* 支持多种返回格式: |
|||
* 1. Map 对象: {"url": "http://xxx", "fileName": "xxx.wav"} |
|||
* 2. JSON 字符串: "{\"url\":\"http://xxx\"}" |
|||
* 3. 纯 URL 字符串: "http://xxx/xxx.wav" |
|||
* 4. 数组格式: [{"url": "http://xxx"}] |
|||
* 5. 相对路径: "voice/xxx/xxx.wav" |
|||
* |
|||
* @param content OSS 返回的内容 |
|||
* @return 解析后的文件路径或URL |
|||
*/ |
|||
private String parseUrlFromContent(Object content) { |
|||
if (content == null) { |
|||
logger.warn("OSS 返回 content 为空"); |
|||
return ""; |
|||
} |
|||
|
|||
// 情况1:Map 对象
|
|||
if (content instanceof java.util.Map) { |
|||
java.util.Map<?, ?> map = (java.util.Map<?, ?>) content; |
|||
Object url = map.get("url"); |
|||
if (url != null) { |
|||
logger.debug("从 Map 解析到 URL: {}", url); |
|||
return url.toString(); |
|||
} |
|||
// 尝试其他可能的 key
|
|||
Object fileUrl = map.get("fileUrl"); |
|||
if (fileUrl != null) { |
|||
logger.debug("从 Map 解析到 fileUrl: {}", fileUrl); |
|||
return fileUrl.toString(); |
|||
} |
|||
Object path = map.get("path"); |
|||
if (path != null) { |
|||
logger.debug("从 Map 解析到 path: {}", path); |
|||
return path.toString(); |
|||
} |
|||
} |
|||
|
|||
// 情况2:List/Array 对象(批量上传返回)
|
|||
if (content instanceof java.util.List) { |
|||
java.util.List<?> list = (java.util.List<?>) content; |
|||
if (!list.isEmpty()) { |
|||
Object first = list.get(0); |
|||
if (first instanceof java.util.Map) { |
|||
Object url = ((java.util.Map<?, ?>) first).get("url"); |
|||
if (url != null) { |
|||
logger.debug("从 List 第一个元素解析到 URL: {}", url); |
|||
return url.toString(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
// 情况3:JSON 字符串
|
|||
String contentStr = content.toString().trim(); |
|||
if (contentStr.startsWith("{") || contentStr.startsWith("[")) { |
|||
try { |
|||
// 尝试解析为 JSON
|
|||
Object parsed = com.alibaba.fastjson2.JSON.parse(contentStr); |
|||
if (parsed instanceof java.util.Map) { |
|||
return parseUrlFromContent(parsed); // 递归解析
|
|||
} |
|||
if (parsed instanceof java.util.List) { |
|||
return parseUrlFromContent(parsed); // 递归解析
|
|||
} |
|||
} catch (Exception e) { |
|||
logger.debug("解析 JSON 失败: {}", e.getMessage()); |
|||
} |
|||
} |
|||
|
|||
// 情况4:直接是 URL 字符串(以 http:// 或 https:// 开头)
|
|||
if (contentStr.startsWith("http://") || contentStr.startsWith("https://")) { |
|||
logger.debug("直接返回 URL 字符串: {}", contentStr); |
|||
return contentStr; |
|||
} |
|||
|
|||
// 情况5:相对路径(以 voice/ 开头)
|
|||
if (contentStr.startsWith("voice/")) { |
|||
logger.debug("返回相对路径: {}", contentStr); |
|||
return contentStr; |
|||
} |
|||
|
|||
// 情况6:尝试从字符串中提取 URL
|
|||
if (contentStr.contains("\"url\"")) { |
|||
try { |
|||
int start = contentStr.indexOf("\"url\":\"") + 7; |
|||
int end = contentStr.indexOf("\"", start); |
|||
if (start > 6 && end > start) { |
|||
String url = contentStr.substring(start, end); |
|||
logger.debug("从字符串提取 URL: {}", url); |
|||
return url; |
|||
} |
|||
} catch (Exception e) { |
|||
logger.warn("从字符串提取 URL 失败: {}", e.getMessage()); |
|||
} |
|||
} |
|||
|
|||
// 兜底:直接返回字符串
|
|||
logger.warn("无法解析 URL,直接返回 content: {}", contentStr); |
|||
return contentStr; |
|||
} |
|||
|
|||
/** |
|||
* 创建支持自定义SSL配置的RestTemplate |
|||
* 针对HTTPS请求,配置信任所有证书并关闭Hostname验证,以兼容自签名证书 |
|||
*/ |
|||
private RestTemplate createRestTemplate() { |
|||
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory() { |
|||
@Override |
|||
protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException { |
|||
if (ossUploadUrl.startsWith("https") && connection instanceof HttpsURLConnection) { |
|||
// HTTPS 特殊配置(如需要)
|
|||
} |
|||
super.prepareConnection(connection, httpMethod); |
|||
} |
|||
}; |
|||
requestFactory.setBufferRequestBody(false); |
|||
return new RestTemplate(requestFactory); |
|||
} |
|||
} |
|||
@ -0,0 +1,164 @@ |
|||
package com.threecloud.dataserviceyy.util; |
|||
|
|||
import com.threecloud.dataserviceyy.entity.OssFileResponse; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.core.io.ByteArrayResource; |
|||
import org.springframework.http.*; |
|||
import org.springframework.http.client.SimpleClientHttpRequestFactory; |
|||
import org.springframework.stereotype.Component; |
|||
import org.springframework.util.LinkedMultiValueMap; |
|||
import org.springframework.util.MultiValueMap; |
|||
import org.springframework.web.client.RestTemplate; |
|||
|
|||
import javax.annotation.PostConstruct; |
|||
import javax.net.ssl.HttpsURLConnection; |
|||
import java.io.IOException; |
|||
import java.net.HttpURLConnection; |
|||
|
|||
/** |
|||
* OSS文件存储服务客户端 |
|||
* |
|||
* 基于新OSS文件存储服务API对接,提供文件上传、预览等功能。 |
|||
* API文档地址: file:sanduoyun/developspace/dataservice-yy/OSS文件存储服务-技术对接文档.md |
|||
* |
|||
* 配置项: |
|||
* oss.base-url: OSS服务基础地址(如 http://53.1.211.7)
|
|||
* oss.upload-path: 上传存储路径(如 voice/record/) |
|||
*/ |
|||
@Component |
|||
public class OssFileService { |
|||
|
|||
private static final Logger logger = LoggerFactory.getLogger(OssFileService.class); |
|||
|
|||
@Value("${oss.base-url:http://53.1.211.7}") |
|||
private String ossBaseUrl; |
|||
|
|||
@Value("${oss.upload-path:voice/record/}") |
|||
private String defaultUploadPath; |
|||
|
|||
private RestTemplate restTemplate; |
|||
|
|||
@PostConstruct |
|||
public void init() { |
|||
restTemplate = createRestTemplate(); |
|||
logger.info("OSS文件服务初始化完成, 基础地址: {}", ossBaseUrl); |
|||
logger.info("OSS上传默认路径: {}", defaultUploadPath); |
|||
} |
|||
|
|||
/** |
|||
* 上传文件到OSS |
|||
* |
|||
* 调用 POST /oss/upload 接口。 |
|||
* 存储路径格式: {uploadPath}/{fileName} |
|||
* 示例: voice/record/xxx.wav |
|||
* |
|||
* @param fileData 文件字节数组 |
|||
* @param fileName 文件名(如 xxx.wav) |
|||
* @return OssFileResponse 包含 fileUrl(预览地址)和 fileKey(文件标识) |
|||
*/ |
|||
public OssFileResponse uploadFile(byte[] fileData, String fileName) { |
|||
return uploadFile(fileData, fileName, defaultUploadPath); |
|||
} |
|||
|
|||
/** |
|||
* 上传文件到OSS(指定存储路径) |
|||
* |
|||
* @param fileData 文件字节数组 |
|||
* @param fileName 文件名 |
|||
* @param uploadPath 存储路径(如 voice/record/) |
|||
* @return OssFileResponse |
|||
*/ |
|||
public OssFileResponse uploadFile(byte[] fileData, String fileName, String uploadPath) { |
|||
String url = ossBaseUrl + "/oss/upload"; |
|||
String path = uploadPath.endsWith("/") ? uploadPath : uploadPath + "/"; |
|||
|
|||
try { |
|||
HttpHeaders headers = new HttpHeaders(); |
|||
headers.setContentType(MediaType.MULTIPART_FORM_DATA); |
|||
|
|||
ByteArrayResource resource = new ByteArrayResource(fileData) { |
|||
@Override |
|||
public String getFilename() { |
|||
return fileName; |
|||
} |
|||
}; |
|||
|
|||
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); |
|||
body.add("file", resource); |
|||
body.add("path", path); |
|||
|
|||
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers); |
|||
|
|||
logger.debug("上传文件到OSS: url={}, path={}, fileName={}, size={} bytes", url, path, fileName, fileData.length); |
|||
ResponseEntity<OssFileResponse> response = restTemplate.exchange( |
|||
url, |
|||
HttpMethod.POST, |
|||
requestEntity, |
|||
OssFileResponse.class |
|||
); |
|||
|
|||
if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) { |
|||
OssFileResponse result = response.getBody(); |
|||
// fileUrl可能为相对路径(如 /upload/...),补全为完整URL
|
|||
if (result.getFileUrl() != null && result.getFileUrl().startsWith("/")) { |
|||
String base = ossBaseUrl.endsWith("/") ? ossBaseUrl.substring(0, ossBaseUrl.length() - 1) : ossBaseUrl; |
|||
result.setFileUrl(base + result.getFileUrl()); |
|||
} |
|||
logger.info("文件上传成功: fileKey={}, fileUrl={}", result.getFileKey(), result.getFileUrl()); |
|||
return result; |
|||
} else { |
|||
throw new RuntimeException("上传失败, HTTP状态码: " + response.getStatusCode()); |
|||
} |
|||
} catch (Exception e) { |
|||
logger.error("文件上传异常: fileName={}", fileName, e); |
|||
throw new RuntimeException("文件上传失败: " + e.getMessage(), e); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 获取文件预览URL |
|||
* |
|||
* 格式: {ossBaseUrl}/oss/preview?fileKey={fileKey} |
|||
* 浏览器可直接打开预览 |
|||
* |
|||
* @param fileKey 文件唯一标识(如 voice/record/xxx.wav) |
|||
* @return 预览URL |
|||
*/ |
|||
public String getPreviewUrl(String fileKey) { |
|||
return ossBaseUrl + "/oss/preview?fileKey=" + fileKey; |
|||
} |
|||
|
|||
/** |
|||
* 创建支持自定义SSL配置的RestTemplate |
|||
*/ |
|||
private RestTemplate createRestTemplate() { |
|||
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory() { |
|||
@Override |
|||
protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException { |
|||
if (ossBaseUrl.startsWith("https") && connection instanceof HttpsURLConnection) { |
|||
HttpsURLConnection httpsConn = (HttpsURLConnection) connection; |
|||
try { |
|||
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[]{ |
|||
new javax.net.ssl.X509TrustManager() { |
|||
public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } |
|||
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {} |
|||
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {} |
|||
} |
|||
}; |
|||
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("TLS"); |
|||
sc.init(null, trustAllCerts, new java.security.SecureRandom()); |
|||
httpsConn.setSSLSocketFactory(sc.getSocketFactory()); |
|||
httpsConn.setHostnameVerifier((hostname, session) -> true); |
|||
} catch (Exception e) { |
|||
logger.warn("SSL配置失败: {}", e.getMessage()); |
|||
} |
|||
} |
|||
super.prepareConnection(connection, httpMethod); |
|||
} |
|||
}; |
|||
requestFactory.setBufferRequestBody(false); |
|||
return new RestTemplate(requestFactory); |
|||
} |
|||
} |
|||
@ -1,72 +0,0 @@ |
|||
package com.threecloud.dataserviceyy.util; |
|||
|
|||
import com.threecloud.dataserviceyy.entity.ResultEntity; |
|||
import org.springframework.core.io.ByteArrayResource; |
|||
import org.springframework.http.*; |
|||
import org.springframework.http.client.SimpleClientHttpRequestFactory; |
|||
import org.springframework.util.LinkedMultiValueMap; |
|||
import org.springframework.util.MultiValueMap; |
|||
import org.springframework.web.client.RestTemplate; |
|||
import org.springframework.web.multipart.MultipartFile; |
|||
|
|||
import javax.net.ssl.HttpsURLConnection; |
|||
import java.io.IOException; |
|||
import java.net.HttpURLConnection; |
|||
|
|||
public class fileUtil { |
|||
|
|||
/** |
|||
* 通过oss上传文件 |
|||
* @param mf |
|||
* @return ResultEntity |
|||
* @throws IOException |
|||
*/ |
|||
private ResultEntity uploadFilesByOss(MultipartFile mf) throws IOException{ |
|||
String ossUrl = "http://127.0.0.1/apiOss";//baseOssConfigService.readOSsUrl();// OSS管理服务地址
|
|||
String ossPreviewUrl = "http://127.0.0.1/apiOssPreview/onlinePreview";//baseOssConfigService.readOSsPreviewUrl();// OSS预览服务地址
|
|||
String ossAppid = "371a3368-e28e-4ba3-95a3-c31c19cf0ad0";//baseOssConfigService.readOSsAppid();// OSS应用ID
|
|||
String ossAppsecret = "06a6a80e-f9d2-4b3b-acc0-8d182c876074";//baseOssConfigService.readOSsAppsecret();// OSS应用密钥
|
|||
String ossAppcode = "dataservice-yy";//baseOssConfigService.readOSsAppcode();// OSS应用别名
|
|||
ResponseEntity<ResultEntity> responseEntity = null; |
|||
RestTemplate restTemplate = createRestTemplate(ossUrl); |
|||
HttpHeaders headers = new HttpHeaders(); |
|||
headers.setContentType(MediaType.MULTIPART_FORM_DATA); |
|||
MultiValueMap<String, Object> files = new LinkedMultiValueMap<String, Object>(); |
|||
//for (MultipartFile file : multipartFiles) {
|
|||
ByteArrayResource byteResource = new ByteArrayResource(mf.getBytes()){ |
|||
@Override |
|||
public String getFilename() { |
|||
return mf.getOriginalFilename(); |
|||
} |
|||
}; |
|||
files.add("files", byteResource);//上传文件
|
|||
//}
|
|||
files.add("appcode", ossAppcode);//项目编号
|
|||
files.set("appcode", ossAppcode); |
|||
files.set("appid", ossAppid); |
|||
files.set("appsecret", ossAppsecret); |
|||
HttpEntity<MultiValueMap<String, Object>> httpEntity = |
|||
new HttpEntity<MultiValueMap<String, Object>>(files,headers); |
|||
responseEntity = restTemplate.exchange(ossUrl + "/oss/fileUpload", HttpMethod.POST, httpEntity, ResultEntity.class); |
|||
|
|||
return responseEntity.getBody(); |
|||
} |
|||
|
|||
/** |
|||
* 创建支持自定义SSL配置的RestTemplate |
|||
* 针对HTTPS请求,配置信任所有证书并关闭Hostname验证,以兼容自签名证书 |
|||
*/ |
|||
private RestTemplate createRestTemplate(String ossUrl) { |
|||
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory() { |
|||
@Override |
|||
protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException { |
|||
if (ossUrl != null && ossUrl.startsWith("https") && connection instanceof HttpsURLConnection) { |
|||
|
|||
} |
|||
super.prepareConnection(connection, httpMethod); |
|||
} |
|||
}; |
|||
requestFactory.setBufferRequestBody(false); |
|||
return new RestTemplate(requestFactory); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue