ycl-common/src/main/java/com/ycl/service/oss/impl/OssServiceImpl.java
@@ -5,11 +5,18 @@ import com.aliyun.oss.model.ObjectMetadata; import com.ycl.service.oss.OssService; import com.ycl.utils.ConstantPropertiesUtils; import com.ycl.utils.common.RandomUtils; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.UUID; @@ -40,11 +47,30 @@ // 这里设置图片有效时间 我设置了30年 Date expiration = new Date(System.currentTimeMillis() + 946080000 * 1000); String url = ossClient.generatePresignedUrl(bucketName, fileName, expiration).toString(); return url; } catch (Exception e) { e.printStackTrace(); System.out.println("uploadImages上传图片失败:"); // e.printStackTrace(); return null; } } private byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); //创建一个Buffer字符串 byte[] buffer = new byte[1024]; //每次读取的字符串长度,如果为-1,代表全部读取完毕 int len = 0; //使用一个输入流从buffer里把数据读取出来 while ((len = inStream.read(buffer)) != -1) { //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度 outStream.write(buffer, 0, len); } //关闭输入流 inStream.close(); //把outStream里的数据写入内存 return outStream.toByteArray(); } @Override @@ -76,7 +102,10 @@ // 上传文件流 try { // 获取文件的名称 String fileName = "sczf/" + String.valueOf(UUID.randomUUID()); LocalDate date = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); String fileName = "sczf/" + date.format(formatter) + RandomUtils.generateRandomInt(8) + "." + ext; System.out.println("文件地址:" + fileName); ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentType(getcontentType(ext)); // 调用oss的方法实现长传 @@ -89,12 +118,24 @@ // 这里设置图片有效时间 我设置了30年 Date expiration = new Date(System.currentTimeMillis() + 946080000 * 1000); String url = ossClient.generatePresignedUrl(bucketName, fileName, expiration).toString(); // System.out.println("oss成功:" + url); System.out.println("oss上传成功:" + url); URL urlO = new URL(url); HttpURLConnection conn = (HttpURLConnection) urlO.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(10 * 1000); conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); InputStream inputStream2 = conn.getInputStream();// 通过输入流获取图片数据 byte[] data = readInputStream(inputStream2); System.out.println("获取图片成功:" + url); return url; } catch (Exception e) { System.out.println("oss异常:" + e.getMessage()); // e.printStackTrace(); return null; return this.uploadImages(inputStream, ext); } } ycl-platform/src/main/java/com/ycl/controller/platformApi/AlarmController.java
@@ -16,9 +16,11 @@ import javax.servlet.http.HttpServletResponse; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.Base64; /** * @author admin @@ -64,11 +66,11 @@ @GetMapping(value = "/img", produces = {MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_PNG_VALUE}) // @GetMapping(value = "/img") @ResponseBody public byte[] getImages(HttpServletResponse response, @RequestParam String fileUrl, @RequestParam(required = false) String OSSAccessKeyId, @RequestParam(required = false) String Signature) { public byte[] getImages(HttpServletResponse response, @RequestParam String fileUrl, @RequestParam String OSSAccessKeyId, @RequestParam String Signature) { try { URL url = new URL(fileUrl); System.out.println("getImages获取图片地址:" + fileUrl + "&OSSAccessKeyId=" + OSSAccessKeyId + "&Signature=" + Signature); URL url = new URL(fileUrl + "&OSSAccessKeyId=" + OSSAccessKeyId + "&Signature=" + Signature); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(10 * 1000); @@ -76,16 +78,47 @@ InputStream inputStream = conn.getInputStream();// 通过输入流获取图片数据 byte[] data = readInputStream(inputStream); System.out.println("getImages获取图片成功"); System.out.println("getImages -图片64"); System.out.println(Base64.getEncoder().encodeToString(data)); return data; } catch (Exception e) { System.out.println("获取图片失败:" + e.getMessage()); e.printStackTrace(); System.out.println("getImages获取图片失败:" + e.getMessage()); // e.printStackTrace(); } return null; } private static String inputStream2Base64(InputStream is) throws Exception { byte[] data = null; try { ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); byte[] buff = new byte[100]; int rc = 0; while ((rc = is.read(buff, 0, 100)) > 0) { swapStream.write(buff, 0, rc); } data = swapStream.toByteArray(); } catch (IOException e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { throw new Exception("输入流关闭异常"); } } } return Base64.getEncoder().encodeToString(data); } private byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); //创建一个Buffer字符串 ycl-platform/src/main/resources/mapper/caseHandler/BaseCaseMapper.xml
@@ -329,9 +329,6 @@ t1.alarm_time, t1.longitude, t1.latitude, -- ( CASE WHEN t1.category = 1 THEN "违规" WHEN t1.category = 2 THEN "违建" ELSE "其他" END ) AS type, -- ( CASE WHEN t1.event_source = 1 THEN "视频巡查" WHEN t1.event_source = 2 THEN "网格巡查" ELSE "其他" END ) AS alarmAdvice, dd.name AS type, ( CASE WHEN t1.event_source = 1 THEN "视频巡查" WHEN t1.event_source = 2 THEN "网格巡查" ELSE "其他" END ) AS source, t1.site AS address,