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