This commit is contained in:
xbx 2024-07-04 18:19:00 +08:00
parent 6676fcd307
commit caff05b057
13 changed files with 48 additions and 12 deletions

View File

@ -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<String> timeChange(@RequestParam("time") String time) {
return openAiService.timeChange(time);
}
@RequestMapping("/timeGet")
public ResponseEntity<String> timeGet() {
return openAiService.timeGet();
}
}

View File

@ -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

View File

@ -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<String> timeChange(String time);
ResponseEntity<String> timeGet();
}

View File

@ -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<String, Object> 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<String> timeChange(String time) {
try {
redisTemplate.opsForValue().set("CollectionTime", time);
} catch (Exception e) {
return ResponseEntity.badRequest().body("error");
}
return ResponseEntity.ok("success");
}
@Override
public ResponseEntity<String> timeGet() {
Object collectionTime = redisTemplate.opsForValue().get("CollectionTime");
if (collectionTime == null) {
return ResponseEntity.badRequest().body("error");
}
return ResponseEntity.ok(collectionTime.toString());
}
}

View File

@ -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:

Binary file not shown.

Before

Width:  |  Height:  |  Size: 116 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 116 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 115 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 112 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 116 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 115 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 114 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 115 KiB