|
@@ -0,0 +1,76 @@
|
|
|
+package com.example.xiaoshiweixinback.service.common;
|
|
|
+
|
|
|
+import com.example.xiaoshiweixinback.business.utils.JsonUtils;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import okhttp3.*;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.FileCopyUtils;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+import static cn.hutool.core.io.FileUtil.getMimeType;
|
|
|
+
|
|
|
+@RequiredArgsConstructor
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class GetVectorService {
|
|
|
+ /**
|
|
|
+ * 调用文件系统上传文件接口
|
|
|
+ *
|
|
|
+ * @param multipartFiles 文件
|
|
|
+ */
|
|
|
+ @Value("${VectorUrl}")
|
|
|
+ private String vectorUrl;
|
|
|
+ public List<String> uploadFile(File file) throws IOException {
|
|
|
+
|
|
|
+ MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder()
|
|
|
+ .setType(MultipartBody.FORM);
|
|
|
+ //根据文件名获取文件的MIME类型
|
|
|
+ String mimeType = getMimeType(file.getPath());
|
|
|
+ multipartBodyBuilder.addFormDataPart("image_info", file.getName(), RequestBody.create(MediaType.parse(mimeType), file));
|
|
|
+ RequestBody requestBody = multipartBodyBuilder
|
|
|
+ .build();
|
|
|
+ OkHttpClient okHttpClient = new OkHttpClient.Builder()
|
|
|
+ .connectTimeout(60, TimeUnit.SECONDS)
|
|
|
+ .writeTimeout(60, TimeUnit.SECONDS)
|
|
|
+ .readTimeout(60, TimeUnit.SECONDS)
|
|
|
+ .build();
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(vectorUrl + "/getVectorByImg")
|
|
|
+ .post(requestBody)
|
|
|
+ .build();
|
|
|
+ Response response = null;
|
|
|
+ response = okHttpClient.newCall(request).execute();
|
|
|
+
|
|
|
+ String jsons = Objects.requireNonNull(response.body()).string();
|
|
|
+ List<String> stringList = JsonUtils.jsonToList(jsons, String.class);
|
|
|
+
|
|
|
+ return stringList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public List<String> getVectorByText(String key) throws IOException {
|
|
|
+
|
|
|
+ OkHttpClient okHttpClient = new OkHttpClient();
|
|
|
+
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(vectorUrl + "/getVectorByText?keyword="+key)
|
|
|
+ .get()
|
|
|
+ .build();
|
|
|
+ Response response = null;
|
|
|
+ response = okHttpClient.newCall(request).execute();
|
|
|
+ String jsons = Objects.requireNonNull(response.body()).string();
|
|
|
+ List<String> stringList = JsonUtils.jsonToList(jsons, String.class);
|
|
|
+
|
|
|
+ return stringList;
|
|
|
+ }
|
|
|
+}
|