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