Parcourir la source

修改fasedfs

lijun il y a 5 ans
Parent
commit
b9f4e68968
1 fichiers modifiés avec 152 ajouts et 147 suppressions
  1. 152 147
      src/main/java/com/caimei/utils/ImageUploadUtils.java

+ 152 - 147
src/main/java/com/caimei/utils/ImageUploadUtils.java

@@ -9,7 +9,9 @@ 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.util.StringUtils;
+import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
 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;
@@ -20,123 +22,126 @@ import java.util.Random;
 
 
 @Controller
 @Controller
 @RequestMapping(value = "/mall/uploadFile")
 @RequestMapping(value = "/mall/uploadFile")
-public class ImageUploadUtils{
-	protected static final Logger logger = LoggerFactory.getLogger(ImageUploadUtils.class);
-
-	@Value("${malladmin.imageDomain}")
-	private String imageDomain;
-
-	 @Autowired
-     private FastDFSClient client;
-
-	/**
-	 *
-	 */
-	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>();
-
-	@ResponseBody
-	@RequestMapping(value = "/imageUpload")
-	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;
-	}
-
-	/**
-	 * 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
-	 * @return
-	 */
-	public  String GetImageStr() {
-		String imgFile = "/mnt/newdatadrive/data/custom/autodeploy/client/releasecode/mai/mai@beta@2020010900000000/test.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();
-		logger.info("encoderImageStr--------------:"+encoder.encode(data));
-		return encoder.encode(data);//返回Base64编码过的字节数组字符串
-	}
-
-	/**
-	 * 对字节数组字符串进行Base64解码并生成本地图片地址
-	 * @param imgStr
-	 * @return
-	 */
-	public String GenerateImage(String imgStr) {
-		if (imgStr == null) {//图像数据为空
-			return null;
-		}
-		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图片
-			String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
-			logger.info("1--------------"+path);
-			String file = new Random().nextInt(99999999) + ".jpg";
-			String filePath = "/mnt/newdatadrive/data/custom/autodeploy/client/releasecode/mai/mai@beta@2020010900000000/tempImage/"  + file;
-			//新生成的图片
-			OutputStream out = new FileOutputStream(filePath);
-			out.write(b);
-			out.flush();
-			out.close();
-			File file2 = new File(filePath);
-			InputStream inStream = new FileInputStream(file2);
+public class ImageUploadUtils {
+    protected static final Logger logger = LoggerFactory.getLogger(ImageUploadUtils.class);
+
+    @Value("${malladmin.imageDomain}")
+    private String imageDomain;
+
+    @Autowired
+    private FastDFSClient client;
+
+    /**
+     *
+     */
+    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>();
+
+    @ResponseBody
+    @RequestMapping(value = "/imageUpload", method = RequestMethod.POST)
+    public JsonModel imageUpload(@RequestBody String imgStr) throws FileNotFoundException {
+        logger.info("--------------------imgStr:"+imgStr);
+        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;
+    }
+
+    /**
+     * 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
+     *
+     * @return
+     */
+    public String GetImageStr() {
+        String imgFile = "/mnt/newdatadrive/data/custom/autodeploy/client/releasecode/mai/mai@beta@2020010900000000/test.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();
+        logger.info("encoderImageStr--------------:" + encoder.encode(data));
+        return encoder.encode(data);//返回Base64编码过的字节数组字符串
+    }
+
+    /**
+     * 对字节数组字符串进行Base64解码并生成本地图片地址
+     *
+     * @param imgStr
+     * @return
+     */
+    public String GenerateImage(String imgStr) {
+        if (imgStr == null) {//图像数据为空
+            return null;
+        }
+        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图片
+            String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
+            logger.info("1--------------" + path);
+            String file = new Random().nextInt(99999999) + ".jpg";
+            String filePath = "/mnt/newdatadrive/data/custom/autodeploy/client/releasecode/mai/mai@beta@2020010900000000/tempImage/" + file;
+            //新生成的图片
+            OutputStream out = new FileOutputStream(filePath);
+            out.write(b);
+            out.flush();
+            out.close();
+            File file2 = new File(filePath);
+            InputStream inStream = new FileInputStream(file2);
 //			String suffix = FileUtil.getImageFileType(file2);
 //			String suffix = FileUtil.getImageFileType(file2);
 //			logger.info("3--------------"+suffix);
 //			logger.info("3--------------"+suffix);
 //			List<ImageSize> sizeList = new ArrayList<>();
 //			List<ImageSize> sizeList = new ArrayList<>();
@@ -144,35 +149,35 @@ public class ImageUploadUtils{
 //			String imageURL = imageDomain + imageUploadInfo.getSource();
 //			String imageURL = imageDomain + imageUploadInfo.getSource();
 //			String imageURL = "";
 //			String imageURL = "";
 //			logger.info("上传图片成功!"+imageURL);
 //			logger.info("上传图片成功!"+imageURL);
-			return filePath;
-		} catch (Exception e) {
-			logger.info("上传图片异常!");
-			return null;
-		}
-	}
-
-	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;
-	}
+            return filePath;
+        } catch (Exception e) {
+            logger.info("上传图片异常!");
+            return null;
+        }
+    }
+
+    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;
+    }
 
 
 }
 }