WANG 6 年之前
父節點
當前提交
ea7612fffe

+ 58 - 0
src/main/java/cn/com/qmth/examcloud/commons/helpers/FormFileDataPart.java

@@ -0,0 +1,58 @@
+package cn.com.qmth.examcloud.commons.helpers;
+
+import java.io.File;
+
+/**
+ * 表单文件参数
+ *
+ * @author WANGWEI
+ * @date 2019年5月9日
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
+ */
+public class FormFileDataPart {
+
+	private String paramName;
+
+	private String filename;
+
+	private File file;
+
+	/**
+	 * 构造函数
+	 *
+	 * @param paramName
+	 * @param filename
+	 * @param file
+	 */
+	public FormFileDataPart(String paramName, String filename, File file) {
+		super();
+		this.paramName = paramName;
+		this.filename = filename;
+		this.file = file;
+	}
+
+	public String getParamName() {
+		return paramName;
+	}
+
+	public void setParamName(String paramName) {
+		this.paramName = paramName;
+	}
+
+	public String getFilename() {
+		return filename;
+	}
+
+	public void setFilename(String filename) {
+		this.filename = filename;
+	}
+
+	public File getFile() {
+		return file;
+	}
+
+	public void setFile(File file) {
+		this.file = file;
+	}
+
+}

+ 4 - 40
src/main/java/cn/com/qmth/examcloud/commons/util/OKHttpUtil.java

@@ -1,6 +1,5 @@
 package cn.com.qmth.examcloud.commons.util;
 
-import java.io.File;
 import java.io.IOException;
 import java.util.List;
 import java.util.Map;
@@ -9,6 +8,7 @@ import java.util.concurrent.TimeUnit;
 
 import org.apache.commons.collections.CollectionUtils;
 
+import cn.com.qmth.examcloud.commons.helpers.FormFileDataPart;
 import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
 import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
 import okhttp3.FormBody;
@@ -248,42 +248,6 @@ public class OKHttpUtil {
 		}
 	}
 
-	/**
-	 * 表单文件参数
-	 *
-	 * @author WANGWEI
-	 * @date 2019年4月10日
-	 * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved.
-	 */
-	public static class FileDataPart {
-
-		private String name;
-
-		private String filename;
-
-		private File file;
-
-		public FileDataPart(String name, String filename, File file) {
-			super();
-			this.name = name;
-			this.filename = filename;
-			this.file = file;
-		}
-
-		public String getName() {
-			return name;
-		}
-
-		public String getFilename() {
-			return filename;
-		}
-
-		public File getFile() {
-			return file;
-		}
-
-	}
-
 	/**
 	 * 发送请求 (包含文件表单)
 	 *
@@ -296,7 +260,7 @@ public class OKHttpUtil {
 	 * @return
 	 */
 	public static Response call(HttpMethod httpMethod, String url, Map<String, String> headers,
-			Map<String, String> params, List<FileDataPart> fileDataPartList) {
+			Map<String, String> params, List<FormFileDataPart> fileDataPartList) {
 
 		LOG.info("[okhttp3] new call: " + httpMethod + " " + url);
 		LOG.info("[okhttp3] headers: " + JsonUtil.toJson(headers));
@@ -313,9 +277,9 @@ public class OKHttpUtil {
 
 		if (CollectionUtils.isNotEmpty(fileDataPartList)) {
 			MediaType type = MediaType.parse("application/octet-stream");
-			for (FileDataPart fileDataPart : fileDataPartList) {
+			for (FormFileDataPart fileDataPart : fileDataPartList) {
 				RequestBody fileBody = RequestBody.create(type, fileDataPart.getFile());
-				multipartBodyBuilder.addFormDataPart(fileDataPart.getName(),
+				multipartBodyBuilder.addFormDataPart(fileDataPart.getParamName(),
 						fileDataPart.getFilename(), fileBody);
 			}
 		}