Explorar o código

增加接口处理

zhuoning %!s(int64=3) %!d(string=hai) anos
pai
achega
9e202e1757

+ 1 - 0
huimv-manage/src/main/java/com/huimv/manage/controller/ApplyController.java

@@ -104,6 +104,7 @@ public class ApplyController {
         // Step1.构造请求字串
         // Step2.发送请求传
         // Step3.请求结果入库
+
         String stateBln = produceMissionTask.parseReturnSetMissionHasDownStateXML(soap.callSoap(produceMissionTask.getSetMissionHasDownloadXML(applyId),webServiceUrl));
         if(stateBln.trim().equalsIgnoreCase("true")){
             // 修改状态

+ 57 - 0
huimv-manage/src/main/java/com/huimv/manage/util/HexUtils.java

@@ -0,0 +1,57 @@
+package com.huimv.manage.util;
+
+import org.springframework.stereotype.Component;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@Component
+public class HexUtils {
+    private static final char[] HEXES = {
+            '0', '1', '2', '3',
+            '4', '5', '6', '7',
+            '8', '9', 'a', 'b',
+            'c', 'd', 'e', 'f'
+    };
+
+    /**
+     * byte数组 转换成 16进制小写字符串
+     */
+    public static String bytes2Hex(byte[] bytes) {
+        if (bytes == null || bytes.length == 0) {
+            return null;
+        }
+
+        StringBuilder hex = new StringBuilder();
+
+        for (byte b : bytes) {
+            hex.append(HEXES[(b >> 4) & 0x0F]);
+            hex.append(HEXES[b & 0x0F]);
+        }
+
+        return hex.toString();
+    }
+
+    /**
+     * 16进制字符串 转换为对应的 byte数组
+     */
+    public static byte[] hex2Bytes(String hex) {
+        if (hex == null || hex.length() == 0) {
+            return null;
+        }
+
+        char[] hexChars = hex.toCharArray();
+        byte[] bytes = new byte[hexChars.length / 2];   // 如果 hex 中的字符不是偶数个, 则忽略最后一个
+
+        for (int i = 0; i < bytes.length; i++) {
+            bytes[i] = (byte) Integer.parseInt("" + hexChars[i * 2] + hexChars[i * 2 + 1], 16);
+        }
+
+        return bytes;
+    }
+}

+ 94 - 0
huimv-manage/src/main/java/com/huimv/manage/util/ImageUtils.java

@@ -0,0 +1,94 @@
+package com.huimv.manage.util;
+
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import javax.imageio.ImageIO;
+
+import org.springframework.stereotype.Component;
+import sun.misc.BASE64Decoder;
+import sun.misc.BASE64Encoder;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@Component
+public class ImageUtils {
+    /**
+     * 将网络图片进行Base64位编码
+     *
+     * @param imageUrl
+     *            图片的url路径,如http://.....xx.jpg
+     * @return
+     */
+    public String encodeImgageToBase64(URL imageUrl) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
+        ByteArrayOutputStream outputStream = null;
+        try {
+            BufferedImage bufferedImage = ImageIO.read(imageUrl);
+            outputStream = new ByteArrayOutputStream();
+            ImageIO.write(bufferedImage, "jpg", outputStream);
+        } catch (MalformedURLException e1) {
+            e1.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        // 对字节数组Base64编码
+        BASE64Encoder encoder = new BASE64Encoder();
+        return encoder.encode(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串
+    }
+
+    /**
+     * 将本地图片进行Base64位编码
+     *
+     * @param imageFile
+     *            图片的url路径,如http://.....xx.jpg
+     * @return
+     */
+    public String encodeImgageToBase64(File imageFile) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
+        ByteArrayOutputStream outputStream = null;
+        try {
+            BufferedImage bufferedImage = ImageIO.read(imageFile);
+            outputStream = new ByteArrayOutputStream();
+            ImageIO.write(bufferedImage, "jpg", outputStream);
+        } catch (MalformedURLException e1) {
+            e1.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        // 对字节数组Base64编码
+        BASE64Encoder encoder = new BASE64Encoder();
+        return encoder.encode(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串
+    }
+
+    /**
+     * 将Base64位编码的图片进行解码,并保存到指定目录
+     *
+     * @param base64
+     *            base64编码的图片信息
+     * @return
+     */
+    public void decodeBase64ToImage(String base64, String path,
+                                           String imgName) {
+        System.out.println(""+base64);
+        BASE64Decoder decoder = new BASE64Decoder();
+        try {
+            FileOutputStream write = new FileOutputStream(new File(path
+                    + imgName));
+            byte[] decoderBytes = decoder.decodeBuffer(base64);
+            write.write(decoderBytes);
+            write.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+}

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 81 - 0
huimv-manage/src/test/java/com/huimv/manage/controller/Base64Test.java


+ 147 - 0
huimv-manage/src/test/java/com/huimv/manage/controller/ByteTest.java

@@ -0,0 +1,147 @@
+package com.huimv.manage.controller;
+
+import com.huimv.manage.util.HexUtils;
+import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import sun.misc.BASE64Decoder;
+import sun.misc.BASE64Encoder;
+
+import javax.xml.bind.DatatypeConverter;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.math.BigInteger;
+import java.sql.SQLOutput;
+import java.util.Base64;//注意:这是jdk8才有的
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@SpringBootTest
+public class ByteTest {
+
+    @Test
+    public void ByteArrayToBinary(){
+//        String byte1 = "paCJTEwGOANoAXgAYAKgAbADaAIgBXUFkh4iNEHek8Y=";
+//        Byte by1 = new Byte("paCJTEwGOANoAXgAYAKgAbADaAIgBXUFkh4iNEHek8Y=");
+//        System.out.println("二进制数据>>");
+//        String str = System.Text.Encoding.ASCII.GetString ( "paCJTEwGOANoAXgAYAKgAbADaAIgBXUFkh4iNEHek8Y=" );
+//        System.out.println("2进制:"   + binary(by1, 2));
+
+        String res = new String("paCJTEwGOANoAXgAYAKgAbADaAIgBXUFkh4iNEHek8Y=");
+        System.out.println("res>>"+res);
+    }
+
+    @Test
+    public void testBase64() throws UnsupportedEncodingException {
+        String text = "paCJTEwGOANoAXgAYAKgAbADaAIgBXUFkh4iNEHek8Y=";
+        byte[] asBytes = Base64.getDecoder().decode(text);
+        System.out.println(new String(asBytes, "utf-8")); //这是Base64编码前的原始数据!!!
+    }
+
+    @Test
+    public void test2() throws Exception{
+        String code_format = "ISO-8859-1";
+//        String sourceData = "这是Base64编码前的原始数据!!!";
+        String sourceData = "a5a0894c4c063803680178006002a001b003680220057505921e223441de93c6";
+        byte[] bytes = sourceData.getBytes(code_format);
+        System.out.println("最初的数据="+sourceData);
+
+        // 编码
+        String asB64 = Base64.getEncoder().encodeToString(bytes);
+        System.out.println("编码后数据="+asB64+",length="+asB64.length()); //6L+Z5pivQmFzZTY057yW56CB5YmN55qE5Y6f5aeL5pWw5o2u77yB77yB77yB
+        System.out.println("vbYmrvwFiAAwAmADYALYAdgDMAIoB0OSODyqJceg88A=.length="+"paCJTEwGOANoAXgAYAKgAbADaAIgBXUFkh4iNEHek8Y=".length());
+        // 解码
+        byte[] asBytes = Base64.getDecoder().decode(asB64);
+        System.out.println("解码后数据="+new String(asBytes, code_format)); //这是Base64编码前的原始数据!!!
+    }
+
+    @Test
+    public void test3() {
+        String str = "a5a0894c4c063803680178006002a001b003680220057505921e223441de93c6";
+        //BASE64编码与解码
+        String encode = DatatypeConverter.printBase64Binary(str.getBytes());
+        System.out.println("1="+encode);
+        byte[] decode= DatatypeConverter.parseBase64Binary(encode);
+        System.out.println("2="+new String(decode));
+
+        //16进制编码与解码
+        String encode1 = DatatypeConverter.printHexBinary(str.getBytes());
+        System.out.println("3="+encode1);
+        byte[] decode1= DatatypeConverter.parseHexBinary(encode1);
+        System.out.println("4="+new String(decode1));
+    }
+
+    @Test
+    public void test4() throws IOException {
+        String data = "a5a0894c4c063803680178006002a001b003680220057505921e223441de93c6";
+        BASE64Encoder encoder = new BASE64Encoder();
+        String e1 = encoder.encode(data.getBytes("UTF-8"));
+        System.out.println("e1="+e1);
+
+        BASE64Decoder decode = new BASE64Decoder();
+        byte[] d1 = decode.decodeBuffer(e1);
+        System.out.println("d1="+new String(d1));
+    }
+
+
+
+    public static String binary(byte[] bytes, int radix) {
+        return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数
+    }
+
+    @Autowired
+    private HexUtils hexUtils;
+
+    @Test
+    public void hexUtils(){
+        String data = "a5a0894c4c063803680178006002a001b003680220057505921e223441de93c6";
+
+        // byte[] 转换为 16进制字符串
+//        String hex = HexUtils.bytes2Hex(data.getBytes());
+//        System.out.println(hex);                    // 输出: 48656c6c6f20576f726c64
+
+        String hex = "vbYmrvwFiAAwAmADYALYAdgDMAIoB0OSODyqJceg88A=";
+        // 16进制字符串 转换为 byte[]
+        byte[] bytes = HexUtils.hex2Bytes(hex);
+        System.out.println(new String(bytes));      // 输出: Hello World
+    }
+
+    @Test
+    public void parse1(){
+        String base64_str = "MDQw";
+        System.out.println("base64:" + base64_str);
+        String hex = DatatypeConverter.printHexBinary(DatatypeConverter.parseBase64Binary(base64_str));
+
+        for (int i = 0; i < hex.length(); i+=6) {
+            String bytes = hex.substring(i, i+6);
+
+            System.out.println("hex: " + bytes);
+
+            StringBuilder binary = new StringBuilder();
+
+            int byte3_int = Integer.parseInt(bytes.substring(4, 6), 16);
+            String byte3_str = Integer.toBinaryString(byte3_int);
+            byte3_int = Integer.valueOf(byte3_str);
+            binary.append(String.format("%08d", byte3_int));
+
+            int byte2_int = Integer.parseInt(bytes.substring(2, 4), 16);
+            String byte2_str = Integer.toBinaryString(byte2_int);
+            byte2_int = Integer.valueOf(byte2_str);
+            binary.append(String.format("%08d", byte2_int));
+
+            int byte1_int = Integer.parseInt(bytes.substring(0, 2), 16);
+            String byte1_str = Integer.toBinaryString(byte1_int);
+            byte1_int = Integer.valueOf(byte1_str);
+            binary.append(String.format("%08d", byte1_int));
+
+            System.out.println("binary: " + binary);
+        }
+    }
+}