lijun 5 лет назад
Родитель
Сommit
f92c60cca1

+ 7 - 0
pom.xml

@@ -117,6 +117,13 @@
             <version>1.0</version>
         </dependency>
 
+        <!--dfs-->
+        <dependency>
+            <groupId>com.caimei</groupId>
+            <artifactId>caimei-dfs-sdk</artifactId>
+            <version>0.0.1-SNAPSHOT</version>
+        </dependency>
+
     </dependencies>
 
     <profiles>

+ 183 - 0
src/main/java/com/caimei/utils/FileUtil.java

@@ -0,0 +1,183 @@
+package com.caimei.utils;
+
+import org.apache.commons.io.FileUtils;
+
+import javax.imageio.ImageIO;
+import javax.imageio.ImageReader;
+import javax.imageio.stream.ImageInputStream;
+import java.awt.image.BufferedImage;
+import java.io.*;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+public class FileUtil {
+	public final static Map<String, String> FILE_TYPE_MAP = new HashMap<String, String>();
+
+	static {
+		getAllFileType(); // 初始化文件类型信息
+	}
+
+	/**
+	 * Created on 2013-1-21
+	 * <p>
+	 * Discription:[getAllFileType,常见文件头信息]
+	 * 
+	 * @author:shaochangfu
+	 */
+	private static void getAllFileType() {
+		FILE_TYPE_MAP.put("jpg", "FFD8FF"); // JPEG (jpg)
+		FILE_TYPE_MAP.put("png", "89504E47");// PNG (png)
+		FILE_TYPE_MAP.put("gif", "47494638");// GIF (gif)
+		FILE_TYPE_MAP.put("bmp", "424D"); // Windows Bitmap (bmp)
+		FILE_TYPE_MAP.put("zip", "504B0304");// zip 压缩文件
+	}
+
+	public FileUtil() {
+	}
+
+	/**
+	 * 把from路径下的文件拷贝到to路径下
+	 * 
+	 * @param from
+	 * @param to
+	 * @throws Exception
+	 */
+	public static void copy(String from, String to) throws Exception {
+		int BUFF_SIZE = 100000;
+		byte[] buffer = new byte[BUFF_SIZE];
+		InputStream in = null;
+		OutputStream out = null;
+		try {
+			in = new FileInputStream(from);
+			out = new FileOutputStream(to);
+			while (true) {
+				synchronized (buffer) {
+					int amountRead = in.read(buffer);
+					if (amountRead == -1) {
+						break;
+					}
+					out.write(buffer, 0, amountRead);
+				}
+			}
+		} finally {
+			if (in != null) {
+				in.close();
+			}
+			if (out != null) {
+				out.close();
+			}
+		}
+	}
+
+	/**
+	 * 把文件移动到to路径下
+	 * 
+	 * @param file
+	 * @param toPath
+	 * @throws IOException
+	 */
+	public static void moveFile(File file, String toPath, String newName)
+			throws IOException {
+		/*
+		 * file.renameTo(new File((new StringBuilder(String.valueOf(s))).append(
+		 * File.separator).append(file.getName()).toString()));
+		 */
+		FileUtils.copyFile(
+				file,
+				new File((new StringBuilder(String.valueOf(toPath)))
+						.append(File.separator).append(newName).toString()));
+	}
+
+	/**
+	 * 按照图片命名规则生成一个不带后缀的图片名
+	 * 
+	 * @param originalFullName
+	 * @return
+	 * @throws Exception
+	 */
+	public static String getNewImageName(String originalFullName)
+			throws Exception {
+		/*
+		 * int dotIndex = originalFullName.lastIndexOf('.'); if (dotIndex == -1)
+		 * { dotIndex = 0; }
+		 */
+		String fileName = RandomCodeGenerator.generateCodeString(10);
+		return (new StringBuilder(String.valueOf(fileName))).append(
+				DateTimeUtil.getCurrentDateTime().replaceAll("-", "")
+						.replaceAll(":", "").replaceAll(" ", "")).toString();
+	}
+
+	/**
+	 * 删除path路径下的文件
+	 * 
+	 * @param path
+	 */
+	public static void deleteFile(String path) {
+		File file = new File(path);
+		if (file.exists())
+			file.delete();
+	}
+
+	/**
+	 * 得到文件的真实后缀名 (文件种类)
+	 * 
+	 * @param f
+	 * @return
+	 * @throws Exception
+	 */
+	public final static String getImageFileType(File f)
+			throws Exception {
+		if (isImage(f)) {
+			try {
+				if (f.length() > 5242880) {
+					throw new Exception("图片过大");
+				}
+				ImageInputStream iis = ImageIO.createImageInputStream(f);
+				Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
+				if (!iter.hasNext()) {
+					return null;
+				}
+				ImageReader reader = iter.next();
+				iis.close();
+				return reader.getFormatName();
+			} catch (Exception e) {
+				e.printStackTrace();
+				throw new Exception("图片后缀名检验异常");
+			}
+		} else {
+			throw new Exception("图片后缀名检验异常");
+		}
+
+	}
+
+	/**
+	 * Created on 2013-1-21
+	 * <p>
+	 * Discription:[isImage,判断文件是否为图片]
+	 * </p>
+	 * 
+	 * @param file
+	 * @return true 是 | false 否
+	 * @author:shaochangfu
+	 */
+	public static final boolean isImage(File file) {
+		boolean flag = false;
+		try {
+			BufferedImage bufreader = ImageIO.read(file);
+			int width = bufreader.getWidth();
+			int height = bufreader.getHeight();
+			if (width == 0 || height == 0) {
+				flag = false;
+			} else {
+				flag = true;
+			}
+		} catch (IOException e) {
+			flag = false;
+		} catch (Exception e) {
+			flag = false;
+		}
+		return flag;
+	}
+
+}

