瀏覽代碼

文件上传接口工具

yingp 6 年之前
父節點
當前提交
353050c3a3

+ 90 - 0
base-servers/file/file-api/src/main/java/com/usoftchina/smartschool/file/api/util/ByteArrayMultipartFile.java

@@ -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);
+    }
+}

+ 35 - 0
base-servers/file/file-api/src/main/java/com/usoftchina/smartschool/file/api/util/FileUploadUtils.java

@@ -0,0 +1,35 @@
+package com.usoftchina.smartschool.file.api.util;
+
+import com.usoftchina.smartschool.base.Result;
+import com.usoftchina.smartschool.context.SpringContextHolder;
+import com.usoftchina.smartschool.exception.BizException;
+import com.usoftchina.smartschool.file.api.FileApi;
+import com.usoftchina.smartschool.file.dto.FileInfoDTO;
+
+/**
+ * @author yingp
+ * @date 2019/1/15
+ */
+public class FileUploadUtils {
+
+    /**
+     * 文件上传
+     *
+     * @param fileName
+     * @param contentType
+     * @param content
+     * @return
+     * @throws Exception
+     */
+    public static FileInfoDTO upload(String fileName, String contentType, byte[] content) throws Exception{
+        FileApi api = SpringContextHolder.getBean(FileApi.class);
+        Result<FileInfoDTO> result = api.upload(null,
+                new ByteArrayMultipartFile(fileName, contentType, content));
+        if (result.isSuccess()) {
+            return result.getData();
+        } else {
+            throw new BizException(result.getCode(), result.getMessage());
+        }
+    }
+
+}