Browse Source

minio图片上传和获取token和数据传输代码开发

gao.qiang 2 years ago
parent
commit
d1d94fa803

+ 13 - 16
hazard-sdk/src/main/java/com/ozs/controller/upload/GeoHazardMonitorTokenController.java

@@ -9,7 +9,6 @@ import com.ozs.vo.RespMsgAlarmVo;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.ResponseBody;
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.annotation.Resource;
@@ -36,23 +35,20 @@ public class GeoHazardMonitorTokenController {
      * @return
      */
     @PostMapping("/IPAddress/token")
-    @ResponseBody
-    public Result login(@RequestParam("grantType") String grantType,
+    public Result getToken(@RequestParam("grantType") String grantType,
                         @RequestParam("clientId") String clientId,
                         @RequestParam("clientSecret") String clientSecret) {
         // 生成令牌
-//        ArrayList<String> objects = apiTokenUtils.createGeoHazardMonitorToken();
-//        if (objects.size() > 0) {
-//            RespGeoHazardMonitorVo respGeoHazardMonitorVo = new RespGeoHazardMonitorVo();
-//            respGeoHazardMonitorVo.setAccessToken(objects.get(1));
-//            respGeoHazardMonitorVo.setExpiresIn(Long.valueOf(objects.get(0)));
-//            respGeoHazardMonitorVo.setTokenType("令牌类型");
-//            return new Result(1, "成功", respGeoHazardMonitorVo);
-//        } else {
-//            return new Result(2, "失败", "生成token失败!!");
-//        }
-        int i=1;
-        return Result.success();
+        ArrayList<String> objects = apiTokenUtils.createGeoHazardMonitorToken();
+        if (objects.size() > 0) {
+            RespGeoHazardMonitorVo respGeoHazardMonitorVo = new RespGeoHazardMonitorVo();
+            respGeoHazardMonitorVo.setAccessToken(objects.get(1));
+            respGeoHazardMonitorVo.setExpiresIn(Long.valueOf(objects.get(0)));
+            respGeoHazardMonitorVo.setTokenType("令牌类型");
+            return new Result(1, "成功", respGeoHazardMonitorVo);
+        } else {
+            return new Result(2, "失败", "生成token失败!!");
+        }
     }
 
     /**
@@ -62,7 +58,8 @@ public class GeoHazardMonitorTokenController {
      * @return
      */
     @PostMapping("/IPAddress/alarm")
-    public Result login(@RequestBody ReqMsgAlarmVo reqMsgAlarmVo, HttpServletRequest request) {
+    public Result alarm(@RequestBody ReqMsgAlarmVo reqMsgAlarmVo,
+                                     HttpServletRequest request) {
         // 生成令牌
         String token = apiTokenUtils.getGeoHazardMonitorToken(request);
         if (StringUtils.isNotEmpty(token)) {

+ 31 - 31
hazard-sdk/src/main/java/com/ozs/controller/upload/UploadController.java

@@ -18,35 +18,35 @@ import java.util.Date;
 @RequestMapping("/system/upLoad")
 public class UploadController {
 
-//    @Value("${minio.bucketName}")
-//    private String bucketName;
-//
-//    @Resource
-//    MinioUtils minioUtils;
-//
-//
-//    @PostMapping("/upLoadImage")
-//    public AjaxResult upLoadImage(@RequestParam("file") MultipartFile image) throws Exception {
-//        if (image.isEmpty()) {
-//            return AjaxResult.error("不能上传空文件哦");
-//        }
-//        //图片保存路径
-//        //String fileUploadPath ="/"+userId+"/image";
-//        String uploadFile=null;
-//        if (image != null && !image.isEmpty()) {
-//
-//            String imageName = image.getOriginalFilename();
-//            if (StringUtils.isNotBlank(imageName)) {
-//                String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
-//                String filename = date + image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf("."));
-//                String imgName = "img" + "/" + filename;
-//                minioUtils.minIoClientUpload(image.getInputStream(), imgName);
-//                uploadFile =  "/" + bucketName + "/" + imgName;
-//
-//            }
-//        } else {
-//            return AjaxResult.error("上传功能出错");
-//        }
-//        return AjaxResult.success(uploadFile);
-//    }
+    @Value("${minio.bucketName}")
+    private String bucketName;
+
+    @Resource
+    MinioUtils minioUtils;
+
+
+    @PostMapping("/upLoadImage")
+    public AjaxResult upLoadImage(@RequestParam("file") MultipartFile image) throws Exception {
+        if (image.isEmpty()) {
+            return AjaxResult.error("不能上传空文件哦");
+        }
+        //图片保存路径
+        //String fileUploadPath ="/"+userId+"/image";
+        String uploadFile=null;
+        if (image != null && !image.isEmpty()) {
+
+            String imageName = image.getOriginalFilename();
+            if (StringUtils.isNotBlank(imageName)) {
+                String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
+                String filename = date + image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf("."));
+                String imgName = "img" + "/" + filename;
+                minioUtils.minIoClientUpload(image.getInputStream(), imgName);
+                uploadFile =  "/" + bucketName + "/" + imgName;
+
+            }
+        } else {
+            return AjaxResult.error("上传功能出错");
+        }
+        return AjaxResult.success(uploadFile);
+    }
 }