Search in sources :

Example 1 with InputStreamMultipart

use of com.dtflys.forest.multipart.InputStreamMultipart in project forest by dromara.

the class TestUploadClient method testUploadInputStream.

@Test
public void testUploadInputStream() throws IOException, InterruptedException, FileUploadException {
    server.enqueue(new MockResponse().setBody(EXPECTED));
    String path = this.getClass().getResource("/test-img.jpg").getPath();
    if (path.startsWith("/") && isWindows()) {
        path = path.substring(1);
    }
    File file = new File(path);
    InputStream in = new FileInputStream(file);
    ForestRequest<Map> request = uploadClient.upload(in, "test-byte-array.jpg");
    assertNotNull(request);
    assertTrue(StringUtils.isNotEmpty(request.getBoundary()));
    List<ForestMultipart> multipartList = request.getMultiparts();
    assertEquals(1, multipartList.size());
    ForestMultipart multipart = multipartList.get(0);
    // assertTrue(Map.class.isAssignableFrom(request.getMethod().getReturnClass()));
    assertTrue(multipart instanceof InputStreamMultipart);
    assertEquals("file", multipart.getName());
    assertEquals("test-byte-array.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-byte-array.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);
        }
    });
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ForestMultipart(com.dtflys.forest.multipart.ForestMultipart) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FileItem(org.apache.commons.fileupload.FileItem) InputStreamMultipart(com.dtflys.forest.multipart.InputStreamMultipart) File(java.io.File) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with InputStreamMultipart

use of com.dtflys.forest.multipart.InputStreamMultipart in project forest by dromara.

the class TestMultipart method testInputStreamMultipart.

public void testInputStreamMultipart() throws IOException {
    URL url = TestMultipart.class.getClassLoader().getResource("log4j2.xml");
    String filePath = url.getFile();
    File file = new File(filePath);
    InputStream in = new FileInputStream(file);
    InputStreamMultipart inputStreamMultipart = new InputStreamMultipart();
    inputStreamMultipart.setName("file");
    inputStreamMultipart.setData(in);
    File resultFile = inputStreamMultipart.getFile();
    inputStreamMultipart.setFileName("log4j2.xml");
    assertNull(resultFile);
    assertEquals("file", inputStreamMultipart.getName());
    assertEquals("log4j2.xml", inputStreamMultipart.getOriginalFileName());
    assertEquals(-1, inputStreamMultipart.getSize());
    String text = FileUtils.readFileToString(file);
    String resultText = IOUtils.toString(inputStreamMultipart.getInputStream());
    assertEquals(text, resultText);
}
Also used : InputStreamMultipart(com.dtflys.forest.multipart.InputStreamMultipart) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) URL(java.net.URL) FileInputStream(java.io.FileInputStream)

Example 3 with InputStreamMultipart

use of com.dtflys.forest.multipart.InputStreamMultipart in project forest by dromara.

the class TestMultipart method testInputStreamMultipart2.

public void testInputStreamMultipart2() throws IOException {
    URL url = TestMultipart.class.getClassLoader().getResource("log4j2.xml");
    String filePath = url.getFile();
    File file = new File(filePath);
    InputStream in = new FileInputStream(file);
    InputStreamMultipart inputStreamMultipart = new InputStreamMultipart();
    inputStreamMultipart.setData(in);
    File resultFile = inputStreamMultipart.getFile();
    inputStreamMultipart.setFileName("log4j2.xml");
    assertNull(resultFile);
    assertEquals("log4j2.xml", inputStreamMultipart.getOriginalFileName());
    assertEquals(-1, inputStreamMultipart.getSize());
    String text = FileUtils.readFileToString(file);
    byte[] byteArray = inputStreamMultipart.getBytes();
    assertNotNull(byteArray);
    String resultText2 = new String(byteArray);
    assertEquals(text, resultText2);
}
Also used : InputStreamMultipart(com.dtflys.forest.multipart.InputStreamMultipart) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) URL(java.net.URL) FileInputStream(java.io.FileInputStream)

Aggregations

InputStreamMultipart (com.dtflys.forest.multipart.InputStreamMultipart)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 URL (java.net.URL)2 ForestMultipart (com.dtflys.forest.multipart.ForestMultipart)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 FileItem (org.apache.commons.fileupload.FileItem)1 Test (org.junit.Test)1