Эх сурвалжийг харах

feat(fileupload):将文件上传改为minio

wangyc 7 жил өмнө
parent
commit
872917001b

+ 5 - 0
pom.xml

@@ -520,6 +520,11 @@
 				</exclusion>
 			</exclusions>
 		</dependency>
+		<dependency>
+			<groupId>io.minio</groupId>
+			<artifactId>minio</artifactId>
+			<version>3.0.6</version>
+		</dependency>
 	</dependencies>
 	<build>
 		<finalName>platform-b2c</finalName>

+ 10 - 8
src/main/java/com/uas/platform/b2c/common/base/service/impl/DFSImageServiceImpl.java

@@ -5,15 +5,17 @@ import com.uas.platform.b2c.common.base.dao.DBPictureRepository;
 import com.uas.platform.b2c.common.base.model.DBPicture;
 import com.uas.platform.b2c.common.base.model.IPicture;
 import com.uas.platform.b2c.common.base.service.ImageService;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.FilenameUtils;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.List;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.FilenameUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
 
 @Service
 public class DFSImageServiceImpl implements ImageService {
@@ -30,7 +32,7 @@ public class DFSImageServiceImpl implements ImageService {
         for (File file : files) {
             try {
                 String fileUrl = fileClient.upload(FileUtils.readFileToByteArray(file), file.length(),
-                        FilenameUtils.getExtension(file.getName()), null);
+                       file.getName(), null);
                 pictures.add(new DBPicture(file.getName(), fileUrl, com.uas.platform.core.util.FileUtils.getImagePixel(file)));
             } catch (Exception e) {
                 e.printStackTrace();
@@ -43,7 +45,7 @@ public class DFSImageServiceImpl implements ImageService {
     public DBPicture save(File file) {
         try {
             String fileUrl = fileClient.upload(FileUtils.readFileToByteArray(file), file.length(),
-                    FilenameUtils.getExtension(file.getName()), null);
+                file.getName(), null);
             return pictureRepository.save(new DBPicture(file.getName(), fileUrl, com.uas.platform.core.util.FileUtils.getImagePixel(file)));
         } catch (IOException e) {
             e.printStackTrace();
@@ -53,7 +55,7 @@ public class DFSImageServiceImpl implements ImageService {
 
     @Override
     public IPicture save(String fileName, byte[] fileBytes) {
-        String fileUrl = fileClient.upload(fileBytes, fileBytes.length, FilenameUtils.getExtension(fileName), null);
+        String fileUrl = fileClient.upload(fileBytes, fileBytes.length, fileName, null);
         return pictureRepository.save(new DBPicture(fileName, fileUrl, com.uas.platform.core.util.FileUtils.getImagePixel(fileBytes)));
     }
 

+ 46 - 36
src/main/java/com/uas/platform/b2c/common/base/service/impl/FileClientImpl.java

@@ -1,26 +1,25 @@
 package com.uas.platform.b2c.common.base.service.impl;
 
-import com.alibaba.fastjson.JSONObject;
 import com.uas.dfs.domain.FileInfo;
 import com.uas.dfs.domain.MetaData;
 import com.uas.dfs.service.FileClient;
 import com.uas.platform.b2c.common.base.constant.FileClientConstant;
 import com.uas.platform.b2c.core.config.SysConf;
+import com.uas.ps.core.page.exception.IllegalOperatorException;
+import java.io.ByteArrayInputStream;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.core.io.ByteArrayResource;
-import org.springframework.http.*;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
-import org.springframework.util.LinkedMultiValueMap;
-import org.springframework.util.MultiValueMap;
 import org.springframework.web.client.RestTemplate;
 
-import java.io.*;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Set;
-
 @Service
 public class FileClientImpl implements FileClient {
 
@@ -30,44 +29,55 @@ public class FileClientImpl implements FileClient {
 
     private final Logger logger = LoggerFactory.getLogger(FileClientImpl.class);
 
+    @Autowired
+    private MinioClientImpl minioClient;
+
     @Autowired
     public FileClientImpl(RestTemplate restTemplate, SysConf sysConf) {
         this.restTemplate = restTemplate;
         this.sysConf = sysConf;
     }
 
+
     @Override
     public String upload(byte[] bytes, long l, final String s, Set<MetaData> set) {
-        HttpHeaders headers = new HttpHeaders();
-        MediaType type = MediaType.parseMediaType("multipart/form-data;charset=UTF-8");
-        headers.setContentType(type);
-
-        File file = new File(s);
         try {
-            OutputStream output = new FileOutputStream(file);
-            BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);
-            bufferedOutput.write(bytes);
-        } catch (IOException e) {
+            return minioClient.upload(new ByteArrayInputStream(bytes), Integer.valueOf(String.valueOf(l)), s);
+        } catch (Exception e) {
             e.printStackTrace();
+            throw new IllegalOperatorException("附件上传失败");
         }
 
-        MultiValueMap<String, Object> form = new LinkedMultiValueMap<String, Object>();
-        ByteArrayResource arrayResource = new ByteArrayResource(bytes){
-            @Override
-            public String getFilename() throws IllegalStateException {
-                return "test." + s;
-            }
-
-        };
-        form.add("file", arrayResource);
-        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(form, headers);
-        ResponseEntity<JSONObject> responseEntity = null;
-        try {
-            responseEntity = restTemplate.postForEntity(sysConf.getUploadFileUrl() + FileClientConstant.FILE_UPLOAD, requestEntity, JSONObject.class);
-        }catch (Exception e) {
-            e.printStackTrace();
-        }
-        return responseEntity.getBody().getString("path");
+//        HttpHeaders headers = new HttpHeaders();
+//        MediaType type = MediaType.parseMediaType("multipart/form-data;charset=UTF-8");
+//        headers.setContentType(type);
+//
+//        File file = new File(s);
+//        try {
+//            OutputStream output = new FileOutputStream(file);
+//            BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);
+//            bufferedOutput.write(bytes);
+//        } catch (IOException e) {
+//            e.printStackTrace();
+//        }
+//
+//        MultiValueMap<String, Object> form = new LinkedMultiValueMap<String, Object>();
+//        ByteArrayResource arrayResource = new ByteArrayResource(bytes){
+//            @Override
+//            public String getFilename() throws IllegalStateException {
+//                return "test." + s;
+//            }
+//
+//        };
+//        form.add("file", arrayResource);
+//        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(form, headers);
+//        ResponseEntity<JSONObject> responseEntity = null;
+//        try {
+//            responseEntity = restTemplate.postForEntity(sysConf.getUploadFileUrl() + FileClientConstant.FILE_UPLOAD, requestEntity, JSONObject.class);
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//        }
+//        return responseEntity.getBody().getString("path");
     }
 
     @Override

+ 2 - 2
src/main/java/com/uas/platform/b2c/common/base/service/impl/FileServiceImpl.java

@@ -21,7 +21,7 @@ public class FileServiceImpl implements FileService {
 		try {
 			// 上传到文件系统
 			return fileClient.upload(file.getBytes(), file.getSize(),
-					FilenameUtils.getExtension(file.getOriginalFilename()), null);
+				file.getOriginalFilename(), null);
 		} catch (Exception e) {
 			e.printStackTrace();
 			throw new IllegalOperatorException("附件上传失败");
@@ -31,7 +31,7 @@ public class FileServiceImpl implements FileService {
 	@Override
 	public String save(String fileName, byte[] fileBytes) {
 		try {
-            return fileClient.upload(fileBytes, fileBytes.length, FilenameUtils.getExtension(fileName), null);
+            return fileClient.upload(fileBytes, fileBytes.length, fileName, null);
 		} catch (Exception e) {
 			e.printStackTrace();
 			throw new IllegalOperatorException("附件上传失败");

+ 68 - 0
src/main/java/com/uas/platform/b2c/common/base/service/impl/MinioClientImpl.java

@@ -0,0 +1,68 @@
+package com.uas.platform.b2c.common.base.service.impl;
+
+import com.alibaba.fastjson.JSONObject;
+import com.uas.platform.b2c.core.config.MinioConfig;
+import com.uas.platform.b2c.core.utils.FastjsonUtils;
+import io.minio.MinioClient;
+import java.io.InputStream;
+import java.util.UUID;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.util.StringUtils;
+
+/**
+ * @author wangyc
+ * @date 2018-10-23 11:14
+ */
+@Service
+public class MinioClientImpl {
+
+    @Autowired
+    private MinioClient minioClient;
+
+    @Autowired
+    private MinioConfig minioConfig;
+
+    // 默认MIME
+    private static final String DEFAULT_MIME = "application/octet-stream";
+
+    // 常见MIME
+    private static final String COMMON_MIME = "{\"bmp\":\"image/bmp\",\"gif\":\"image/gif\",\"ico\":\"image/x-icon\",\"jpe\":\"image/jpeg\",\"jpeg\":\"image/jpeg\",\"jpg\":\"image/jpeg\",\"png\":\"image/png\",\"svg\":\"image/svg+xml\",\"3gp\":\"video/3gpp\",\"avi\":\"video/x-msvideo\",\"flv\":\"video/x-flv\",\"mov\":\"video/quicktime\",\"movie\":\"video/x-sgi-movie\",\"mp4\":\"video/mp4\",\"mpe\":\"video/mpeg\",\"mpeg\":\"video/mpeg\",\"mpg\":\"video/mpeg\",\"webm\":\"video/webm\",\"kar\":\"audio/midi\",\"mid\":\"audio/midi\",\"midi\":\"audio/midi\",\"mp2\":\"audio/mpeg\",\"mp3\":\"audio/mpeg\",\"mpga\":\"audio/mpeg\",\"wav\":\"audio/x-wav\",\"asc\":\"text/plain\",\"css\":\"text/css\",\"htm\":\"text/html\",\"html\":\"text/html\",\"js\":\"text/javascript\",\"doc\":\"application/msword\",\"ogg\":\"application/ogg\",\"pdf\":\"application/pdf\",\"ppt\":\"application/vnd.ms-powerpoint\",\"vxml\":\"application/voicexml+xml\",\"wbxml\":\"application/vnd.wap.wbxml\",\"xht\":\"application/xhtml+xml\",\"xhtml\":\"application/xhtml+xml\",\"xls\":\"application/vnd.ms-excel\",\"xml\":\"application/xml\",\"xsl\":\"application/xml\"}";
+
+    /**
+     * 文件上传
+     * @param inputStream 文件流
+     * @param size 文件大小
+     * @param name 文件名
+     * @return
+     * @throws Exception Exception
+     */
+    public String upload(InputStream inputStream, int size, String name) throws Exception {
+        String extension = name.lastIndexOf(".") == -1 ? "" : name.substring(name.lastIndexOf(".") + 1);
+        JSONObject mime = FastjsonUtils.parseObject(COMMON_MIME);
+        String contentType = DEFAULT_MIME;
+        String objectName = UUID.randomUUID().toString();
+        if (!StringUtils.isEmpty(extension) && !StringUtils.isEmpty(mime.get(extension))) {
+            objectName = String.format("%s.%s", objectName, extension);
+            contentType = mime.get(extension).toString();
+        }
+
+        String url = uploadFile(inputStream, minioConfig.getBuketName(), objectName, size, contentType);
+        return String.format("%s%s", minioConfig.getStaticUrl(), url);
+    }
+
+    /**
+     * 文件上传
+     * @param stream 文件流
+     * @param buketName 存储桶名称
+     * @param objectName 对象名称
+     * @param size 文件大小
+     * @param contentType 文件格式
+     * @exception Exception Exception
+     * @return
+     */
+    public String uploadFile(InputStream stream, String buketName, String objectName, int size, String contentType) throws Exception{
+        minioClient.putObject(buketName, objectName, stream, size, contentType);
+        return String.format("/%s/%s", buketName, objectName);
+    }
+}

+ 88 - 0
src/main/java/com/uas/platform/b2c/core/config/MinioConfig.java

@@ -0,0 +1,88 @@
+package com.uas.platform.b2c.core.config;
+
+import io.minio.MinioClient;
+import io.minio.errors.MinioException;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Primary;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author wangyc
+ * @date 2018-10-23 11:20
+ */
+@Component
+public class MinioConfig {
+
+    @Value("#{minio.url}")
+    private String url;
+
+    @Value("#{minio.accessKey}")
+    private String accessKey;
+
+    @Value("#{minio.secretKey}")
+    private String secretKey;
+
+    @Value("#{minio.staticUrl}")
+    private String staticUrl;
+
+    @Value("#{minio.buketName}")
+    private String buketName;
+
+    /**
+     * 生成MinioClient
+     * @return
+     */
+    @Bean
+    @Primary
+    public MinioClient minioClient() {
+        try {
+            MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
+            return minioClient;
+        } catch (MinioException e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+    public String getUrl() {
+        return url;
+    }
+
+    public void setUrl(String url) {
+        this.url = url;
+    }
+
+    public String getAccessKey() {
+        return accessKey;
+    }
+
+    public void setAccessKey(String accessKey) {
+        this.accessKey = accessKey;
+    }
+
+    public String getSecretKey() {
+        return secretKey;
+    }
+
+    public void setSecretKey(String secretKey) {
+        this.secretKey = secretKey;
+    }
+
+    public String getStaticUrl() {
+        return staticUrl;
+    }
+
+    public void setStaticUrl(String staticUrl) {
+        this.staticUrl = staticUrl;
+    }
+
+    public String getBuketName() {
+        return buketName;
+    }
+
+    public void setBuketName(String buketName) {
+        this.buketName = buketName;
+    }
+}

+ 1 - 0
src/main/java/com/uas/platform/b2c/core/config/SysConf.java

@@ -1,6 +1,7 @@
 package com.uas.platform.b2c.core.config;
 
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.stereotype.Component;
 
 /**

+ 6 - 0
src/main/resources/dev/minio.properties

@@ -0,0 +1,6 @@
+# minio upload file
+url=http://uuzcc.cn:9000
+accessKey=WAB5ARL2SOS1D3ZRL8VA
+secretKey=nsstQiQMdyDqpQKogDG9oa2RHUpFGMD4exNPyc8Y
+staticUrl=https://static.uuzcc.cn
+buketName=b2ctest

+ 6 - 0
src/main/resources/pre/minio.properties

@@ -0,0 +1,6 @@
+# minio upload file
+url=http://uuzcc.cn:9000
+accessKey=WAB5ARL2SOS1D3ZRL8VA
+secretKey=nsstQiQMdyDqpQKogDG9oa2RHUpFGMD4exNPyc8Y
+staticUrl=https://static.uuzcc.cn
+buketName=b2ctest

+ 6 - 0
src/main/resources/prod/minio.properties

@@ -0,0 +1,6 @@
+# minio upload file
+url=http://uuzcc.cn:9000
+accessKey=WAB5ARL2SOS1D3ZRL8VA
+secretKey=nsstQiQMdyDqpQKogDG9oa2RHUpFGMD4exNPyc8Y
+staticUrl=https://static.uuzcc.cn
+buketName=b2ctest

+ 3 - 0
src/main/resources/spring/context.xml

@@ -26,6 +26,9 @@
 	<!-- 消息参数 -->
 	<util:properties id="message"
 					 location="classpath:${profile}/message.properties" />
+	<!-- Minio参数 -->
+	<util:properties id="minio"
+		location="classpath:${profile}/minio.properties" />
 	<!-- 注册spring上下文对象 -->
 	<bean class="com.uas.platform.b2c.core.support.ApplicationContextRegister" />
 	<!-- 容器启动完成之后执行 -->

+ 6 - 0
src/main/resources/test/minio.properties

@@ -0,0 +1,6 @@
+# minio upload file
+url=http://uuzcc.cn:9000
+accessKey=WAB5ARL2SOS1D3ZRL8VA
+secretKey=nsstQiQMdyDqpQKogDG9oa2RHUpFGMD4exNPyc8Y
+staticUrl=https://static.uuzcc.cn
+buketName=b2ctest

+ 6 - 0
src/main/resources/txcloud/minio.properties

@@ -0,0 +1,6 @@
+# minio upload file
+url=http://uuzcc.cn:9000
+accessKey=WAB5ARL2SOS1D3ZRL8VA
+secretKey=nsstQiQMdyDqpQKogDG9oa2RHUpFGMD4exNPyc8Y
+staticUrl=https://static.uuzcc.cn
+buketName=b2ctest