+ 102 - 0
src/main/java/com/caimei/utils/ImageUploadUtils.java

@@ -0,0 +1,102 @@
+package com.caimei.utils;
+
+import com.caimei.dfs.image.beens.ImageSize;
+import com.caimei.dfs.image.beens.ImageUploadInfo;
+import org.springframework.beans.factory.annotation.Value;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Hashtable;
+import java.util.List;
+
+public class ImageUploadUtils{
+
+	@Value("${malladmin.imageDomain}")
+	private String imageDomain;
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+	private File fileMaterial;// Ajax获取图片文件,与控件type=File中的name属性一样
+	private String dirName;// Ajax获取图片相对路径名称,相应的name属性名称+FileName
+	private Hashtable<String, String> jsonResult;
+	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 imageUpload() throws Exception {
+		return imageUpload2FastDFS();
+	}
+
+	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() {
+		return dirName;
+	}
+
+	public void setDirName(String dirName) {
+		this.dirName = dirName;
+	}
+
+	public File getFileMaterial() {
+		return fileMaterial;
+	}
+
+	public void setFileMaterial(File fileMaterial) {
+		this.fileMaterial = fileMaterial;
+	}
+
+	public Hashtable<String, String> getJsonResult() {
+		return jsonResult;
+	}
+
+	public void setJsonResult(Hashtable<String, String> jsonResult) {
+		this.jsonResult = jsonResult;
+	}
+
+}

+ 2 - 2
src/main/resources/dev/application-dev.yml

@@ -1,5 +1,5 @@
 server:
-  port: 8108
+  port: 9104
 spring:
   application:
     name: caimei-mall-admin #指定服务名
@@ -43,4 +43,4 @@ logging:
 #自定义配置
 malladmin:
   domain: https://www.caimei365.com
-
+  imageDomain: https://img-b.caimei365.com

+ 2 - 1
src/main/resources/prod/application-prod.yml

@@ -1,5 +1,5 @@
 server:
-  port: 8108
+  port: 9104
 spring:
   application:
     name: caimei-mall-admin #指定服务名
@@ -45,3 +45,4 @@ logging:
 #自定义配置
 malladmin:
   domain: https://www.caimei365.com
+  imageDomain: https://img.caimei365.com

+ 3 - 2
src/main/resources/test/application-test.yml

@@ -1,5 +1,5 @@
 server:
-  port: 8108
+  port: 9104
 spring:
   application:
     name: caimei-mall-admin #指定服务名
@@ -43,4 +43,5 @@ logging:
 
 #自定义配置
 malladmin:
-  domain: https://www-b.caimei365.com
+  domain: https://www-b.caimei365.com
+  imageDomain: https://img-b.caimei365.com