From a090d5f26c9ab7f8cdf667c76c9ba8c4207bbaca Mon Sep 17 00:00:00 2001 From: zhanghua <314079846@qq.com> Date: 星期四, 30 三月 2023 18:01:40 +0800 Subject: [PATCH] 图片测试 --- ycl-platform/src/main/resources/mapper/caseHandler/BaseCaseMapper.xml | 3 - ycl-common/src/main/java/com/ycl/service/oss/impl/OssServiceImpl.java | 49 ++++++++++++++++++++++-- ycl-platform/src/main/java/com/ycl/controller/platformApi/AlarmController.java | 45 +++++++++++++++++++--- 3 files changed, 84 insertions(+), 13 deletions(-) diff --git a/ycl-common/src/main/java/com/ycl/service/oss/impl/OssServiceImpl.java b/ycl-common/src/main/java/com/ycl/service/oss/impl/OssServiceImpl.java index 47a4f18..c6c001c 100644 --- a/ycl-common/src/main/java/com/ycl/service/oss/impl/OssServiceImpl.java +++ b/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(); + //鍒涘缓涓�涓狟uffer瀛楃涓� + byte[] buffer = new byte[1024]; + //姣忔璇诲彇鐨勫瓧绗︿覆闀垮害锛屽鏋滀负-1锛屼唬琛ㄥ叏閮ㄨ鍙栧畬姣� + int len = 0; + //浣跨敤涓�涓緭鍏ユ祦浠巄uffer閲屾妸鏁版嵁璇诲彇鍑烘潵 + while ((len = inStream.read(buffer)) != -1) { + //鐢ㄨ緭鍑烘祦寰�buffer閲屽啓鍏ユ暟鎹紝涓棿鍙傛暟浠h〃浠庡摢涓綅缃紑濮嬭锛宭en浠h〃璇诲彇鐨勯暱搴� + outStream.write(buffer, 0, len); + } + //鍏抽棴杈撳叆娴� + inStream.close(); + //鎶妎utStream閲岀殑鏁版嵁鍐欏叆鍐呭瓨 + 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); } } diff --git a/ycl-platform/src/main/java/com/ycl/controller/platformApi/AlarmController.java b/ycl-platform/src/main/java/com/ycl/controller/platformApi/AlarmController.java index f9aa42f..d848cee 100644 --- a/ycl-platform/src/main/java/com/ycl/controller/platformApi/AlarmController.java +++ b/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(); //鍒涘缓涓�涓狟uffer瀛楃涓� diff --git a/ycl-platform/src/main/resources/mapper/caseHandler/BaseCaseMapper.xml b/ycl-platform/src/main/resources/mapper/caseHandler/BaseCaseMapper.xml index 9b49703..610a851 100644 --- a/ycl-platform/src/main/resources/mapper/caseHandler/BaseCaseMapper.xml +++ b/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, -- Gitblit v1.8.0