parent
ab6a6f4e17
commit
f7d60b7f58
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -21,6 +21,7 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
|
||||
import org.springframework.core.io.InputStreamSource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
|
@ -93,6 +94,17 @@ public interface MultipartFile extends InputStreamSource {
|
|||
@Override
|
||||
InputStream getInputStream() throws IOException;
|
||||
|
||||
/**
|
||||
* Return a Resource representation of this MultipartFile. This can be used
|
||||
* as input to the {@code RestTemplate} or the {@code WebClient} to expose
|
||||
* content length and the filename along with the InputStream.
|
||||
* @return this MultipartFile adapted to the Resource contract
|
||||
* @since 5.1
|
||||
*/
|
||||
default Resource getResource() {
|
||||
return new MultipartFileResource(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfer the received file to the given destination file.
|
||||
* <p>This may either move the file in the filesystem, copy the file in the
|
||||
|
|
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.web.multipart;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.springframework.core.io.AbstractResource;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Adapt {@link MultipartFile} to {@link org.springframework.core.io.Resource},
|
||||
* exposing the content as {@code InputStream} and also overriding
|
||||
* {@link #contentLength()} as well as {@link #getFilename()}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.1
|
||||
*/
|
||||
class MultipartFileResource extends AbstractResource {
|
||||
|
||||
private final MultipartFile multipartFile;
|
||||
|
||||
|
||||
public MultipartFileResource(MultipartFile multipartFile) {
|
||||
Assert.notNull(multipartFile, "MultipartFile must not be null.");
|
||||
this.multipartFile = multipartFile;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This implementation always returns {@code true}.
|
||||
*/
|
||||
@Override
|
||||
public boolean exists() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* This implementation always returns {@code true}.
|
||||
*/
|
||||
@Override
|
||||
public boolean isOpen() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long contentLength() {
|
||||
return this.multipartFile.getSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFilename() {
|
||||
return this.multipartFile.getOriginalFilename();
|
||||
}
|
||||
|
||||
/**
|
||||
* This implementation throws IllegalStateException if attempting to
|
||||
* read the underlying stream multiple times.
|
||||
*/
|
||||
@Override
|
||||
public InputStream getInputStream() throws IOException, IllegalStateException {
|
||||
return this.multipartFile.getInputStream();
|
||||
}
|
||||
|
||||
/**
|
||||
* This implementation returns a description that has the Multipart name.
|
||||
*/
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "MultipartFile resource [" + this.multipartFile.getName() + "]";
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return (obj == this ||
|
||||
(obj instanceof MultipartFileResource &&
|
||||
((MultipartFileResource) obj).multipartFile.equals(this.multipartFile)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.multipartFile.hashCode();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -16,12 +16,20 @@
|
|||
|
||||
package org.springframework.web.multipart.support;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.http.MockHttpOutputMessage;
|
||||
import org.springframework.http.converter.FormHttpMessageConverter;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockPart;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
|
@ -33,8 +41,8 @@ public class StandardMultipartHttpServletRequestTests {
|
|||
|
||||
@Test
|
||||
public void filename() throws Exception {
|
||||
StandardMultipartHttpServletRequest request = getRequest(
|
||||
"file", "form-data; name=\"file\"; filename=\"myFile.txt\"");
|
||||
String disposition = "form-data; name=\"file\"; filename=\"myFile.txt\"";
|
||||
StandardMultipartHttpServletRequest request = requestWithPart("file", disposition, "");
|
||||
|
||||
MultipartFile multipartFile = request.getFile("file");
|
||||
assertNotNull(multipartFile);
|
||||
|
@ -43,8 +51,8 @@ public class StandardMultipartHttpServletRequestTests {
|
|||
|
||||
@Test // SPR-13319
|
||||
public void filenameRfc5987() throws Exception {
|
||||
StandardMultipartHttpServletRequest request = getRequest(
|
||||
"file", "form-data; name=\"file\"; filename*=\"UTF-8''foo-%c3%a4-%e2%82%ac.html\"");
|
||||
String disposition = "form-data; name=\"file\"; filename*=\"UTF-8''foo-%c3%a4-%e2%82%ac.html\"";
|
||||
StandardMultipartHttpServletRequest request = requestWithPart("file", disposition, "");
|
||||
|
||||
MultipartFile multipartFile = request.getFile("file");
|
||||
assertNotNull(multipartFile);
|
||||
|
@ -53,19 +61,42 @@ public class StandardMultipartHttpServletRequestTests {
|
|||
|
||||
@Test // SPR-15205
|
||||
public void filenameRfc2047() throws Exception {
|
||||
StandardMultipartHttpServletRequest request = getRequest(
|
||||
"file", "form-data; name=\"file\"; filename=\"=?UTF-8?Q?Declara=C3=A7=C3=A3o.pdf?=\"");
|
||||
String disposition = "form-data; name=\"file\"; filename=\"=?UTF-8?Q?Declara=C3=A7=C3=A3o.pdf?=\"";
|
||||
StandardMultipartHttpServletRequest request = requestWithPart("file", disposition, "");
|
||||
|
||||
MultipartFile multipartFile = request.getFile("file");
|
||||
assertNotNull(multipartFile);
|
||||
assertEquals("Declaração.pdf", multipartFile.getOriginalFilename());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipartFileResource() throws IOException {
|
||||
String name = "file";
|
||||
String disposition = "form-data; name=\"" + name + "\"; filename=\"myFile.txt\"";
|
||||
StandardMultipartHttpServletRequest request = requestWithPart(name, disposition, "myBody");
|
||||
MultipartFile multipartFile = request.getFile(name);
|
||||
|
||||
private StandardMultipartHttpServletRequest getRequest(String name, String dispositionValue) {
|
||||
assertNotNull(multipartFile);
|
||||
|
||||
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
|
||||
map.add(name, multipartFile.getResource());
|
||||
|
||||
MockHttpOutputMessage output = new MockHttpOutputMessage();
|
||||
new FormHttpMessageConverter().write(map, null, output);
|
||||
|
||||
assertThat(output.getBodyAsString(StandardCharsets.UTF_8), containsString(
|
||||
"Content-Disposition: form-data; name=\"file\"; filename=\"myFile.txt\"\r\n" +
|
||||
"Content-Type: text/plain\r\n" +
|
||||
"Content-Length: 6\r\n" +
|
||||
"\r\n" +
|
||||
"myBody\r\n"));
|
||||
}
|
||||
|
||||
|
||||
private StandardMultipartHttpServletRequest requestWithPart(String name, String disposition, String content) {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockPart part = new MockPart(name, null);
|
||||
part.getHeaders().set("Content-Disposition", dispositionValue);
|
||||
MockPart part = new MockPart(name, null, content.getBytes(StandardCharsets.UTF_8));
|
||||
part.getHeaders().set("Content-Disposition", disposition);
|
||||
request.addPart(part);
|
||||
return new StandardMultipartHttpServletRequest(request);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue