Bladeren bron

修改配置

lijun 5 jaren geleden
bovenliggende
commit
a143c6c58e

+ 19 - 3
pom.xml

@@ -119,11 +119,27 @@
 
 
         <!--dfs-->
         <!--dfs-->
         <dependency>
         <dependency>
-            <groupId>com.caimei</groupId>
-            <artifactId>caimei-dfs-sdk</artifactId>
-            <version>0.0.1-SNAPSHOT</version>
+            <groupId>com.github.tobato</groupId>
+            <artifactId>fastdfs-client</artifactId>
+            <version>1.26.1-RELEASE</version>
         </dependency>
         </dependency>
 
 
+        <!--junit-->
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.12</version>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-test</artifactId>
+            <version>4.3.7.RELEASE</version>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-test</artifactId>
+            <version>1.5.2.RELEASE</version>
+        </dependency>
     </dependencies>
     </dependencies>
 
 
     <profiles>
     <profiles>

+ 6 - 0
src/main/java/com/caimei/CaimeiMallAdminApplication.java

@@ -1,9 +1,15 @@
 package com.caimei;
 package com.caimei;
 
 
+import com.github.tobato.fastdfs.FdfsClientConfig;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.EnableMBeanExport;
+import org.springframework.context.annotation.Import;
+import org.springframework.jmx.support.RegistrationPolicy;
 
 
 @SpringBootApplication
 @SpringBootApplication
+@Import(FdfsClientConfig.class)
+@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
 public class CaimeiMallAdminApplication {
 public class CaimeiMallAdminApplication {
 
 
     public static void main(String[] args) {
     public static void main(String[] args) {

+ 49 - 0
src/main/java/com/caimei/modules/fastDFS/FastDFSClient.java

@@ -0,0 +1,49 @@
+package com.caimei.modules.fastDFS;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import com.github.tobato.fastdfs.domain.StorePath;
+import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.FilenameUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import com.github.tobato.fastdfs.service.FastFileStorageClient;
+
+@Component
+public class FastDFSClient {
+    @Autowired
+    private FastFileStorageClient storageClient;
+
+
+    // 文件上传
+    public String uploadFile(String path) throws FileNotFoundException{
+        File file = new File(path);
+        InputStream input = new FileInputStream(file);
+        long size = FileUtils.sizeOf(file);
+        String name = file.getName();
+        String fileName = name.substring(name.lastIndexOf("/")+1);
+        StorePath storePath = storageClient.uploadFile(input,size, FilenameUtils.getExtension(fileName),null);
+        return storePath.getFullPath();
+    }
+
+    // 文件下载
+    public boolean downloadFile(String path,String downloadFilePath) throws IOException{
+        File file = new File(downloadFilePath);
+        FileOutputStream outputStream = null;
+        // fastdfs 文件读取
+        String filepath = path.substring(path.lastIndexOf("group1/")+7);
+        DownloadByteArray callback = new DownloadByteArray();
+        byte[] content = storageClient.downloadFile("group1", filepath,callback);
+        // 数据写入指定文件夹中
+        outputStream = new FileOutputStream(file);
+        outputStream.write(content);
+        return true;
+    }
+
+}

+ 65 - 72
src/main/java/com/caimei/utils/ImageUploadUtils.java

@@ -1,21 +1,21 @@
 package com.caimei.utils;
 package com.caimei.utils;
 
 
-import com.caimei.dfs.image.beens.ImageSize;
-import com.caimei.dfs.image.beens.ImageUploadInfo;
+
+import com.caimei.modules.fastDFS.FastDFSClient;
 import org.slf4j.Logger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Controller;
 import org.springframework.stereotype.Controller;
 import org.springframework.util.ClassUtils;
 import org.springframework.util.ClassUtils;
+import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.ResponseBody;
 import org.springframework.web.bind.annotation.ResponseBody;
 import sun.misc.BASE64Decoder;
 import sun.misc.BASE64Decoder;
 import sun.misc.BASE64Encoder;
 import sun.misc.BASE64Encoder;
 
 
 import java.io.*;
 import java.io.*;
-import java.util.ArrayList;
 import java.util.Hashtable;
 import java.util.Hashtable;
-import java.util.List;
 import java.util.Random;
 import java.util.Random;
 
 
 @Controller
 @Controller
@@ -25,8 +25,12 @@ public class ImageUploadUtils{
 
 
 	@Value("${malladmin.imageDomain}")
 	@Value("${malladmin.imageDomain}")
 	private String imageDomain;
 	private String imageDomain;
+
+	 @Autowired
+     private FastDFSClient client;
+
 	/**
 	/**
-	 * 
+	 *
 	 */
 	 */
 	private static final long serialVersionUID = 1L;
 	private static final long serialVersionUID = 1L;
 	private File fileMaterial;// Ajax获取图片文件,与控件type=File中的name属性一样
 	private File fileMaterial;// Ajax获取图片文件,与控件type=File中的name属性一样
@@ -34,37 +38,50 @@ public class ImageUploadUtils{
 	private Hashtable<String, String> jsonResult;
 	private Hashtable<String, String> jsonResult;
 	private static Hashtable<String, String> dirNameTableHashtable = new Hashtable<String, String>();
 	private static Hashtable<String, String> dirNameTableHashtable = new Hashtable<String, String>();
 
 
-	static {
-		String[][] modules = {
-				{ "gclub.businesslicenseimage", "clubBusinessLicenseImage" },
-				{ "club.businesslicenseimage", "clubBusinessLicenseImage" },
-				{ "businesslicenseimage", "clubBusinessLicenseImage" },
-				{ "headpic", "club" },
-				{ "club.headpic", "club" },
-				{ "serviceProvider.businessLicenseImage",
-						"serviceProviderBusinessLicenseImage" },
-				{ "serviceProvider.logo", "serviceProvider" },
-				{ "serviceProvider.companyImage", "serviceProvider" },
-				{ "shop.businessLicenseImage", "shopBusinessLicenseImage" },
-				{ "shop.logo", "shopLogo" },
-				{ "shop.certificate", "shopCert" },
-				{ "shop.banner", "shopBanner" }, { "product", "product" },
-				{ "comment", "comment" }, { "user", "user" } };
-
-		for (int i = 0; i < modules.length; i++) {
-			dirNameTableHashtable.put(modules[i][0], modules[i][1]);
-		}
-	}
-
-	public String uploadFile() throws Exception {
-		return imageUpload2FastDFS();
-	}
-
 	@ResponseBody
 	@ResponseBody
 	@RequestMapping(value = "/imageUpload")
 	@RequestMapping(value = "/imageUpload")
-	public JsonModel imageUpload(String imgStr) throws Exception {
-		String s = GenerateImage(imgStr);
-		return JsonModel.newInstance().success(s);
+	public JsonModel imageUpload(String imgStr) throws FileNotFoundException {
+		JsonModel jsonModel = JsonModel.newInstance();
+		try{
+			String filepath = GenerateImage(imgStr);//解析编码
+			String result = client.uploadFile(filepath);
+			if (!StringUtils.isEmpty(result)) {
+				result = imageDomain + "/" + result;
+				logger.info(">>>>>>>>>>>>>>>>图片上传成功:"+result);
+				jsonModel.success(result);
+			} else {
+				logger.info(">>>>>>>>>>>>>>>>图片上传失败:");
+				jsonModel.error("图片上传失败");
+				}
+			}catch (Exception e){
+				logger.info("图片上传异常:"+e.getMessage());
+				jsonModel.error("图片上传异常");
+			}
+			return jsonModel;
+		}
+
+		@ResponseBody
+		@RequestMapping(value = "/testImageUpload")
+	public JsonModel testImageUpload() throws Exception {
+		JsonModel jsonModel = JsonModel.newInstance();
+		try{
+			String imgStr = GetImageStr();
+			imgStr = GetImageStr();//获取base64文件编码
+			String filepath = GenerateImage(imgStr);//解析编码
+			String result = client.uploadFile(filepath);
+			if (!StringUtils.isEmpty(result)) {
+				result = imageDomain + "/" + result;
+				logger.info(">>>>>>>>>>>>>>>>图片上传成功:"+result);
+				jsonModel.success(result);
+			} else {
+				logger.info(">>>>>>>>>>>>>>>>图片上传失败:");
+				jsonModel.error("图片上传失败");
+			}
+		}catch (Exception e){
+			logger.info("图片上传异常:"+e.getMessage());
+			jsonModel.error();
+		}
+		return jsonModel;
 	}
 	}
 
 
 	/**
 	/**
@@ -72,7 +89,7 @@ public class ImageUploadUtils{
 	 * @return
 	 * @return
 	 */
 	 */
 	public  String GetImageStr() {
 	public  String GetImageStr() {
-		String imgFile = "D:\\360CloudUI\\tupian\\jt.jpg";//待处理的图片
+		String imgFile = "/mnt/newdatadrive/data/custom/autodeploy/client/releasecode/mai/mai@beta@2020010900000000/test.jpg";//待处理的图片
 		InputStream in = null;
 		InputStream in = null;
 		byte[] data = null;
 		byte[] data = null;
 		//读取图片字节数组
 		//读取图片字节数组
@@ -86,17 +103,18 @@ public class ImageUploadUtils{
 		}
 		}
 		//对字节数组Base64编码
 		//对字节数组Base64编码
 		BASE64Encoder encoder = new BASE64Encoder();
 		BASE64Encoder encoder = new BASE64Encoder();
+		logger.info("encoderImageStr--------------:"+encoder.encode(data));
 		return encoder.encode(data);//返回Base64编码过的字节数组字符串
 		return encoder.encode(data);//返回Base64编码过的字节数组字符串
 	}
 	}
 
 
 	/**
 	/**
-	 * 对字节数组字符串进行Base64解码并生成图片
+	 * 对字节数组字符串进行Base64解码并生成本地图片地址
 	 * @param imgStr
 	 * @param imgStr
 	 * @return
 	 * @return
 	 */
 	 */
 	public String GenerateImage(String imgStr) {
 	public String GenerateImage(String imgStr) {
 		if (imgStr == null) {//图像数据为空
 		if (imgStr == null) {//图像数据为空
-			return "error";
+			return null;
 		}
 		}
 		BASE64Decoder decoder = new BASE64Decoder();
 		BASE64Decoder decoder = new BASE64Decoder();
 		try {
 		try {
@@ -108,56 +126,31 @@ public class ImageUploadUtils{
 				}
 				}
 			}
 			}
 			//生成jpeg图片
 			//生成jpeg图片
-			System.out.println("生成jpeg图");
 			String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
 			String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
+			logger.info("1--------------"+path);
 			String file = new Random().nextInt(99999999) + ".jpg";
 			String file = new Random().nextInt(99999999) + ".jpg";
-
-			String filePath = path + "static/photo/" + file;
+			String filePath = "/mnt/newdatadrive/data/custom/autodeploy/client/releasecode/mai/mai@beta@2020010900000000/tempImage"  + file;
 			//新生成的图片
 			//新生成的图片
 			OutputStream out = new FileOutputStream(filePath);
 			OutputStream out = new FileOutputStream(filePath);
 			out.write(b);
 			out.write(b);
 			out.flush();
 			out.flush();
 			out.close();
 			out.close();
 			File file2 = new File(filePath);
 			File file2 = new File(filePath);
-//			StorePath storePath = fastFileStorageClient.uploadFile(null, new FileInputStream(file2), file2.length(), "png");
 			InputStream inStream = new FileInputStream(file2);
 			InputStream inStream = new FileInputStream(file2);
-			String suffix = FileUtil.getImageFileType(file2);
-			List<ImageSize> sizeList = new ArrayList<>();
-			ImageUploadInfo imageUploadInfo = com.caimei.dfs.image.ImageUpload.resizeUpload(inStream, suffix, sizeList, null);
-			String imageURL = imageDomain + imageUploadInfo.getSource();
-			logger.info("上传图片成功!"+imageURL);
-			return imageURL;
+//			String suffix = FileUtil.getImageFileType(file2);
+//			logger.info("3--------------"+suffix);
+//			List<ImageSize> sizeList = new ArrayList<>();
+//			ImageUploadInfo imageUploadInfo = com.caimei.dfs.image.ImageUpload.resizeUpload(inStream, suffix, sizeList, null);
+//			String imageURL = imageDomain + imageUploadInfo.getSource();
+//			String imageURL = "";
+//			logger.info("上传图片成功!"+imageURL);
+			return filePath;
 		} catch (Exception e) {
 		} catch (Exception e) {
 			logger.info("上传图片异常!");
 			logger.info("上传图片异常!");
 			return null;
 			return null;
 		}
 		}
 	}
 	}
 
 
-	private String imageUpload2FastDFS() {
-		String message;
-		jsonResult = new Hashtable<String, String>();
-		if (fileMaterial != null) {
-			try {
-				InputStream inStream = new FileInputStream(fileMaterial);
-				List<ImageSize> sizeList = new ArrayList<>();
-				// String fileName = fileMaterial.getName();
-			    String suffix = FileUtil.getImageFileType(fileMaterial);
-				ImageUploadInfo imageUploadInfo = com.caimei.dfs.image.ImageUpload.resizeUpload(inStream, suffix, sizeList, null);
-				String imageURL = imageDomain + imageUploadInfo.getSource();
-				jsonResult.put("imgUrl", imageURL);
-				message = AppKeys.SUCCESS;
-			} catch (FileNotFoundException e) {
-				message = "没有找到待上传的文件";
-			} catch (Exception e) {
-				message = "未接收到文件";
-			}
-		}else {
-			message = "未接收到文件";
-		}
-		jsonResult.put("message", message);
-		return "json";
-	}
-
 	public String getDirName() {
 	public String getDirName() {
 		return dirName;
 		return dirName;
 	}
 	}

+ 11 - 0
src/main/resources/dev/application-dev.yml

@@ -44,3 +44,14 @@ logging:
 malladmin:
 malladmin:
   domain: https://www.caimei365.com
   domain: https://www.caimei365.com
   imageDomain: https://img-b.caimei365.com
   imageDomain: https://img-b.caimei365.com
+
+#DFS配置
+fdfs:
+  so-timeout: 5000 #上传的超时时间
+  connect-timeout: 2000 #连接超时时间
+  thumb-image:             #缩略图生成参数
+    width: 150
+    height: 150
+  tracker-list:            #TrackerList参数,支持多个
+    - 119.29.0.46:22122
+

+ 10 - 0
src/main/resources/prod/application-prod.yml

@@ -46,3 +46,13 @@ logging:
 malladmin:
 malladmin:
   domain: https://www.caimei365.com
   domain: https://www.caimei365.com
   imageDomain: https://img.caimei365.com
   imageDomain: https://img.caimei365.com
+
+#DFS配置
+fdfs:
+  so-timeout: 5000 #上传的超时时间
+  connect-timeout: 2000 #连接超时时间
+  thumb-image:             #缩略图生成参数
+    width: 150
+    height: 150
+  tracker-list:            #TrackerList参数,支持多个
+    - 10.104.172.219:22122

+ 11 - 1
src/main/resources/test/application-test.yml

@@ -44,4 +44,14 @@ logging:
 #自定义配置
 #自定义配置
 malladmin:
 malladmin:
   domain: https://www-b.caimei365.com
   domain: https://www-b.caimei365.com
-  imageDomain: https://img-b.caimei365.com
+  imageDomain: https://img-b.caimei365.com
+
+#DFS配置
+fdfs:
+  so-timeout: 5000 #上传的超时时间
+  connect-timeout: 2000 #连接超时时间
+  thumb-image:             #缩略图生成参数
+    width: 150
+    height: 150
+  tracker-list:            #TrackerList参数,支持多个
+    - 10.104.50.235:22122