添加了上传文件的接口
This commit is contained in:
@@ -12,6 +12,7 @@ import com.srs.common.core.domain.AjaxResult; // ✅ RuoYi 的返回结果类
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||
|
||||
@@ -57,10 +58,12 @@ public class AiChatController extends BaseController {
|
||||
* Dify会话API的基础URL
|
||||
* 用于获取会话列表
|
||||
*/
|
||||
|
||||
private static final String DIFY_CONVERSATIONS_URL = "http://47.112.118.149:8100/v1/conversations";
|
||||
//private static final String DIFY_CONVERSATIONS_URL = "http://localhost:8080/v1/conversations";
|
||||
|
||||
//文件上传API
|
||||
private static final String DIFY_FILES_URL = "http://47.112.118.149:8100/v1/files/upload";
|
||||
|
||||
/**
|
||||
* Dify API的访问密钥
|
||||
* 用于身份验证,授权访问Dify服务
|
||||
@@ -608,4 +611,64 @@ public class AiChatController extends BaseController {
|
||||
return error("获取会话列表失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传接口
|
||||
* <p>
|
||||
* 上传文件并在发送消息时使用,可实现图文多模态理解。支持应用程序所支持的所有格式。
|
||||
* 上传的文件仅供当前终端用户使用。
|
||||
* </p>
|
||||
*
|
||||
* @param file 要上传的文件
|
||||
* @param user 用户标识,用于定义终端用户的身份
|
||||
* @return 包含文件信息的统一响应结果
|
||||
*/
|
||||
@PostMapping("/files/upload")
|
||||
public AjaxResult uploadFile(@RequestParam("file") MultipartFile file,
|
||||
@RequestParam("user") String user) {
|
||||
try {
|
||||
// 检查文件是否为空
|
||||
if (file.isEmpty()) {
|
||||
return AjaxResult.error("文件不能为空");
|
||||
}
|
||||
|
||||
// 创建MultipartBody以上传文件
|
||||
RequestBody requestBody = new MultipartBody.Builder()
|
||||
.setType(MultipartBody.FORM)
|
||||
.addFormDataPart("user", user)
|
||||
.addFormDataPart("file", file.getOriginalFilename(),
|
||||
RequestBody.create(MediaType.parse("application/octet-stream"),file.getBytes()))
|
||||
.build();
|
||||
|
||||
// 构建请求
|
||||
Request request = new Request.Builder()
|
||||
.url(DIFY_FILES_URL)
|
||||
.addHeader("Authorization", "Bearer " + DIFY_API_KEY)
|
||||
.post(requestBody)
|
||||
.build();
|
||||
|
||||
// 发送请求
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
if (response.isSuccessful()) {
|
||||
String responseBody = response.body().string();
|
||||
JsonNode rootNode = mapper.readTree(responseBody);
|
||||
|
||||
// 返回成功结果
|
||||
return AjaxResult.success("文件上传成功", mapper.convertValue(rootNode, Map.class));
|
||||
} else {
|
||||
String errorMsg = "文件上传失败: " + response.code() + " " + response.message();
|
||||
try (ResponseBody errorBody = response.body()) {
|
||||
if (errorBody != null) {
|
||||
errorMsg += " - " + errorBody.string();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
errorMsg += " (无法读取错误详情)";
|
||||
}
|
||||
return AjaxResult.error(errorMsg);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return AjaxResult.error("文件上传异常: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user