2.0
|
@ -1,20 +1,18 @@
|
||||||
package com.example.aitest.controller;
|
package com.example.aitest.controller;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.example.aitest.dto.ApiDto;
|
import com.example.aitest.dto.ApiDto;
|
||||||
import com.example.aitest.service.ApiService;
|
import com.example.aitest.service.ApiService;
|
||||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
@RestController //注解标识这是一个控制器类
|
@RestController //注解标识这是一个控制器类
|
||||||
@CrossOrigin //加上CrossOrigin可解决跨域问题
|
@CrossOrigin //加上CrossOrigin可解决跨域问题
|
||||||
|
@ -30,12 +28,22 @@ public class ApiController {
|
||||||
|
|
||||||
// 设置保存路径
|
// 设置保存路径
|
||||||
// 动态生成文件名
|
// 动态生成文件名
|
||||||
String uniqueFileName = UUID.randomUUID().toString() + ".png";
|
long time = System.currentTimeMillis();
|
||||||
Path path = Paths.get("static/images/avatar/" + uniqueFileName);
|
String uniqueFileName = time + ".png";
|
||||||
|
Path path = Paths.get("static/images/" + uniqueFileName);
|
||||||
Files.createDirectories(path.getParent());
|
Files.createDirectories(path.getParent());
|
||||||
Files.write(path, decodedBytes);
|
Files.write(path, decodedBytes);
|
||||||
// 调用OpenAI API
|
// 调用OpenAI API
|
||||||
return openAiService.processImage(path.toFile());
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,6 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
package com.example.aitest.service;
|
package com.example.aitest.service;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
public interface ApiService {
|
public interface ApiService {
|
||||||
JSONObject processImage(File imageFile);
|
JSONObject processImage(File imageFile);
|
||||||
|
|
||||||
|
ResponseEntity<String> timeChange(String time);
|
||||||
|
|
||||||
|
ResponseEntity<String> timeGet();
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,8 +9,11 @@ import org.apache.http.entity.ContentType;
|
||||||
import org.apache.http.entity.StringEntity;
|
import org.apache.http.entity.StringEntity;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClients;
|
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 org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@ -18,6 +21,8 @@ import java.io.IOException;
|
||||||
public class ApiServiceImpl implements ApiService {
|
public class ApiServiceImpl implements ApiService {
|
||||||
private static final String API_SECRET_KEY = "sk-zk23dcb27bb18e67d9ea0d749ed62f8088a667936c4c0682";
|
private static final String API_SECRET_KEY = "sk-zk23dcb27bb18e67d9ea0d749ed62f8088a667936c4c0682";
|
||||||
private static final String BASE_URL = "https://api.zhizengzeng.com/v1/chat/completions";
|
private static final String BASE_URL = "https://api.zhizengzeng.com/v1/chat/completions";
|
||||||
|
@Resource
|
||||||
|
RedisTemplate<String, Object> redisTemplate;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JSONObject processImage(File imageFile) {
|
public JSONObject processImage(File imageFile) {
|
||||||
|
@ -31,7 +36,7 @@ public class ApiServiceImpl implements ApiService {
|
||||||
JSONObject imageContent = new JSONObject();
|
JSONObject imageContent = new JSONObject();
|
||||||
imageContent.put("type", "image_url");
|
imageContent.put("type", "image_url");
|
||||||
JSONObject imageUrl = new JSONObject();
|
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);
|
imageContent.put("image_url", imageUrl);
|
||||||
|
|
||||||
JSONObject message = new JSONObject();
|
JSONObject message = new JSONObject();
|
||||||
|
@ -56,6 +61,7 @@ public class ApiServiceImpl implements ApiService {
|
||||||
// Parse the JSON response
|
// Parse the JSON response
|
||||||
String responseBody = new String(response.getEntity().getContent().readAllBytes());
|
String responseBody = new String(response.getEntity().getContent().readAllBytes());
|
||||||
JSONObject jsonResponse = JSON.parseObject(responseBody);
|
JSONObject jsonResponse = JSON.parseObject(responseBody);
|
||||||
|
jsonResponse.put("imgUrl", imageFile.getName());
|
||||||
System.out.println(jsonResponse.toJSONString());
|
System.out.println(jsonResponse.toJSONString());
|
||||||
return jsonResponse;
|
return jsonResponse;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -63,4 +69,23 @@ public class ApiServiceImpl implements ApiService {
|
||||||
throw new RuntimeException(e);
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ spring:
|
||||||
host: localhost
|
host: localhost
|
||||||
datasource:
|
datasource:
|
||||||
username: root
|
username: root
|
||||||
password: ob666666
|
password: 3534957771fb4925
|
||||||
url: jdbc:mysql://localhost:3306/AITest
|
url: jdbc:mysql://localhost:3306/AITest
|
||||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
mybatis:
|
mybatis:
|
||||||
|
|
Before Width: | Height: | Size: 116 KiB |
Before Width: | Height: | Size: 116 KiB |
Before Width: | Height: | Size: 115 KiB |
Before Width: | Height: | Size: 112 KiB |
Before Width: | Height: | Size: 116 KiB |
Before Width: | Height: | Size: 115 KiB |
Before Width: | Height: | Size: 114 KiB |
Before Width: | Height: | Size: 115 KiB |