|
|
@ -33,7 +33,7 @@ public class FileUploadUtil { |
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(FileUploadUtil.class); |
|
|
private static final Logger logger = LoggerFactory.getLogger(FileUploadUtil.class); |
|
|
|
|
|
|
|
|
@Value("${ftp-sync.oss.base-url:http://53.1.211.7/apiOss}") |
|
|
@Value("${ftp-sync.oss.base-url:http://53.1.211.7:8090}") |
|
|
private String ossBaseUrl; |
|
|
private String ossBaseUrl; |
|
|
|
|
|
|
|
|
private RestTemplate restTemplate; |
|
|
private RestTemplate restTemplate; |
|
|
@ -57,6 +57,12 @@ public class FileUploadUtil { |
|
|
*/ |
|
|
*/ |
|
|
public String uploadWav(String ossPath, String fileName, byte[] fileData) { |
|
|
public String uploadWav(String ossPath, String fileName, byte[] fileData) { |
|
|
String path = "voice/" + ossPath; |
|
|
String path = "voice/" + ossPath; |
|
|
|
|
|
if (fileName != null && !fileName.isEmpty() && path.endsWith(fileName)) { |
|
|
|
|
|
path = path.substring(0, path.length() - fileName.length()); |
|
|
|
|
|
} |
|
|
|
|
|
if (path.endsWith("/")) { |
|
|
|
|
|
path = path.substring(0, path.length() - 1); |
|
|
|
|
|
} |
|
|
logger.debug("上传文件到OSS, path={}, 大小={} bytes", path, fileData.length); |
|
|
logger.debug("上传文件到OSS, path={}, 大小={} bytes", path, fileData.length); |
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
@ -76,21 +82,39 @@ public class FileUploadUtil { |
|
|
|
|
|
|
|
|
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers); |
|
|
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers); |
|
|
|
|
|
|
|
|
ResponseEntity<ResultEntity> response = restTemplate.exchange( |
|
|
ResponseEntity<String> response = restTemplate.exchange( |
|
|
ossBaseUrl + "/oss/upload", |
|
|
ossBaseUrl + "/oss/upload", |
|
|
HttpMethod.POST, |
|
|
HttpMethod.POST, |
|
|
requestEntity, |
|
|
requestEntity, |
|
|
ResultEntity.class |
|
|
String.class |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) { |
|
|
if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) { |
|
|
ResultEntity result = response.getBody(); |
|
|
String responseBody = response.getBody(); |
|
|
if (result.getCode() == ResultEntity.StatusCode.SUCCESS.getCode()) { |
|
|
logger.info("文件上传响应原始数据: {}", responseBody); |
|
|
|
|
|
|
|
|
|
|
|
// 优先检查是否直接返回了包含 fileUrl 或 url 的对象(兼容扁平格式)
|
|
|
|
|
|
if (responseBody.trim().startsWith("{")) { |
|
|
|
|
|
try { |
|
|
|
|
|
Map<?, ?> map = JSON.parseObject(responseBody, Map.class); |
|
|
|
|
|
if (map != null && (map.containsKey("fileUrl") || map.containsKey("url"))) { |
|
|
|
|
|
String fileUrl = parseFileUrl(responseBody); |
|
|
|
|
|
logger.info("文件上传完成 (扁平格式), URL: {}", fileUrl); |
|
|
|
|
|
return fileUrl; |
|
|
|
|
|
} |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
logger.debug("尝试直接解析为Map失败: {}", e.getMessage()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 否则按照 ResultEntity 包装格式解析(兼容旧包装格式)
|
|
|
|
|
|
ResultEntity result = JSON.parseObject(responseBody, ResultEntity.class); |
|
|
|
|
|
if (result != null && result.getCode() != null && result.getCode() == ResultEntity.StatusCode.SUCCESS.getCode()) { |
|
|
String fileUrl = parseFileUrl(result.getContent()); |
|
|
String fileUrl = parseFileUrl(result.getContent()); |
|
|
logger.info("文件上传完成, URL: {}", fileUrl); |
|
|
logger.info("文件上传完成 (包装格式), URL: {}", fileUrl); |
|
|
return fileUrl; |
|
|
return fileUrl; |
|
|
} else { |
|
|
} else { |
|
|
throw new RuntimeException("上传失败, code=" + result.getCode() + ", msg=" + result.getMsg()); |
|
|
throw new RuntimeException("上传失败, 原始响应=" + responseBody + ", code=" + (result != null ? result.getCode() : "null") + ", msg=" + (result != null ? result.getMsg() : "null")); |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
throw new RuntimeException("上传失败, HTTP状态码: " + response.getStatusCode()); |
|
|
throw new RuntimeException("上传失败, HTTP状态码: " + response.getStatusCode()); |
|
|
|