diff --git a/src/main/java/com/example/aitest/controller/ApiController.java b/src/main/java/com/example/aitest/controller/ApiController.java index ffe7f59..5022992 100644 --- a/src/main/java/com/example/aitest/controller/ApiController.java +++ b/src/main/java/com/example/aitest/controller/ApiController.java @@ -1,20 +1,18 @@ package com.example.aitest.controller; - +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.example.aitest.dto.ApiDto; import com.example.aitest.service.ApiService; -import org.springframework.web.bind.annotation.CrossOrigin; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + import javax.annotation.Resource; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Base64; -import java.util.UUID; @RestController //注解标识这是一个控制器类 @CrossOrigin //加上CrossOrigin可解决跨域问题 @@ -30,12 +28,22 @@ public class ApiController { // 设置保存路径 // 动态生成文件名 - String uniqueFileName = UUID.randomUUID().toString() + ".png"; - Path path = Paths.get("static/images/avatar/" + uniqueFileName); + long time = System.currentTimeMillis(); + String uniqueFileName = time + ".png"; + Path path = Paths.get("static/images/" + uniqueFileName); Files.createDirectories(path.getParent()); Files.write(path, decodedBytes); // 调用OpenAI API return openAiService.processImage(path.toFile()); } + @RequestMapping("/timeChange") + public ResponseEntity timeChange(@RequestParam("time") String time) { + return openAiService.timeChange(time); + } + @RequestMapping("/timeGet") + public ResponseEntity timeGet() { + return openAiService.timeGet(); + } + } diff --git a/src/main/java/com/example/aitest/controller/UserController.java b/src/main/java/com/example/aitest/controller/UserController.java index 4a2f6c3..521122f 100644 --- a/src/main/java/com/example/aitest/controller/UserController.java +++ b/src/main/java/com/example/aitest/controller/UserController.java @@ -7,7 +7,6 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; -import javax.annotation.Resource; import java.security.NoSuchAlgorithmException; @RestController diff --git a/src/main/java/com/example/aitest/service/ApiService.java b/src/main/java/com/example/aitest/service/ApiService.java index b58182d..4b7b313 100644 --- a/src/main/java/com/example/aitest/service/ApiService.java +++ b/src/main/java/com/example/aitest/service/ApiService.java @@ -1,10 +1,14 @@ package com.example.aitest.service; import com.alibaba.fastjson.JSONObject; -import org.springframework.stereotype.Service; +import org.springframework.http.ResponseEntity; import java.io.File; public interface ApiService { JSONObject processImage(File imageFile); + + ResponseEntity timeChange(String time); + + ResponseEntity timeGet(); } diff --git a/src/main/java/com/example/aitest/service/impl/ApiServiceImpl.java b/src/main/java/com/example/aitest/service/impl/ApiServiceImpl.java index 8380cfb..ffcbcff 100644 --- a/src/main/java/com/example/aitest/service/impl/ApiServiceImpl.java +++ b/src/main/java/com/example/aitest/service/impl/ApiServiceImpl.java @@ -9,8 +9,11 @@ import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; +import javax.annotation.Resource; import java.io.File; import java.io.IOException; @@ -18,6 +21,8 @@ import java.io.IOException; public class ApiServiceImpl implements ApiService { private static final String API_SECRET_KEY = "sk-zk23dcb27bb18e67d9ea0d749ed62f8088a667936c4c0682"; private static final String BASE_URL = "https://api.zhizengzeng.com/v1/chat/completions"; + @Resource + RedisTemplate redisTemplate; @Override public JSONObject processImage(File imageFile) { @@ -31,7 +36,7 @@ public class ApiServiceImpl implements ApiService { JSONObject imageContent = new JSONObject(); imageContent.put("type", "image_url"); JSONObject imageUrl = new JSONObject(); - imageUrl.put("url", "http://124.71.135.249:8081/images/avatar/" + imageFile.getName()); + imageUrl.put("url", "http://49.233.248.140:8081/images/" + imageFile.getName()); imageContent.put("image_url", imageUrl); JSONObject message = new JSONObject(); @@ -56,6 +61,7 @@ public class ApiServiceImpl implements ApiService { // Parse the JSON response String responseBody = new String(response.getEntity().getContent().readAllBytes()); JSONObject jsonResponse = JSON.parseObject(responseBody); + jsonResponse.put("imgUrl", imageFile.getName()); System.out.println(jsonResponse.toJSONString()); return jsonResponse; } catch (IOException e) { @@ -63,4 +69,23 @@ public class ApiServiceImpl implements ApiService { throw new RuntimeException(e); } } + + @Override + public ResponseEntity timeChange(String time) { + try { + redisTemplate.opsForValue().set("CollectionTime", time); + } catch (Exception e) { + return ResponseEntity.badRequest().body("error"); + } + return ResponseEntity.ok("success"); + } + + @Override + public ResponseEntity timeGet() { + Object collectionTime = redisTemplate.opsForValue().get("CollectionTime"); + if (collectionTime == null) { + return ResponseEntity.badRequest().body("error"); + } + return ResponseEntity.ok(collectionTime.toString()); + } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index ce4bde9..51f4d61 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -7,7 +7,7 @@ spring: host: localhost datasource: username: root - password: ob666666 + password: 3534957771fb4925 url: jdbc:mysql://localhost:3306/AITest driver-class-name: com.mysql.cj.jdbc.Driver mybatis: diff --git a/uploads/1624327e-b33d-46c9-bbf2-11ff097d0f29.png b/uploads/1624327e-b33d-46c9-bbf2-11ff097d0f29.png deleted file mode 100644 index d9735fa..0000000 Binary files a/uploads/1624327e-b33d-46c9-bbf2-11ff097d0f29.png and /dev/null differ diff --git a/uploads/31093de1-bd07-4d8f-84d2-1a5382cf7bba.png b/uploads/31093de1-bd07-4d8f-84d2-1a5382cf7bba.png deleted file mode 100644 index 3cc8d6c..0000000 Binary files a/uploads/31093de1-bd07-4d8f-84d2-1a5382cf7bba.png and /dev/null differ diff --git a/uploads/46a29353-9086-4d9a-9bff-1f295f4273de.png b/uploads/46a29353-9086-4d9a-9bff-1f295f4273de.png deleted file mode 100644 index f62f21c..0000000 Binary files a/uploads/46a29353-9086-4d9a-9bff-1f295f4273de.png and /dev/null differ diff --git a/uploads/7ec1cfdd-9ed5-491e-a96c-4ee7e46339c8.png b/uploads/7ec1cfdd-9ed5-491e-a96c-4ee7e46339c8.png deleted file mode 100644 index 5ad442d..0000000 Binary files a/uploads/7ec1cfdd-9ed5-491e-a96c-4ee7e46339c8.png and /dev/null differ diff --git a/uploads/8ac40278-ad9d-4c5a-a67c-aab96610524b.png b/uploads/8ac40278-ad9d-4c5a-a67c-aab96610524b.png deleted file mode 100644 index e2182e8..0000000 Binary files a/uploads/8ac40278-ad9d-4c5a-a67c-aab96610524b.png and /dev/null differ diff --git a/uploads/c0781c16-d9bb-4a9b-9f06-ff276aac8114.png b/uploads/c0781c16-d9bb-4a9b-9f06-ff276aac8114.png deleted file mode 100644 index 9a156de..0000000 Binary files a/uploads/c0781c16-d9bb-4a9b-9f06-ff276aac8114.png and /dev/null differ diff --git a/uploads/c5a12bd5-35bc-4eb7-96d9-45b68e3946d3.png b/uploads/c5a12bd5-35bc-4eb7-96d9-45b68e3946d3.png deleted file mode 100644 index ad1d1a9..0000000 Binary files a/uploads/c5a12bd5-35bc-4eb7-96d9-45b68e3946d3.png and /dev/null differ diff --git a/uploads/dc4180c9-4f2d-4788-bdc8-0f9fe511b32a.png b/uploads/dc4180c9-4f2d-4788-bdc8-0f9fe511b32a.png deleted file mode 100644 index de4345c..0000000 Binary files a/uploads/dc4180c9-4f2d-4788-bdc8-0f9fe511b32a.png and /dev/null differ