模型上传接口实现
This commit is contained in:
parent
3ddd251dc5
commit
a7272298dc
|
|
@ -6,7 +6,10 @@ import com.ruoyi.platform.domain.Dataset;
|
|||
import com.ruoyi.platform.domain.Models;
|
||||
import com.ruoyi.platform.service.ModelsService;
|
||||
import com.ruoyi.platform.mapper.ModelsDao;
|
||||
import com.ruoyi.platform.utils.MinioUtil;
|
||||
import io.minio.MinioClient;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
|
@ -16,9 +19,11 @@ import org.springframework.stereotype.Service;
|
|||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.Date;
|
||||
|
|
@ -34,6 +39,26 @@ public class ModelsServiceImpl implements ModelsService {
|
|||
@Resource
|
||||
private ModelsDao modelsDao;
|
||||
|
||||
private MinioClient minioClient;
|
||||
|
||||
@Value("${minio.endpoint}")
|
||||
private String minioEndpoint;
|
||||
|
||||
@Value("${minio.accessKey}")
|
||||
private String minioAccessKey;
|
||||
|
||||
@Value("${minio.secretKey}")
|
||||
private String minioSecretKey;
|
||||
|
||||
// 固定存储桶名
|
||||
private final String bucketName = "platform-data";
|
||||
|
||||
private final MinioUtil minioUtil;
|
||||
|
||||
public ModelsServiceImpl(MinioUtil minioUtil) {
|
||||
this.minioUtil = minioUtil;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过ID查询单条数据
|
||||
*
|
||||
|
|
@ -160,6 +185,28 @@ public class ModelsServiceImpl implements ModelsService {
|
|||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadModels(MultipartFile file) throws Exception{
|
||||
if(file.isEmpty()){
|
||||
throw new Exception("文件为空,无法上传");
|
||||
}
|
||||
|
||||
//得到用户名拼接objectName
|
||||
LoginUser loginUser = SecurityUtils.getLoginUser();
|
||||
String username = loginUser.getUser().getUserName();
|
||||
|
||||
String objectName = "models/" + username + "/" + file.getOriginalFilename();
|
||||
|
||||
// 上传文件到MinIO
|
||||
try (InputStream inputStream = file.getInputStream()) {
|
||||
minioUtil.uploadObject(bucketName, objectName, inputStream);
|
||||
return "文件成功上传到: " + objectName;
|
||||
} catch (Exception e) {
|
||||
throw new Exception("上传到MinIO失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private String extractFileName(String urlStr) {
|
||||
return urlStr.substring(urlStr.lastIndexOf('/') + 1);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue