BAEL-3559: Added purpose-built endpoint for testing multipart uploads with HttpClient.

This commit is contained in:
Justin Albano 2019-12-05 09:42:34 -05:00
parent 592d3e27d7
commit 8bdf6f8d34
2 changed files with 38 additions and 5 deletions

View File

@ -34,7 +34,7 @@ public class HttpClientMultipartLiveTest {
// No longer available
// private static final String SERVER = "http://echo.200please.com";
private static final String SERVER = "http://posttestserver.com/post.php";
private static final String SERVER = "http://localhost:8080/spring-mvc-java/stub/multipart";
private static final String TEXTFILENAME = "temp.txt";
private static final String IMAGEFILENAME = "image.jpg";
private static final String ZIPFILENAME = "zipFile.zip";
@ -84,7 +84,7 @@ public class HttpClientMultipartLiveTest {
//
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("upfile", fileBody);
builder.addPart("file", fileBody);
builder.addPart("text1", stringBody1);
builder.addPart("text2", stringBody2);
final HttpEntity entity = builder.build();
@ -112,7 +112,7 @@ public class HttpClientMultipartLiveTest {
final String message = "This is a multipart post";
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("upfile", file, ContentType.DEFAULT_BINARY, TEXTFILENAME);
builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, TEXTFILENAME);
builder.addTextBody("text", message, ContentType.DEFAULT_BINARY);
final HttpEntity entity = builder.build();
post.setEntity(entity);
@ -141,7 +141,7 @@ public class HttpClientMultipartLiveTest {
final String message = "This is a multipart post";
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("upfile", file, ContentType.DEFAULT_BINARY, IMAGEFILENAME);
builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, IMAGEFILENAME);
builder.addBinaryBody("upstream", inputStream, ContentType.create("application/zip"), ZIPFILENAME);
builder.addTextBody("text", message, ContentType.TEXT_PLAIN);
final HttpEntity entity = builder.build();
@ -165,7 +165,7 @@ public class HttpClientMultipartLiveTest {
final byte[] bytes = "binary code".getBytes();
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("upfile", bytes, ContentType.DEFAULT_BINARY, TEXTFILENAME);
builder.addBinaryBody("file", bytes, ContentType.DEFAULT_BINARY, TEXTFILENAME);
builder.addTextBody("text", message, ContentType.TEXT_PLAIN);
final HttpEntity entity = builder.build();
post.setEntity(entity);

View File

@ -0,0 +1,33 @@
package com.baeldung.web.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class MultipartFileUploadStubController {
private static final Logger logger = LoggerFactory.getLogger(MultipartFileUploadStubController.class);
@PostMapping("/stub/multipart")
@ResponseStatus(HttpStatus.OK)
public void uploadFile(MultipartFile file, String text, String text1, String text2, MultipartFile upstream) {
logger.info("Uploaded file: " + format(file));
logger.info(" - text: [" + format(text) + "]");
logger.info(" - text1: [" + format(text1) + "]");
logger.info(" - text2: [" + format(text2) + "]");
logger.info(" - upstream: [" + format(upstream) + "]");
}
private static String format(MultipartFile file) {
return file == null ? "<null>" : file.getOriginalFilename() + " (size: " + file.getSize() + " bytes)";
}
private static String format(String str) {
return str == null ? "<null>" : str;
}
}