From 0814c1d07fde681c26cf9a6797862099a8146f7b Mon Sep 17 00:00:00 2001 From: shahaibo <1023316923@qq.com> Date: Wed, 19 Feb 2025 17:25:48 +0800 Subject: [PATCH] =?UTF-8?q?BUGFXI:=E8=8E=B7=E5=8F=96=E7=9B=B8=E4=BC=BC?= =?UTF-8?q?=E5=BA=A6=E6=8E=A5=E5=8F=A3=E8=B7=AF=E5=BE=84=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mixi/controller/ProductController.java | 13 ++++ .../java/com/mixi/service/PythonService.java | 73 ++++++++++++++++++- .../com/mixi/service/TProductService.java | 5 ++ 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/mixi/controller/ProductController.java b/src/main/java/com/mixi/controller/ProductController.java index 52686da..a7db776 100644 --- a/src/main/java/com/mixi/controller/ProductController.java +++ b/src/main/java/com/mixi/controller/ProductController.java @@ -127,4 +127,17 @@ public class ProductController { return Response.success(tProductService.getCategory(category)); } + @ApiOperation(value = "上传lookbook") + @PostMapping("/lookbookUpload") + public Response 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)); + } + } diff --git a/src/main/java/com/mixi/service/PythonService.java b/src/main/java/com/mixi/service/PythonService.java index 20fabaf..3bed75f 100644 --- a/src/main/java/com/mixi/service/PythonService.java +++ b/src/main/java/com/mixi/service/PythonService.java @@ -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."); + } + } } diff --git a/src/main/java/com/mixi/service/TProductService.java b/src/main/java/com/mixi/service/TProductService.java index 529a677..d2de203 100644 --- a/src/main/java/com/mixi/service/TProductService.java +++ b/src/main/java/com/mixi/service/TProductService.java @@ -1972,4 +1972,9 @@ public class TProductService extends ServiceImpl { List 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; + } }