|
@@ -6,6 +6,9 @@ import java.net.URL;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.*;
|
|
|
|
|
|
+import cn.hutool.http.Header;
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
import cn.hutool.json.JSONArray;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
@@ -28,46 +31,67 @@ public class DifyUtil {
|
|
|
* @return API响应结果
|
|
|
* @throws IOException IO异常
|
|
|
*/
|
|
|
- public String uploadFile(String filePath) throws IOException {
|
|
|
+ public String uploadFile(String filePath,String userid) throws IOException {
|
|
|
String endpoint = "/files/upload";
|
|
|
- HttpURLConnection connection = null;
|
|
|
+// HttpURLConnection connection = null;
|
|
|
File file = new File(filePath);
|
|
|
|
|
|
- try {
|
|
|
- URL url = new URL(API_BASE_URL + endpoint);
|
|
|
- connection = (HttpURLConnection) url.openConnection();
|
|
|
- connection.setRequestMethod("POST");
|
|
|
- connection.setRequestProperty("Authorization", "Bearer " + API_KEY);
|
|
|
- connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
|
|
|
- connection.setDoOutput(true);
|
|
|
+ HashMap<String, Object> paramMap = new HashMap<>();
|
|
|
+ //文件上传只需将参数中的键指定(默认file),值设为文件对象即可,对于使用者来说,文件上传与普通表单提交并无区别
|
|
|
+ paramMap.put("file", file);
|
|
|
+ paramMap.put("user", userid);
|
|
|
|
|
|
- try (OutputStream output = connection.getOutputStream();
|
|
|
- PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8))) {
|
|
|
-
|
|
|
- // 添加文件部分
|
|
|
- writer.append("--").append(BOUNDARY).append("\r\n");
|
|
|
- writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"").append(file.getName()).append("\"\r\n");
|
|
|
- writer.append("Content-Type: application/octet-stream\r\n\r\n");
|
|
|
- writer.flush();
|
|
|
- try (FileInputStream fileInputStream = new FileInputStream(file)) {
|
|
|
- byte[] buffer = new byte[4096];
|
|
|
- int bytesRead;
|
|
|
- while ((bytesRead = fileInputStream.read(buffer)) != -1) {
|
|
|
- output.write(buffer, 0, bytesRead);
|
|
|
- }
|
|
|
- }
|
|
|
- writer.append("\r\n");
|
|
|
-
|
|
|
- // 完成边界
|
|
|
- writer.append("--").append(BOUNDARY).append("--\r\n");
|
|
|
- }
|
|
|
|
|
|
- return getResponseString(connection);
|
|
|
- } finally {
|
|
|
- if (connection != null) {
|
|
|
- connection.disconnect();
|
|
|
- }
|
|
|
- }
|
|
|
+ String result2 = HttpRequest.post(API_BASE_URL + endpoint)
|
|
|
+ .header(Header.AUTHORIZATION, "Bearer " + API_KEY)
|
|
|
+ .form(paramMap)//表单内容
|
|
|
+ .timeout(-1)//超时,毫秒
|
|
|
+ .execute().body();
|
|
|
+ return result2;
|
|
|
+
|
|
|
+
|
|
|
+//
|
|
|
+// try {
|
|
|
+// URL url = new URL(API_BASE_URL + endpoint);
|
|
|
+// connection = (HttpURLConnection) url.openConnection();
|
|
|
+// connection.setRequestMethod("POST");
|
|
|
+// connection.setRequestProperty("Authorization", "Bearer " + API_KEY);
|
|
|
+// connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
|
|
|
+// connection.setDoOutput(true);
|
|
|
+//
|
|
|
+// try (OutputStream output = connection.getOutputStream();
|
|
|
+// PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8))) {
|
|
|
+//
|
|
|
+// // 添加文件部分
|
|
|
+// writer.append("--").append(BOUNDARY).append("\r\n");
|
|
|
+// writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"").append(file.getName()).append("\"\r\n");
|
|
|
+// writer.append("Content-Type: application/octet-stream\r\n\r\n");
|
|
|
+// writer.flush();
|
|
|
+// try (FileInputStream fileInputStream = new FileInputStream(file)) {
|
|
|
+// byte[] buffer = new byte[4096];
|
|
|
+// int bytesRead;
|
|
|
+// while ((bytesRead = fileInputStream.read(buffer)) != -1) {
|
|
|
+// output.write(buffer, 0, bytesRead);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// writer.append("\r\n");
|
|
|
+//
|
|
|
+// // 添加用户参数部分
|
|
|
+// writer.append("--").append(BOUNDARY).append("\r\n");
|
|
|
+// writer.append("Content-Disposition: form-data; name=\"user\"\r\n\r\n");
|
|
|
+// writer.append(userid).append("\r\n");
|
|
|
+//
|
|
|
+//
|
|
|
+// // 完成边界
|
|
|
+// writer.append("--").append(BOUNDARY).append("--\r\n");
|
|
|
+// }
|
|
|
+//
|
|
|
+// return getResponseString(connection);
|
|
|
+// } finally {
|
|
|
+// if (connection != null) {
|
|
|
+// connection.disconnect();
|
|
|
+// }
|
|
|
+// }
|
|
|
}
|
|
|
|
|
|
/**
|