diff --git a/hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/task/service/impl/FileUploaderService.java b/hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/task/service/impl/FileUploaderService.java new file mode 100644 index 0000000..8320629 --- /dev/null +++ b/hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/task/service/impl/FileUploaderService.java @@ -0,0 +1,50 @@ +package com.hnac.hzinfo.inspect.task.service.impl; + +import com.hnac.hzinfo.exception.HzServiceException; +import com.hnac.hzinfo.inspect.utils.FileUtil; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springblade.core.oss.model.BladeFile; +import org.springblade.core.tool.api.R; +import org.springblade.core.tool.api.ResultCode; +import org.springblade.resource.feign.IOssClient; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.stereotype.Service; +import org.springframework.util.Assert; + +/** + * @Author: huangxing + * @Date: 2024/08/13 19:44 + */ +@Service +@AllArgsConstructor +@Slf4j +public class FileUploaderService { + + private final IOssClient ossClient; + + /** + * minio上传文件 + * @param url 待上传文件url + * @param fileName 上传图片名称 + * @return 上传结果 + */ + public BladeFile upload(String url,String fileName) { + MockMultipartFile file = FileUtil.urlToMultipartFile(url, fileName); + return this.upload(file); + } + + /** + * minio上传文件 + * @param file 待上传文件 + * @return 上传结果 + */ + public BladeFile upload(MockMultipartFile file) { + R result = ossClient.putFile(file); + Assert.isTrue(result.isSuccess(), () -> { + throw new HzServiceException(ResultCode.FAILURE,"文件上传失败!"); + }); + return result.getData(); + } + +} diff --git a/hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/utils/FileUtil.java b/hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/utils/FileUtil.java new file mode 100644 index 0000000..5db64be --- /dev/null +++ b/hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/utils/FileUtil.java @@ -0,0 +1,42 @@ +package com.hnac.hzinfo.inspect.utils; + +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import com.hnac.hzinfo.exception.HzServiceException; +import org.springblade.core.oss.model.BladeFile; +import org.springblade.core.tool.api.IResultCode; +import org.springblade.core.tool.api.R; +import org.springblade.core.tool.api.ResultCode; +import org.springblade.core.tool.utils.SpringUtil; +import org.springblade.resource.feign.IOssClient; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.util.Assert; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.multipart.MultipartFile; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; + +/** + * @Author: huangxing + * @Date: 2024/08/13 11:32 + */ +public class FileUtil { + + public static MockMultipartFile urlToMultipartFile(String url, String fileName) { + HttpResponse response = HttpRequest.get(url).execute(); + if (response.isOk()) { + // 读取响应体为字节数组 + byte[] fileBytes = response.bodyBytes(); + // 使用字节数组、文件名和MIME类型创建MockMultipartFile + return new MockMultipartFile( + "file", // 表单字段名,这通常与你的Spring MVC控制器中的@RequestParam("file")匹配 + fileName, // 文件名 + "image/jpeg", // MIME类型 + fileBytes // 文件内容字节数组 + ); + } + return null; + } + +} diff --git a/hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/utils/HiKUtil.java b/hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/utils/HiKUtil.java index 44f44b9..ff61d1b 100644 --- a/hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/utils/HiKUtil.java +++ b/hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/utils/HiKUtil.java @@ -2,6 +2,7 @@ package com.hnac.hzinfo.inspect.utils; import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.databind.JsonNode; @@ -14,7 +15,9 @@ import org.springblade.core.log.exception.ServiceException; import org.springblade.core.tool.jackson.JsonUtil; import org.springblade.core.tool.utils.Func; import org.springframework.beans.factory.annotation.Value; +import org.springframework.mock.web.MockMultipartFile; import org.springframework.util.Assert; +import org.springframework.web.multipart.MultipartFile; import java.util.HashMap; import java.util.Map; @@ -100,10 +103,6 @@ public class HiKUtil { .map(JSONObject::parseObject).map(o -> o.getString("picUrl")).orElse(null); } -// public static void main(String[] args) { -// System.out.println(HiKUtil.manualCapture("5e73bfcd8dba40de9be905588aa5b62b")); -// } - /** * 海康视频调用API * @param api 海康API