|
|
@@ -0,0 +1,90 @@
|
|
|
+package com.usoftchina.smartschool.file.api.util;
|
|
|
+
|
|
|
+import org.springframework.lang.Nullable;
|
|
|
+import org.springframework.util.Assert;
|
|
|
+import org.springframework.util.FileCopyUtils;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author yingp
|
|
|
+ * @date 2019/1/15
|
|
|
+ */
|
|
|
+public class ByteArrayMultipartFile implements MultipartFile {
|
|
|
+ private final String name;
|
|
|
+
|
|
|
+ private String originalFilename;
|
|
|
+
|
|
|
+ @Nullable
|
|
|
+ private String contentType;
|
|
|
+
|
|
|
+ private final byte[] content;
|
|
|
+
|
|
|
+ public ByteArrayMultipartFile(String name, byte[] content) throws IOException {
|
|
|
+ this(name, "", null, content);
|
|
|
+ }
|
|
|
+
|
|
|
+ public ByteArrayMultipartFile(String name, String contentType, byte[] content) throws IOException {
|
|
|
+ this(name, "", contentType, content);
|
|
|
+ }
|
|
|
+
|
|
|
+ public ByteArrayMultipartFile(
|
|
|
+ String name, @Nullable String originalFilename, @Nullable String contentType, @Nullable byte[] content) {
|
|
|
+ Assert.hasLength(name, "Name must not be null");
|
|
|
+ this.name = name;
|
|
|
+ this.originalFilename = (originalFilename != null ? originalFilename : "");
|
|
|
+ this.contentType = contentType;
|
|
|
+ this.content = (content != null ? content : new byte[0]);
|
|
|
+ }
|
|
|
+
|
|
|
+ public ByteArrayMultipartFile(
|
|
|
+ String name, @Nullable String originalFilename, @Nullable String contentType, InputStream contentStream)
|
|
|
+ throws IOException {
|
|
|
+ this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getName() {
|
|
|
+ return this.name;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getOriginalFilename() {
|
|
|
+ return this.originalFilename;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Nullable
|
|
|
+ public String getContentType() {
|
|
|
+ return this.contentType;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean isEmpty() {
|
|
|
+ return this.content.length == 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public long getSize() {
|
|
|
+ return this.content.length;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public byte[] getBytes() throws IOException {
|
|
|
+ return this.content;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public InputStream getInputStream() throws IOException {
|
|
|
+ return new ByteArrayInputStream(this.content);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void transferTo(File dest) throws IOException, IllegalStateException {
|
|
|
+ FileCopyUtils.copy(this.content, dest);
|
|
|
+ }
|
|
|
+}
|