修正头像1144555
This commit is contained in:
parent
7997d0abe9
commit
dfab1ec324
@ -15,6 +15,7 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
@ -39,18 +40,75 @@ public class Oss {
|
||||
}
|
||||
|
||||
public static boolean putObject(String fileName, byte[] content) {
|
||||
if (content == null || content.length == 0) {
|
||||
logger.warn("Failed to upload object to OSS: content is null or empty, fileName={}", fileName);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (fileName == null || fileName.trim().isEmpty()) {
|
||||
logger.warn("Failed to upload object to OSS: fileName is null or empty");
|
||||
return false;
|
||||
}
|
||||
|
||||
OSS client = createClient();
|
||||
try (InputStream input = new ByteArrayInputStream(content)) {
|
||||
client.putObject(ossConfig.getBucket(), fileName, input);
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(content);
|
||||
|
||||
try {
|
||||
// 推断 Content-Type
|
||||
String contentType = determineContentType(fileName);
|
||||
|
||||
// 创建对象元数据
|
||||
ObjectMetadata metadata = new ObjectMetadata();
|
||||
metadata.setContentLength(content.length);
|
||||
metadata.setContentType(contentType);
|
||||
|
||||
// 执行上传
|
||||
PutObjectRequest request = new PutObjectRequest(ossConfig.getBucket(), fileName, inputStream, metadata);
|
||||
client.putObject(request);
|
||||
|
||||
logger.info("Successfully uploaded object to OSS: bucket={}, fileName={}, size={} bytes",
|
||||
ossConfig.getBucket(), fileName, content.length);
|
||||
return true;
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.info(e.getMessage());
|
||||
logger.error("Failed to upload object to OSS. fileName={}, error={}", fileName, e.getMessage(), e);
|
||||
return false;
|
||||
} finally {
|
||||
try {
|
||||
inputStream.close(); // 可以不关(ByteArrayInputStream 空操作),但建议写上
|
||||
} catch (IOException ignored) { }
|
||||
client.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
private static String determineContentType(String fileName) {
|
||||
if (fileName == null) return "application/octet-stream";
|
||||
|
||||
int lastDotIndex = fileName.lastIndexOf('.');
|
||||
if (lastDotIndex < 0) {
|
||||
return "application/octet-stream";
|
||||
}
|
||||
|
||||
String ext = fileName.substring(lastDotIndex + 1).toLowerCase();
|
||||
|
||||
return switch (ext) {
|
||||
case "jpg", "jpeg" -> "image/jpeg";
|
||||
case "png" -> "image/png";
|
||||
case "gif" -> "image/gif";
|
||||
case "bmp" -> "image/bmp";
|
||||
case "webp" -> "image/webp";
|
||||
case "pdf" -> "application/pdf";
|
||||
case "txt" -> "text/plain";
|
||||
case "html", "htm" -> "text/html";
|
||||
case "xml" -> "application/xml";
|
||||
case "json" -> "application/json";
|
||||
case "mp4" -> "video/mp4";
|
||||
case "avi" -> "video/x-msvideo";
|
||||
case "mp3" -> "audio/mpeg";
|
||||
default -> "application/octet-stream";
|
||||
};
|
||||
}
|
||||
|
||||
public static String getObjectToString(String fileName) {
|
||||
OSS client = createClient();
|
||||
try {
|
||||
|
||||
@ -390,10 +390,10 @@ public class UserService {
|
||||
String dateTimeStr = LocalDateTime.now()
|
||||
.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
|
||||
int randomSuffix = new Random().nextInt(9000) + 1000;
|
||||
String ossPath = "user/avatar/" + dateTimeStr + randomSuffix + ".png";
|
||||
String ossPath = "dev/static/images/" + dateTimeStr + randomSuffix + ".png";
|
||||
|
||||
// 3. 上传 OSS
|
||||
boolean success = Oss.putObject("/" + ossPath, imageBytes);
|
||||
boolean success = Oss.putObject(ossPath, imageBytes);
|
||||
if (!success) {
|
||||
throw new BusinessException("图片处理失败");
|
||||
}
|
||||
@ -402,7 +402,9 @@ public class UserService {
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(e.getMessage());
|
||||
// 头像处理失败
|
||||
System.out.println(e.getMessage());
|
||||
return "";
|
||||
} finally {
|
||||
if (conn != null) {
|
||||
conn.disconnect();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user