|
@@ -1,6 +1,8 @@
|
|
|
package com.usoftchina.smartschool.device.po;
|
|
package com.usoftchina.smartschool.device.po;
|
|
|
|
|
|
|
|
import org.springframework.lang.Nullable;
|
|
import org.springframework.lang.Nullable;
|
|
|
|
|
+import org.springframework.util.Assert;
|
|
|
|
|
+import org.springframework.util.FileCopyUtils;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
import java.io.*;
|
|
@@ -9,55 +11,101 @@ import java.io.*;
|
|
|
* @author: guq
|
|
* @author: guq
|
|
|
* @create: 2019-03-08 16:42
|
|
* @create: 2019-03-08 16:42
|
|
|
**/
|
|
**/
|
|
|
-public class ImageFile implements MultipartFile{
|
|
|
|
|
|
|
+public class ImageFile implements MultipartFile {
|
|
|
|
|
|
|
|
- private final byte[] imgContent;
|
|
|
|
|
- private final String header;
|
|
|
|
|
|
|
+ private final String name;
|
|
|
|
|
|
|
|
- public ImageFile(byte[] imgContent, String header) {
|
|
|
|
|
- this.imgContent = imgContent;
|
|
|
|
|
- this.header = header.split(";")[0];
|
|
|
|
|
|
|
+ private String originalFilename;
|
|
|
|
|
+
|
|
|
|
|
+ @Nullable
|
|
|
|
|
+ private String contentType;
|
|
|
|
|
+
|
|
|
|
|
+ private final byte[] content;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Create a new MockMultipartFile with the given content.
|
|
|
|
|
+ * @param name the name of the file
|
|
|
|
|
+ * @param content the content of the file
|
|
|
|
|
+ */
|
|
|
|
|
+ public ImageFile(String name, @Nullable byte[] content) {
|
|
|
|
|
+ this(name, "", null, content);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Create a new MockMultipartFile with the given content.
|
|
|
|
|
+ * @param name the name of the file
|
|
|
|
|
+ * @param contentStream the content of the file as stream
|
|
|
|
|
+ * @throws IOException if reading from the stream failed
|
|
|
|
|
+ */
|
|
|
|
|
+ public ImageFile(String name, InputStream contentStream) throws IOException {
|
|
|
|
|
+ this(name, "", null, FileCopyUtils.copyToByteArray(contentStream));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Create a new MockMultipartFile with the given content.
|
|
|
|
|
+ * @param name the name of the file
|
|
|
|
|
+ * @param originalFilename the original filename (as on the client's machine)
|
|
|
|
|
+ * @param contentType the content type (if known)
|
|
|
|
|
+ * @param content the content of the file
|
|
|
|
|
+ */
|
|
|
|
|
+ public ImageFile(
|
|
|
|
|
+ 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]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Create a new MockMultipartFile with the given content.
|
|
|
|
|
+ * @param name the name of the file
|
|
|
|
|
+ * @param originalFilename the original filename (as on the client's machine)
|
|
|
|
|
+ * @param contentType the content type (if known)
|
|
|
|
|
+ * @param contentStream the content of the file as stream
|
|
|
|
|
+ * @throws IOException if reading from the stream failed
|
|
|
|
|
+ */
|
|
|
|
|
+ public ImageFile(
|
|
|
|
|
+ String name, @Nullable String originalFilename, @Nullable String contentType, InputStream contentStream)
|
|
|
|
|
+ throws IOException {
|
|
|
|
|
+
|
|
|
|
|
+ this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
|
|
|
+
|
|
|
public String getName() {
|
|
public String getName() {
|
|
|
- return System.currentTimeMillis() + Math.random() + "." + header.split("/")[1];
|
|
|
|
|
|
|
+ return this.name;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @Nullable
|
|
|
|
|
- @Override
|
|
|
|
|
public String getOriginalFilename() {
|
|
public String getOriginalFilename() {
|
|
|
- return System.currentTimeMillis() + (int)Math.random() * 10000 + "." + header.split("/")[1];
|
|
|
|
|
|
|
+ return this.originalFilename;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
@Nullable
|
|
|
- @Override
|
|
|
|
|
public String getContentType() {
|
|
public String getContentType() {
|
|
|
- return header.split(":")[1];
|
|
|
|
|
|
|
+ return this.contentType;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
|
public boolean isEmpty() {
|
|
public boolean isEmpty() {
|
|
|
- return imgContent == null || imgContent.length == 0;
|
|
|
|
|
|
|
+ return (this.content.length == 0);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
|
public long getSize() {
|
|
public long getSize() {
|
|
|
- return imgContent.length;
|
|
|
|
|
|
|
+ return this.content.length;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
|
public byte[] getBytes() throws IOException {
|
|
public byte[] getBytes() throws IOException {
|
|
|
- return imgContent;
|
|
|
|
|
|
|
+ return this.content;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
|
public InputStream getInputStream() throws IOException {
|
|
public InputStream getInputStream() throws IOException {
|
|
|
- return new ByteArrayInputStream(imgContent);
|
|
|
|
|
|
|
+ return new ByteArrayInputStream(this.content);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
|
public void transferTo(File dest) throws IOException, IllegalStateException {
|
|
public void transferTo(File dest) throws IOException, IllegalStateException {
|
|
|
- new FileOutputStream(dest).write(imgContent);
|
|
|
|
|
|
|
+ FileCopyUtils.copy(this.content, dest);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
}
|
|
}
|