diff --git a/src/main/java/cn/hellohao/controller/ClientAppController.java b/src/main/java/cn/hellohao/controller/ClientAppController.java index f5fe65d..3b352d2 100644 --- a/src/main/java/cn/hellohao/controller/ClientAppController.java +++ b/src/main/java/cn/hellohao/controller/ClientAppController.java @@ -8,11 +8,17 @@ import cn.hellohao.pojo.User; import cn.hellohao.service.AppClientService; import cn.hellohao.service.ImgService; import cn.hellohao.service.UploadConfigService; +import cn.hellohao.service.impl.UploadServicel; import cn.hellohao.service.impl.UserServiceImpl; +import cn.hellohao.utils.Base64ToMultipartFile; import com.alibaba.fastjson.JSONObject; import org.apache.shiro.SecurityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import sun.misc.BASE64Decoder; + +import javax.servlet.http.HttpServletRequest; /** * @author Hellohao @@ -31,14 +37,16 @@ public class ClientAppController { private UserServiceImpl userService; @Autowired private UploadConfigService uploadConfigService; + @Autowired + private UploadServicel uploadServicel; @PostMapping("/loginByToken") @ResponseBody public Msg loginByToken(@RequestParam(value = "data", defaultValue = "") String data) { Msg msg = new Msg(); - JSONObject jsonObj = JSONObject.parseObject(data); - JSONObject jsonObject = new JSONObject(); try{ + JSONObject jsonObj = JSONObject.parseObject(data); + JSONObject jsonObject = new JSONObject(); User newUser = new User(); String userToken = jsonObj.getString("userToken"); newUser.setToken(userToken); @@ -99,6 +107,27 @@ public class ClientAppController { return msg; } + @PostMapping("/imgUploading") + public Msg imgUploading(@RequestParam(required = true, value = "data") String data, HttpServletRequest request) { + Msg msg = new Msg(); + try{ + JSONObject jsonObj = JSONObject.parseObject(data); + String imgstr = jsonObj.getString("imgstr"); + Integer days = jsonObj.getInteger("days"); +// String imgclass = jsonObj.getString("imgclass"); + if (null != imgstr && !imgstr.isEmpty()) { + MultipartFile multipartFile = Base64ToMultipartFile.base64Convert(imgstr); + msg = uploadServicel.uploadForLoc(request, multipartFile, days, null); + } + }catch (Exception e){ + e.printStackTrace(); + msg.setCode("500"); + } + + return msg; + + } + } diff --git a/src/main/java/cn/hellohao/utils/Base64ToMultipartFile.java b/src/main/java/cn/hellohao/utils/Base64ToMultipartFile.java new file mode 100644 index 0000000..18eec09 --- /dev/null +++ b/src/main/java/cn/hellohao/utils/Base64ToMultipartFile.java @@ -0,0 +1,98 @@ +package cn.hellohao.utils; + +import org.springframework.web.multipart.MultipartFile; +import sun.misc.BASE64Decoder; + +import java.io.*; + +/** + * base64转为multipartFile工具类 + * base64Convert + */ + +public class Base64ToMultipartFile implements MultipartFile { + private final byte[] imgContent; + private final String header; + + + + public Base64ToMultipartFile (byte[] imgContent, String header) { + this.imgContent = imgContent; + this.header = header.split(";")[0]; + } + + @Override + public String getName() { + return System.currentTimeMillis() + Math.random() + "." + header.split("/")[1]; + } + + @Override + public String getOriginalFilename() { + return System.currentTimeMillis() + (int) Math.random() * 10000 + "." + header.split("/")[1]; + } + + @Override + public String getContentType() { + return header.split(":")[1]; + } + + @Override + public boolean isEmpty() { + return imgContent == null || imgContent.length == 0; + } + + @Override + public long getSize() { + return imgContent.length; + } + + @Override + public byte[] getBytes() throws IOException { + return imgContent; + } + + @Override + public InputStream getInputStream() throws IOException { + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imgContent); + // 关闭流,不然文件会一直占用资源,无法对其操作 + byteArrayInputStream.close(); + return byteArrayInputStream; + } + + @Override + public void transferTo(File dest) throws IOException, IllegalStateException { + FileOutputStream fileOutputStream = new FileOutputStream(dest); + fileOutputStream.write(imgContent); + // 关闭流,不然文件会一直占用资源,无法对其操作 + fileOutputStream.close(); + } + + /** + * base64转multipartFile + * + * @param base64 + * @return + */ + public static MultipartFile base64Convert(String base64) { + String[] baseStrs = base64.split(","); + + BASE64Decoder decoder = new BASE64Decoder(); + byte[] b = new byte[0]; + try { + b = decoder.decodeBuffer(baseStrs[1]); + } catch (IOException e) { + e.printStackTrace(); + } + for (int i = 0; i < b.length; ++i) { + if (b[i] < 0) { + b[i] += 256; + } + } + return new Base64ToMultipartFile (b, baseStrs[0]); + } + + + + +} +