Blame view

src/main/java/com/objecteye/service/impl/UploadFileServiceImpl.java 3.25 KB
40c853a1   Liu Haoyu   去掉MySQL相关内容, 去掉my...
1
2
3
4
5
6
7
8
  package com.objecteye.service.impl;
  
  import com.alibaba.fastjson.JSON;
  import com.alibaba.fastjson.JSONObject;
  import com.objecteye.config.RabbitmqConfig;
  import com.objecteye.entity.ResponseParam;
  import com.objecteye.entity.UploadVehicleResult;
  import com.objecteye.entity.VpAnalysisParam;
21d68d52   Liu Haoyu   代码重构;
9
  import com.objecteye.exception.CustomXException;
40c853a1   Liu Haoyu   去掉MySQL相关内容, 去掉my...
10
11
12
13
14
  import com.objecteye.service.IUploadFileService;
  import com.objecteye.utils.GlobalUtil;
  import org.springframework.amqp.rabbit.core.RabbitTemplate;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Value;
40c853a1   Liu Haoyu   去掉MySQL相关内容, 去掉my...
15
16
17
18
19
20
21
22
23
  import org.springframework.stereotype.Component;
  import org.springframework.web.multipart.MultipartFile;
  
  import java.io.File;
  import java.io.IOException;
  
  @Component
  public class UploadFileServiceImpl implements IUploadFileService {
      @Autowired
40c853a1   Liu Haoyu   去掉MySQL相关内容, 去掉my...
24
25
26
      private RabbitTemplate rabbitTemplate;
  
      @Value("${picture.storePath}")
21d68d52   Liu Haoyu   代码重构;
27
      private String storePath;
40c853a1   Liu Haoyu   去掉MySQL相关内容, 去掉my...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
      @Value("${picture.http}")
      private String http;
      @Value("${requestFile}")
      private String url;
  
      /**
       * 上传文件解析并保存到mongo, 代替ftp
       *
       * @param multipartFile 上传文件
       * @return 结果集
       */
      @Override
      public JSONObject uploadFile2Mongo(MultipartFile multipartFile) throws IOException {
          // 格式化文件名由format开头, e.g. format-15321354564-12.12654_15.156567.jpg
          String fileName = multipartFile.getOriginalFilename();
21d68d52   Liu Haoyu   代码重构;
43
44
45
46
          if (fileName == null || "".equals(fileName)) {
              throw new CustomXException("文件名不能不能为空", "201");
          }
          String picPath = storePath + File.separator + fileName;
40c853a1   Liu Haoyu   去掉MySQL相关内容, 去掉my...
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
          File newFile = new File(picPath);
          multipartFile.transferTo(newFile);
  
          String picUrl = http + fileName;
          String latitude = null;
          String longitude = null;
          long captureTime = System.currentTimeMillis();
          if (fileName.startsWith("format")) {
              String[] fileNameMsg = fileName.split("-");
              captureTime = Long.parseLong(fileNameMsg[1]);
              String[] position = fileNameMsg[2].split("_");
              latitude = position[0];
              longitude = position[1];
          }
  
          String body = GlobalUtil.httpExecute(url, newFile);
          ResponseParam responseParam = JSON.parseObject(body, ResponseParam.class);
          String code = responseParam.getCode();
          if ("0".equals(code)) {
              String s1 = JSON.toJSONString(responseParam.getResult());
              VpAnalysisParam vpAnalysisParam = JSON.parseObject(s1, VpAnalysisParam.class);
              UploadVehicleResult uploadVehicleResult = new UploadVehicleResult();
              uploadVehicleResult.setCount(vpAnalysisParam.getCount());
              uploadVehicleResult.setInfo(vpAnalysisParam.getInfo());
              uploadVehicleResult.setPicName(fileName);
              uploadVehicleResult.setImageUrl(picUrl);
              uploadVehicleResult.setLatitude(latitude);
              uploadVehicleResult.setLongitude(longitude);
              uploadVehicleResult.setCaptureTime(captureTime);
              String s = JSON.toJSONString(uploadVehicleResult);
              rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_TOPICS_INFORM, "inform.store", s);
          }
68a67f36   Liu Haoyu   接口问题处理;
79
          return new JSONObject();
40c853a1   Liu Haoyu   去掉MySQL相关内容, 去掉my...
80
81
      }
  }