Skip to content

Commit

Permalink
[SCB-2862]able to upload with List form parameters (#4235)
Browse files Browse the repository at this point in the history
  • Loading branch information
liubao68 authored Feb 18, 2024
1 parent 4e17365 commit eeacbec
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,12 @@ private boolean isPart(RequestBody parameter, String paramName) {
MediaType file = parameter.getContent().get(SwaggerConst.FILE_MEDIA_TYPE);
if (file != null) {
Schema<?> schema = (Schema<?>) file.getSchema().getProperties().get(paramName);
return schema instanceof ArraySchema ||
("string".equals(schema.getType()) && "binary".equals(schema.getFormat()));
if (schema instanceof ArraySchema) {
return "string".equals(schema.getItems().getType()) &&
"binary".equals(schema.getItems().getFormat());
} else {
return ("string".equals(schema.getType()) && "binary".equals(schema.getFormat()));
}
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static String encodeValue(Object value) throws UnsupportedEncodingException {
return URLEncoder.encode(value.toString(), StandardCharsets.UTF_8.name());
}

// can not replaced by value.toString() because of date serialize
// can not be replaced by value.toString() because of date serialize
static String convertToString(Object value) throws Exception {
return RestObjectMapperFactory.getRestObjectMapper().convertToString(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void testRestTransport() throws Exception {
testFileUploadMultiRpc();
testUploadFileAndAttribute();
testUploadFileRequestPartAttribute();
testUploadFileRequestPartAttributeList();
}

private void testServerStartupSuccess() {
Expand Down Expand Up @@ -141,4 +142,25 @@ private void testUploadFileRequestPartAttribute() throws Exception {
new HttpEntity<>(map, headers), String.class);
TestMgr.check("hi test", result);
}

private void testUploadFileRequestPartAttributeList() throws Exception {
RestOperations template = RestTemplateBuilder.create();
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
String message1 = "msg1";
String message2 = "msg2";
File file1 = File.createTempFile("file1", ".txt");
FileUtils.writeStringToFile(file1, "test1", StandardCharsets.UTF_8, false);
File file2 = File.createTempFile("file2", ".txt");
FileUtils.writeStringToFile(file2, "test2", StandardCharsets.UTF_8, false);

map.add("files", new FileSystemResource(file1));
map.add("files", new FileSystemResource(file2));
map.add("attributes", message1);
map.add("attributes", message2);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(org.springframework.http.MediaType.MULTIPART_FORM_DATA);
String result = template.postForObject("servicecomb://springmvc/upload/uploadFileRequestPartAttributeList",
new HttpEntity<>(map, headers), String.class);
TestMgr.check("test1test2msg1msg2", result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ public String uploadFileRequestPartAttribute(@RequestPart(name = "file") Multipa
}
}

@PostMapping(path = "/uploadFileRequestPartAttributeList", produces = MediaType.TEXT_PLAIN_VALUE)
public String uploadFileRequestPartAttributeList(@RequestPart(name = "files") List<MultipartFile> files,
@RequestPart(name = "attributes") List<String> attributes) throws IOException {
StringBuilder result = new StringBuilder();
for (MultipartFile file : files) {
try (InputStream is = file.getInputStream()) {
result.append(IOUtils.toString(is, StandardCharsets.UTF_8));
}
}
for (String attribute : attributes) {
result.append(attribute);
}
return result.toString();
}

@PostMapping(path = "/uploadFileAndAttribute", produces = MediaType.TEXT_PLAIN_VALUE)
public String uploadFileAndAttribute(@RequestPart(name = "file") MultipartFile file,
@RequestAttribute(name = "attribute") String attribute) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

Expand Down Expand Up @@ -185,17 +186,28 @@ protected void writeChunkedForm(Map<String, Object> formMap) throws Exception {
protected Buffer genChunkedFormBuffer(Map<String, Object> formMap, String boundary) throws Exception {
ByteBuf byteBuf = Unpooled.buffer(RestClientEncoder.FORM_BUFFER_SIZE);
for (Entry<String, Object> entry : formMap.entrySet()) {
writeCharSequence(byteBuf, "\r\n--");
writeCharSequence(byteBuf, boundary);
writeCharSequence(byteBuf, "\r\nContent-Disposition: form-data; name=\"");
writeCharSequence(byteBuf, entry.getKey());
writeCharSequence(byteBuf, "\"\r\n\r\n");

String value = QueryCodec.convertToString(entry.getValue());
writeCharSequence(byteBuf, value);
Object content = entry.getValue();
if (content instanceof List<?>) {
for (Object item : ((List<?>) content)) {
writeFormData(byteBuf, boundary, entry.getKey(), item);
}
} else {
writeFormData(byteBuf, boundary, entry.getKey(), entry.getValue());
}
}
return Buffer.buffer(byteBuf);
}

private void writeFormData(ByteBuf byteBuf, String boundary, String key, Object data) throws Exception {
writeCharSequence(byteBuf, "\r\n--");
writeCharSequence(byteBuf, boundary);
writeCharSequence(byteBuf, "\r\nContent-Disposition: form-data; name=\"");
writeCharSequence(byteBuf, key);
writeCharSequence(byteBuf, "\"\r\n\r\n");

String value = QueryCodec.convertToString(data);
writeCharSequence(byteBuf, value);
}
}

protected static void writeCharSequence(ByteBuf byteBuf, String value) {
Expand Down

0 comments on commit eeacbec

Please sign in to comment.