Commit 908dcc0e by Dave Syer

Support for multivalued map of file uploads

i.e. user sends multiple files with the same parameter name. Fixes gh-870
parent 4f6b2bce
...@@ -20,6 +20,7 @@ import java.io.ByteArrayOutputStream; ...@@ -20,6 +20,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import javax.servlet.ServletInputStream; import javax.servlet.ServletInputStream;
...@@ -54,7 +55,8 @@ public class FormBodyWrapperFilter extends ZuulFilter { ...@@ -54,7 +55,8 @@ public class FormBodyWrapperFilter extends ZuulFilter {
public FormBodyWrapperFilter() { public FormBodyWrapperFilter() {
this.requestField = ReflectionUtils.findField(HttpServletRequestWrapper.class, this.requestField = ReflectionUtils.findField(HttpServletRequestWrapper.class,
"req", HttpServletRequest.class); "req", HttpServletRequest.class);
Assert.notNull(this.requestField, "HttpServletRequestWrapper.req field not found"); Assert.notNull(this.requestField,
"HttpServletRequestWrapper.req field not found");
this.requestField.setAccessible(true); this.requestField.setAccessible(true);
} }
...@@ -82,8 +84,8 @@ public class FormBodyWrapperFilter extends ZuulFilter { ...@@ -82,8 +84,8 @@ public class FormBodyWrapperFilter extends ZuulFilter {
try { try {
MediaType mediaType = MediaType.valueOf(contentType); MediaType mediaType = MediaType.valueOf(contentType);
return MediaType.APPLICATION_FORM_URLENCODED.includes(mediaType) return MediaType.APPLICATION_FORM_URLENCODED.includes(mediaType)
|| (isDispatcherServletRequest(request) && MediaType.MULTIPART_FORM_DATA || (isDispatcherServletRequest(request)
.includes(mediaType)); && MediaType.MULTIPART_FORM_DATA.includes(mediaType));
} }
catch (InvalidMediaTypeException ex) { catch (InvalidMediaTypeException ex) {
return false; return false;
...@@ -91,7 +93,8 @@ public class FormBodyWrapperFilter extends ZuulFilter { ...@@ -91,7 +93,8 @@ public class FormBodyWrapperFilter extends ZuulFilter {
} }
private boolean isDispatcherServletRequest(HttpServletRequest request) { private boolean isDispatcherServletRequest(HttpServletRequest request) {
return request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null; return request.getAttribute(
DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null;
} }
@Override @Override
...@@ -100,8 +103,8 @@ public class FormBodyWrapperFilter extends ZuulFilter { ...@@ -100,8 +103,8 @@ public class FormBodyWrapperFilter extends ZuulFilter {
HttpServletRequest request = ctx.getRequest(); HttpServletRequest request = ctx.getRequest();
FormBodyRequestWrapper wrapper = null; FormBodyRequestWrapper wrapper = null;
if (request instanceof HttpServletRequestWrapper) { if (request instanceof HttpServletRequestWrapper) {
HttpServletRequest wrapped = (HttpServletRequest) ReflectionUtils.getField( HttpServletRequest wrapped = (HttpServletRequest) ReflectionUtils
this.requestField, request); .getField(this.requestField, request);
wrapper = new FormBodyRequestWrapper(wrapped); wrapper = new FormBodyRequestWrapper(wrapped);
ReflectionUtils.setField(this.requestField, request, wrapper); ReflectionUtils.setField(this.requestField, request, wrapper);
} }
...@@ -170,16 +173,19 @@ public class FormBodyWrapperFilter extends ZuulFilter { ...@@ -170,16 +173,19 @@ public class FormBodyWrapperFilter extends ZuulFilter {
} }
if (this.request instanceof MultipartRequest) { if (this.request instanceof MultipartRequest) {
MultipartRequest multi = (MultipartRequest) this.request; MultipartRequest multi = (MultipartRequest) this.request;
for (Entry<String, MultipartFile> part : multi.getFileMap() for (Entry<String, List<MultipartFile>> parts : multi
.entrySet()) { .getMultiFileMap().entrySet()) {
MultipartFile file = part.getValue(); for (MultipartFile part : parts.getValue()) {
HttpHeaders headers = new HttpHeaders(); MultipartFile file = part;
headers.setContentDispositionFormData(file.getName(), HttpHeaders headers = new HttpHeaders();
file.getOriginalFilename()); headers.setContentDispositionFormData(file.getName(),
headers.setContentType(MediaType.valueOf(file.getContentType())); file.getOriginalFilename());
HttpEntity<byte[]> entity = new HttpEntity<byte[]>( headers.setContentType(
file.getBytes(), headers); MediaType.valueOf(file.getContentType()));
builder.set(part.getKey(), entity); HttpEntity<byte[]> entity = new HttpEntity<byte[]>(
file.getBytes(), headers);
builder.add(parts.getKey(), entity);
}
} }
} }
FormHttpOutputMessage data = new FormHttpOutputMessage(); FormHttpOutputMessage data = new FormHttpOutputMessage();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment