3 changed files with 153 additions and 11 deletions
@ -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(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue