lijun před 5 roky
rodič
revize
117094a8a0

+ 88 - 5
src/main/java/com/caimei/utils/ImageUploadUtils.java

@@ -2,16 +2,26 @@ package com.caimei.utils;
 
 import com.caimei.dfs.image.beens.ImageSize;
 import com.caimei.dfs.image.beens.ImageUploadInfo;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Value;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.InputStream;
+import org.springframework.stereotype.Controller;
+import org.springframework.util.ClassUtils;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+import sun.misc.BASE64Decoder;
+import sun.misc.BASE64Encoder;
+
+import java.io.*;
 import java.util.ArrayList;
 import java.util.Hashtable;
 import java.util.List;
+import java.util.Random;
 
+@Controller
+@RequestMapping(value = "/mall/uploadFile")
 public class ImageUploadUtils{
+	protected static final Logger logger = LoggerFactory.getLogger(ImageUploadUtils.class);
 
 	@Value("${malladmin.imageDomain}")
 	private String imageDomain;
@@ -46,10 +56,83 @@ public class ImageUploadUtils{
 		}
 	}
 
-	public String imageUpload() throws Exception {
+	public String uploadFile() throws Exception {
 		return imageUpload2FastDFS();
 	}
 
+	@ResponseBody
+	@RequestMapping(value = "/imageUpload")
+	public JsonModel imageUpload(String imgStr) throws Exception {
+		String s = GenerateImage(imgStr);
+		return JsonModel.newInstance().success(s);
+	}
+
+	/**
+	 * 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
+	 * @return
+	 */
+	public  String GetImageStr() {
+		String imgFile = "D:\\360CloudUI\\tupian\\jt.jpg";//待处理的图片
+		InputStream in = null;
+		byte[] data = null;
+		//读取图片字节数组
+		try {
+			in = new FileInputStream(imgFile);
+			data = new byte[in.available()];
+			in.read(data);
+			in.close();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		//对字节数组Base64编码
+		BASE64Encoder encoder = new BASE64Encoder();
+		return encoder.encode(data);//返回Base64编码过的字节数组字符串
+	}
+
+	/**
+	 * 对字节数组字符串进行Base64解码并生成图片
+	 * @param imgStr
+	 * @return
+	 */
+	public String GenerateImage(String imgStr) {
+		if (imgStr == null) {//图像数据为空
+			return "error";
+		}
+		BASE64Decoder decoder = new BASE64Decoder();
+		try {
+			//Base64解码
+			byte[] b = decoder.decodeBuffer(imgStr);
+			for (int i = 0; i < b.length; ++i) {
+				if (b[i] < 0) {//调整异常数据
+					b[i] += 256;
+				}
+			}
+			//生成jpeg图片
+			System.out.println("生成jpeg图");
+			String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
+			String file = new Random().nextInt(99999999) + ".jpg";
+
+			String filePath = path + "static/photo/" + file;
+			//新生成的图片
+			OutputStream out = new FileOutputStream(filePath);
+			out.write(b);
+			out.flush();
+			out.close();
+			File file2 = new File(filePath);
+//			StorePath storePath = fastFileStorageClient.uploadFile(null, new FileInputStream(file2), file2.length(), "png");
+			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;
+		} catch (Exception e) {
+			logger.info("上传图片异常!");
+			return null;
+		}
+	}
+
 	private String imageUpload2FastDFS() {
 		String message;
 		jsonResult = new Hashtable<String, String>();