diff --git a/src/main/java/com/we/controller/PhotographController.java b/src/main/java/com/we/controller/PhotographController.java index db5dbf2c3ea9000ad5b20f17b92e82b7bca7550b..c6bb305fcfd46d8f64b66b634bc1c62e7e7c90fa 100644 --- a/src/main/java/com/we/controller/PhotographController.java +++ b/src/main/java/com/we/controller/PhotographController.java @@ -3,6 +3,19 @@ package com.we.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.multipart.MultipartFile; +import sun.misc.BASE64Decoder; +import sun.text.resources.FormatData; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.text.DateFormat; +import java.text.Format; +import java.util.Date; @Controller @RequestMapping("/photograph") @@ -14,4 +27,38 @@ public class PhotographController { return prefix + "/photo"; } + + /** + * 头像上传 + * @param file + * @return + */ + @RequestMapping("/photoUpload") + @ResponseBody + public String fileUpload(@RequestParam("op") String op, @RequestParam("base64url") String base64url) { + if (!"takePhoto".equals(op)) { + return "false"; + } + BASE64Decoder decoder = new BASE64Decoder(); + try { + // Base64解码 + byte[] b = decoder.decodeBuffer(base64url); + for (int i = 0; i < b.length; ++i) { + if (b[i] < 0) {// 调整异常数据 + b[i] += 256; + } + } + String imgFilePath = "D:\\datafile\\photo.jpg"; + OutputStream out = new FileOutputStream(imgFilePath); + out.write(b); + out.flush(); + out.close(); + + return "true"; + } catch (Exception e) { + return "false"; + } + + } + } diff --git a/src/main/resources/templates/photograph/photo.html b/src/main/resources/templates/photograph/photo.html index 8f15c96751954ebaaaa0a1b142c608cdeae47d85..80c0492f291482b563bf6a9ee85244679611a685 100644 --- a/src/main/resources/templates/photograph/photo.html +++ b/src/main/resources/templates/photograph/photo.html @@ -84,7 +84,6 @@ mediaStreamTrack && mediaStreamTrack.stop(); $("#video").hide(); $("#showImg").show(); - } //cropper图片裁剪 $('#tailoringImg').cropper({ @@ -136,7 +135,7 @@ var cas = $('#tailoringImg').cropper('getCroppedCanvas');//获取被裁剪后的canvas var base64url = cas.toDataURL('image/png'); //转换为base64地址形式 base64url=base64url.replace("\r","") - $.post("../Info", {op:"takePhoto",base64url:base64url }, function(data) { + $.post("/photograph/photoUpload", {op:"takePhoto",base64url:base64url }, function(data) { var result=parseInt($.trim(data.result)); if(result==-1){ $.messager.show({title:"温馨提示",msg:"输入的信息不完整,确认后重新提交。",timeout:3000,showType:'slide'})