Skip to content

Commit eeacbec

Browse files
authored
[SCB-2862]able to upload with List form parameters (#4235)
1 parent 4e17365 commit eeacbec

File tree

5 files changed

+64
-11
lines changed

5 files changed

+64
-11
lines changed

common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/FormProcessorCreator.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,12 @@ private boolean isPart(RequestBody parameter, String paramName) {
120120
MediaType file = parameter.getContent().get(SwaggerConst.FILE_MEDIA_TYPE);
121121
if (file != null) {
122122
Schema<?> schema = (Schema<?>) file.getSchema().getProperties().get(paramName);
123-
return schema instanceof ArraySchema ||
124-
("string".equals(schema.getType()) && "binary".equals(schema.getFormat()));
123+
if (schema instanceof ArraySchema) {
124+
return "string".equals(schema.getItems().getType()) &&
125+
"binary".equals(schema.getItems().getFormat());
126+
} else {
127+
return ("string".equals(schema.getType()) && "binary".equals(schema.getFormat()));
128+
}
125129
}
126130
return false;
127131
}

common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/query/QueryCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static String encodeValue(Object value) throws UnsupportedEncodingException {
3535
return URLEncoder.encode(value.toString(), StandardCharsets.UTF_8.name());
3636
}
3737

38-
// can not replaced by value.toString() because of date serialize
38+
// can not be replaced by value.toString() because of date serialize
3939
static String convertToString(Object value) throws Exception {
4040
return RestObjectMapperFactory.getRestObjectMapper().convertToString(value);
4141
}

demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestUploadSchema.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public void testRestTransport() throws Exception {
6060
testFileUploadMultiRpc();
6161
testUploadFileAndAttribute();
6262
testUploadFileRequestPartAttribute();
63+
testUploadFileRequestPartAttributeList();
6364
}
6465

6566
private void testServerStartupSuccess() {
@@ -141,4 +142,25 @@ private void testUploadFileRequestPartAttribute() throws Exception {
141142
new HttpEntity<>(map, headers), String.class);
142143
TestMgr.check("hi test", result);
143144
}
145+
146+
private void testUploadFileRequestPartAttributeList() throws Exception {
147+
RestOperations template = RestTemplateBuilder.create();
148+
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
149+
String message1 = "msg1";
150+
String message2 = "msg2";
151+
File file1 = File.createTempFile("file1", ".txt");
152+
FileUtils.writeStringToFile(file1, "test1", StandardCharsets.UTF_8, false);
153+
File file2 = File.createTempFile("file2", ".txt");
154+
FileUtils.writeStringToFile(file2, "test2", StandardCharsets.UTF_8, false);
155+
156+
map.add("files", new FileSystemResource(file1));
157+
map.add("files", new FileSystemResource(file2));
158+
map.add("attributes", message1);
159+
map.add("attributes", message2);
160+
HttpHeaders headers = new HttpHeaders();
161+
headers.setContentType(org.springframework.http.MediaType.MULTIPART_FORM_DATA);
162+
String result = template.postForObject("servicecomb://springmvc/upload/uploadFileRequestPartAttributeList",
163+
new HttpEntity<>(map, headers), String.class);
164+
TestMgr.check("test1test2msg1msg2", result);
165+
}
144166
}

demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/UploadSchema.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ public String uploadFileRequestPartAttribute(@RequestPart(name = "file") Multipa
7878
}
7979
}
8080

81+
@PostMapping(path = "/uploadFileRequestPartAttributeList", produces = MediaType.TEXT_PLAIN_VALUE)
82+
public String uploadFileRequestPartAttributeList(@RequestPart(name = "files") List<MultipartFile> files,
83+
@RequestPart(name = "attributes") List<String> attributes) throws IOException {
84+
StringBuilder result = new StringBuilder();
85+
for (MultipartFile file : files) {
86+
try (InputStream is = file.getInputStream()) {
87+
result.append(IOUtils.toString(is, StandardCharsets.UTF_8));
88+
}
89+
}
90+
for (String attribute : attributes) {
91+
result.append(attribute);
92+
}
93+
return result.toString();
94+
}
95+
8196
@PostMapping(path = "/uploadFileAndAttribute", produces = MediaType.TEXT_PLAIN_VALUE)
8297
public String uploadFileAndAttribute(@RequestPart(name = "file") MultipartFile file,
8398
@RequestAttribute(name = "attribute") String attribute) throws IOException {

transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestClientEncoder.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.net.URLEncoder;
2525
import java.nio.charset.StandardCharsets;
26+
import java.util.List;
2627
import java.util.Map;
2728
import java.util.Map.Entry;
2829

@@ -185,17 +186,28 @@ protected void writeChunkedForm(Map<String, Object> formMap) throws Exception {
185186
protected Buffer genChunkedFormBuffer(Map<String, Object> formMap, String boundary) throws Exception {
186187
ByteBuf byteBuf = Unpooled.buffer(RestClientEncoder.FORM_BUFFER_SIZE);
187188
for (Entry<String, Object> entry : formMap.entrySet()) {
188-
writeCharSequence(byteBuf, "\r\n--");
189-
writeCharSequence(byteBuf, boundary);
190-
writeCharSequence(byteBuf, "\r\nContent-Disposition: form-data; name=\"");
191-
writeCharSequence(byteBuf, entry.getKey());
192-
writeCharSequence(byteBuf, "\"\r\n\r\n");
193-
194-
String value = QueryCodec.convertToString(entry.getValue());
195-
writeCharSequence(byteBuf, value);
189+
Object content = entry.getValue();
190+
if (content instanceof List<?>) {
191+
for (Object item : ((List<?>) content)) {
192+
writeFormData(byteBuf, boundary, entry.getKey(), item);
193+
}
194+
} else {
195+
writeFormData(byteBuf, boundary, entry.getKey(), entry.getValue());
196+
}
196197
}
197198
return Buffer.buffer(byteBuf);
198199
}
200+
201+
private void writeFormData(ByteBuf byteBuf, String boundary, String key, Object data) throws Exception {
202+
writeCharSequence(byteBuf, "\r\n--");
203+
writeCharSequence(byteBuf, boundary);
204+
writeCharSequence(byteBuf, "\r\nContent-Disposition: form-data; name=\"");
205+
writeCharSequence(byteBuf, key);
206+
writeCharSequence(byteBuf, "\"\r\n\r\n");
207+
208+
String value = QueryCodec.convertToString(data);
209+
writeCharSequence(byteBuf, value);
210+
}
199211
}
200212

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

0 commit comments

Comments
 (0)