use of com.dtflys.forest.multipart.ForestMultipart in project forest by dromara.
the class DefaultBinaryConverter method encodeRequestBody.
@Override
public byte[] encodeRequestBody(ForestRequest request, Charset charset) {
ForestBody reqBody = request.body();
List<ForestMultipart> multiparts = request.getMultiparts();
List<byte[]> byteList = new LinkedList<>();
int size = 0;
for (ForestMultipart multipart : multiparts) {
byte[] byteArray = multipart.getBytes();
byteList.add(byteArray);
size += byteArray.length;
}
for (ForestRequestBody body : reqBody) {
byte[] byteArray = body.getByteArray();
byteList.add(byteArray);
size += byteArray.length;
}
byte[] bytes = new byte[size];
int pos = 0;
for (byte[] bytesItem : byteList) {
for (int i = 0; i < bytesItem.length; i++) {
bytes[pos + i] = bytesItem[i];
}
pos += bytesItem.length;
}
return bytes;
}
use of com.dtflys.forest.multipart.ForestMultipart in project forest by dromara.
the class TestUploadClient method testUploadByteArrayMap.
/**
* Test Byte Array Collections *
*/
@Test
public void testUploadByteArrayMap() throws IOException, InterruptedException, FileUploadException {
server.enqueue(new MockResponse().setBody(EXPECTED));
URL[] urlArray = new URL[] { this.getClass().getResource("/test-img.jpg"), this.getClass().getResource("/test-img2.jpg") };
Map<String, byte[]> byteArrayMap = new LinkedHashMap<>();
for (int i = 0; i < urlArray.length; i++) {
URL url = urlArray[i];
byte[] byteArray = IOUtils.toByteArray(url);
byteArrayMap.put("test-img-" + i + ".jpg", byteArray);
}
ForestRequest<Map> request = uploadClient.uploadByteArrayMap(byteArrayMap);
assertNotNull(request);
List<ForestMultipart> multipartList = request.getMultiparts();
assertEquals(2, multipartList.size());
// assertTrue(Map.class.isAssignableFrom(request.getMethod().getReturnClass()));
int i = 0;
for (ForestMultipart multipart : multipartList) {
assertTrue(multipart instanceof ByteArrayMultipart);
assertEquals("file", multipart.getName());
byte[] bytes = multipart.getBytes();
String key = "test-img-" + i + ".jpg";
byte[] byteArray = byteArrayMap.get(key);
assertNotNull(bytes);
assertByteArrayEquals(byteArray, bytes);
assertEquals(key, multipart.getOriginalFileName());
i++;
}
Map result = (Map) request.execute();
assertNotNull(result);
MockServerRequest.mockRequest(server).assertMultipart("file", fileItems -> {
assertEquals(2, fileItems.size());
FileItem fileItem1 = fileItems.get(0);
assertEquals("test-img-0.jpg", fileItem1.getName());
assertEquals("image/jpeg", fileItem1.getContentType());
try {
byte[] bytes = IOUtils.toByteArray(fileItem1.getInputStream());
URL url = urlArray[0];
assertNotNull(url);
assertArrayEquals(IOUtils.toByteArray(new FileInputStream(url.getFile())), bytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
FileItem fileItem2 = fileItems.get(1);
assertEquals("test-img-1.jpg", fileItem2.getName());
assertEquals("image/jpeg", fileItem2.getContentType());
try {
byte[] bytes = IOUtils.toByteArray(fileItem2.getInputStream());
URL url = urlArray[1];
assertNotNull(url);
assertArrayEquals(IOUtils.toByteArray(new FileInputStream(url.getFile())), bytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
use of com.dtflys.forest.multipart.ForestMultipart in project forest by dromara.
the class TestUploadClient method testUploadFile.
@Test
public void testUploadFile() throws InterruptedException, FileUploadException {
server.enqueue(new MockResponse().setBody(EXPECTED));
String path = Objects.requireNonNull(this.getClass().getResource("/test-img.jpg")).getPath();
if (path.startsWith("/") && isWindows()) {
path = path.substring(1);
}
File file = new File(path);
ForestRequest<Map> request = uploadClient.upload(file, progress -> {
});
assertNotNull(request);
List<ForestMultipart> multipartList = request.getMultiparts();
assertEquals(1, multipartList.size());
assertTrue(StringUtils.isNotBlank(request.getBoundary()));
ForestMultipart multipart = multipartList.get(0);
// assertTrue(Map.class.isAssignableFrom(request.getMethod().getReturnClass()));
assertTrue(multipart instanceof FileMultipart);
assertEquals("file", multipart.getName());
assertEquals("test-img.jpg", multipart.getOriginalFileName());
Map result = (Map) request.execute();
assertNotNull(result);
MockServerRequest.mockRequest(server).assertMultipart("file", fileItems -> {
assertEquals(1, fileItems.size());
FileItem fileItem = fileItems.get(0);
assertEquals("test-img.jpg", fileItem.getName());
assertEquals("image/jpeg", fileItem.getContentType());
try {
byte[] bytes = IOUtils.toByteArray(fileItem.getInputStream());
assertArrayEquals(IOUtils.toByteArray(new FileInputStream(file)), bytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
use of com.dtflys.forest.multipart.ForestMultipart in project forest by dromara.
the class TestUploadClient method testMixtureImageUploadWithJSONBodyParamsAndWithoutContentType.
@Test
public void testMixtureImageUploadWithJSONBodyParamsAndWithoutContentType() throws InterruptedException, FileUploadException {
server.enqueue(new MockResponse().setBody(EXPECTED));
String path = Objects.requireNonNull(this.getClass().getResource("/test-img.jpg")).getPath();
if (path.startsWith("/") && isWindows()) {
path = path.substring(1);
}
File file = new File(path);
Map<String, Object> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
ForestRequest request = uploadClient.imageUploadWithJSONBodyParamsAndWithoutContentType("img1.jpg", file, map);
assertNotNull(request);
List<ForestMultipart> multipartList = request.getMultiparts();
assertEquals(1, multipartList.size());
ForestMultipart multipart = multipartList.get(0);
// assertTrue(Map.class.isAssignableFrom(request.getMethod().getReturnClass()));
assertTrue(multipart instanceof FileMultipart);
assertEquals("file", multipart.getName());
assertEquals("img1.jpg", multipart.getOriginalFileName());
Map result = (Map) request.execute();
assertNotNull(result);
MockServerRequest.mockRequest(server).assertMultipart("file", multiparts -> {
assertEquals(1, multiparts.size());
FileItem fileItem = multiparts.get(0);
assertEquals("img1.jpg", fileItem.getName());
assertEquals("image/jpeg", fileItem.getContentType());
}).assertMultipart("params", params -> {
assertEquals(1, params.size());
FileItem item = params.get(0);
ContentType contentType = new ContentType(item.getContentType());
assertEquals("application/json", contentType.toStringWithoutParameters());
try {
assertEquals(JSON.toJSONString(map), IOUtils.toString(item.getInputStream(), "UTF-8"));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
use of com.dtflys.forest.multipart.ForestMultipart in project forest by dromara.
the class TestUploadClient method testUploadPathArray.
@Test
public void testUploadPathArray() throws InterruptedException, FileUploadException {
server.enqueue(new MockResponse().setBody(EXPECTED));
URL[] urlArray = new URL[] { this.getClass().getResource("/test-img.jpg"), this.getClass().getResource("/test-img2.jpg") };
String[] pathArray = new String[urlArray.length];
for (int i = 0; i < urlArray.length; i++) {
URL url = urlArray[i];
String path = getPathFromURL(url);
pathArray[i] = path;
}
ForestRequest<Map> request = uploadClient.uploadPathArray(pathArray);
assertNotNull(request);
List<ForestMultipart> multipartList = request.getMultiparts();
assertEquals(2, multipartList.size());
// assertTrue(Map.class.isAssignableFrom(request.getMethod().getReturnClass()));
int i = 0;
for (ForestMultipart multipart : multipartList) {
assertTrue(multipart instanceof FilePathMultipart);
assertEquals("file", multipart.getName());
File file = multipart.getFile();
assertNotNull(file);
assertEquals("test-img-" + i + ".jpg", multipart.getOriginalFileName());
i++;
}
Map result = (Map) request.execute();
assertNotNull(result);
MockServerRequest.mockRequest(server).assertMultipart("file", fileItems -> {
assertEquals(2, fileItems.size());
FileItem fileItem1 = fileItems.get(0);
assertEquals("test-img-0.jpg", fileItem1.getName());
assertEquals("image/jpeg", fileItem1.getContentType());
try {
byte[] bytes = IOUtils.toByteArray(fileItem1.getInputStream());
URL url = urlArray[0];
assertNotNull(url);
assertArrayEquals(IOUtils.toByteArray(new FileInputStream(url.getFile())), bytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
FileItem fileItem2 = fileItems.get(1);
assertEquals("test-img-1.jpg", fileItem2.getName());
assertEquals("image/jpeg", fileItem2.getContentType());
try {
byte[] bytes = IOUtils.toByteArray(fileItem2.getInputStream());
URL url = urlArray[1];
assertNotNull(url);
assertArrayEquals(IOUtils.toByteArray(new FileInputStream(url.getFile())), bytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
Aggregations