WANG 5 年之前
父节点
当前提交
71810cfde6
共有 1 个文件被更改,包括 64 次插入0 次删除
  1. 64 0
      src/main/java/cn/com/qmth/examcloud/web/upyun/UpyunTest.java

+ 64 - 0
src/main/java/cn/com/qmth/examcloud/web/upyun/UpyunTest.java

@@ -0,0 +1,64 @@
+package cn.com.qmth.examcloud.web.upyun;
+
+import java.io.File;
+import java.util.Map.Entry;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.RandomUtils;
+import org.apache.http.Consts;
+import org.apache.http.HttpEntity;
+import org.apache.http.StatusLine;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.mime.HttpMultipartMode;
+import org.apache.http.entity.mime.MultipartEntityBuilder;
+import org.apache.http.entity.mime.content.StringBody;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+
+import cn.com.qmth.examcloud.commons.util.JsonUtil;
+import cn.com.qmth.examcloud.commons.util.MD5;
+import cn.com.qmth.examcloud.web.support.SpringContextHolder;
+
+public class UpyunTest {
+
+	public static void testFormApi() {
+		File file = new File("D:/temp/fuck.jpg");
+		String md5 = MD5.md5Hex(file);
+
+		UpyunService upyunService = SpringContextHolder.getBean(UpyunService.class);
+		UpyunPathEnvironmentInfo env = new UpyunPathEnvironmentInfo();
+		env.setFileSuffix(".jpg");
+		env.setRootOrgId(String.valueOf(RandomUtils.nextLong()));
+		UpYunHttpRequest upYunSign = upyunService.buildUpYunHttpRequest("test", env, md5);
+		System.out.println("upYunSign: " + JsonUtil.toPrettyJson(upYunSign));
+
+		HttpPost httpPost = new HttpPost(upYunSign.getFormUrl());
+		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
+		builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
+		for (Entry<String, String> formFilePart : upYunSign.getFormParams().entrySet()) {
+			builder.addPart(formFilePart.getKey(), new StringBody(formFilePart.getValue(),
+					ContentType.create("text/plain", Consts.UTF_8)));
+		}
+		builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, "fuck.jpg");
+		HttpEntity entity = builder.build();
+		httpPost.setEntity(entity);
+
+		CloseableHttpClient httpclient = HttpClients.createDefault();
+		CloseableHttpResponse resp = null;
+		try {
+			resp = httpclient.execute(httpPost);
+			StatusLine statusLine = resp.getStatusLine();
+			System.out.println(statusLine);
+			System.out.println(EntityUtils.toString(resp.getEntity(), "UTF-8"));
+		} catch (Exception e) {
+			e.printStackTrace();
+		} finally {
+			IOUtils.closeQuietly(resp);
+			IOUtils.closeQuietly(httpclient);
+		}
+	}
+
+}