BUGFXI:获取相似度接口路径修改

This commit is contained in:
shahaibo
2025-02-19 17:25:48 +08:00
parent f22819f666
commit 0814c1d07f
3 changed files with 90 additions and 1 deletions

View File

@@ -127,4 +127,17 @@ public class ProductController {
return Response.success(tProductService.getCategory(category));
}
@ApiOperation(value = "上传lookbook")
@PostMapping("/lookbookUpload")
public Response<Boolean> lookbookUpload(@RequestParam("files") MultipartFile[] files,
@RequestParam(value = "tag") String tag,
@RequestParam(value = "year") String year){
Assert.notNull(files,"Please select a file !");
Assert.isTrue(files.length <=10,"A maximum of 10 images can be uploaded !");
Stream.of(files).forEach(file ->{
Assert.isTrue(!StringUtils.isEmpty(file.getOriginalFilename()),"Please select a file!");
});
return Response.success(tProductService.lookbookUpload(files, tag, year));
}
}

View File

@@ -24,8 +24,10 @@ import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;
@@ -516,7 +518,8 @@ public class PythonService {
content.put("result_number", 5);
RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(content));
Request request = new Request.Builder()
.url(accessPythonIp + ":9993/api/similar_matchsimilar_match")
.url(accessPythonIp + ":9993/api/similar_match")
// .url(accessPythonIp + ":9993/api/similar_matchsimilar_match")
.method("POST", body)
.addHeader("Authorization", "Basic YWlkbGFiOjEyMw==")
.addHeader("Content-Type", "application/json")
@@ -625,4 +628,72 @@ public class PythonService {
//生成失败
throw new BusinessException("AI recommendation exception.");
}
public void lookbookUpload(MultipartFile[] files, String tag, String year) {
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(300, TimeUnit.SECONDS)
.pingInterval(5, TimeUnit.SECONDS)
.readTimeout(300, TimeUnit.SECONDS)
.writeTimeout(300, TimeUnit.SECONDS)
.build();
// 构建 Multipart 表单数据
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
// 添加文件
for (MultipartFile file : files) {
try {
// 将 MultipartFile 转换为 File 对象
File tempFile = File.createTempFile("upload", file.getOriginalFilename());
file.transferTo(tempFile);
multipartBuilder.addFormDataPart("files", file.getOriginalFilename(),
RequestBody.create(MediaType.parse(file.getContentType()), tempFile)); // 修正部分
} catch (IOException e) {
log.error("Error creating temp file for upload", e);
throw new RuntimeException("File upload failed");
}
}
// 添加 tag 和 year 字段
multipartBuilder.addFormDataPart("tag", tag);
multipartBuilder.addFormDataPart("year", year);
RequestBody requestBody = multipartBuilder.build();
Request request = new Request.Builder()
.url("http://18.167.251.121:5001/process-lookbooks") // FastAPI 接口的 URL
.method("POST", requestBody)
.addHeader("Content-Type", "multipart/form-data")
.build();
Response response = null;
String responseBody = null;
try {
log.info("Sending request to FastAPI for lookbook processing...");
response = client.newCall(request).execute();
responseBody = response.body().string();
log.info("Response from FastAPI: {}", responseBody);
if (!response.isSuccessful()) {
log.error("FastAPI returned an error: {}", responseBody);
throw new BusinessException("FastAPI returned an error.");
}
// 使用 org.json.JSONObject 解析 JSON 响应
JSONObject jsonObject = JSON.parseObject(responseBody);
String message = jsonObject.getString("message");
log.info("Lookbook upload result: {}", message);
} catch (IOException ioException) {
log.error("Error communicating with FastAPI: {}", ioException.getMessage(), ioException);
throw new RuntimeException("Error communicating with FastAPI");
}
if (response == null || responseBody == null) {
log.error("No response from FastAPI");
throw new BusinessException("No response from FastAPI.");
}
}
}

View File

@@ -1972,4 +1972,9 @@ public class TProductService extends ServiceImpl<TProductMapper, TProduct> {
List<MiTuProduct> miTuProductList = miTuProductMapper.selectList(qw);
return miTuProductList.stream().map(MiTuProduct::getItemGroup).collect(Collectors.toSet());
}
public Boolean lookbookUpload(MultipartFile[] files, String tag, String year) {
pythonService.lookbookUpload(files, tag, year);
return Boolean.TRUE;
}
}