This commit is contained in:
Looly
2020-05-14 01:56:03 +08:00
parent 3da83e0227
commit d0fe78ae66
14 changed files with 337 additions and 156 deletions

View File

@@ -27,11 +27,13 @@ public class SimpleServerTest {
// 文件上传测试
// http://localhost:8888/formTest?a=1&a=2&b=3
.addAction("/file", (request, response) -> {
final UploadFile file = request.getMultipart().getFile("file");
final UploadFile[] files = request.getMultipart().getFiles("file");
// 传入目录默认读取HTTP头中的文件名然后创建文件
file.write("d:/test/");
Console.log("Write file to: d:/test/");
response.write(request.getParams().toString(), ContentType.TEXT_PLAIN.toString());
for (UploadFile file : files) {
file.write("d:/test/");
Console.log("Write file: d:/test/" + file.getFileName());
}
response.write(request.getMultipart().getParamMap().toString(), ContentType.TEXT_PLAIN.toString());
}
)
.start();

View File

@@ -1,15 +1,15 @@
package cn.hutool.http.test;
import java.io.File;
import java.util.HashMap;
import org.junit.Ignore;
import org.junit.Test;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
import java.util.HashMap;
/**
* 文件上传单元测试
@@ -24,16 +24,16 @@ public class UploadTest {
@Test
@Ignore
public void uploadFilesTest() {
File file = FileUtil.file("e:\\face.jpg");
File file2 = FileUtil.file("e:\\face2.jpg");
File file = FileUtil.file("d:\\图片1.JPG");
File file2 = FileUtil.file("d:\\图片3.png");
// 方法一:自定义构建表单
HttpRequest request = HttpRequest//
.post("http://localhost:8090/file/upload")//
.post("http://localhost:8888/file")//
.form("file", file2, file)//
.form("fileType", "图片");
HttpResponse response = request.execute();
System.out.println(response.body());
Console.log(response.body());
}
@Test