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7KrgWxbUagFUFfrIC0pPt5C3sVLRFx1+xCaRXczK9LAm+8WxKv51u0P6HrNgBSGa2Uvi0Tk0QMMuthsLWO2dRNiFTWqm3BrCLd5FXiWCKN3NRng50o/mZ1LC0YubIoXeo/l5UjBHcTpORmBmzqWzJNv5H6f72cAsS5qS5uyFZkErh08iKqY6+uO2mG6cc+/W3HlrXQReWRaZAH8wb4LcFDmuyxXm6TFbrc8UKJ8IMX0KSjtcEDWQr6ZCBndL1t9peWQUyvRsAVkf6xFHx1ZWMTk3PKpTIxFhr97YD8CsbLsE+yQvzQxGRZzoVWfGvW5nwyJEUMXcx6ObTIH3fdeg6loOlRhgHpMs75RjbpNwfk57FyDlbhgSno78IQ/Ls0V01eddYCEO+FQOyDfVPoZNbLOaL+J/llTTtDTVtWeF4taqaAN3w8aOJSky67RduCsQP5YERtTdH5RoEKB0A0iJwmYhbn3ScxmB6CxQCsHdakfcKjq/MSdZO704uyY7tFLjtRgr8V7+whk3HckIbTZSuSBi0dath8hZTZbc0ELcYO5jH9sD5D1JpZRL0ZNKnQ2GsceeAOG7IasaRj/a7sTAAVz8c1QD2D78saYHoaYOZNk1fDvCHuFegLeUtfTLCTychtKra0bZ6Z3Jza1f90NXHmzDGyM2cOyM4ckJ05G1nWwva3E+4/RJ0Nyv4JMAD4YPGJjdAeMAAAAABJRU5ErkJggg==", "cargoReceiptLineNo":"000009_01" }
後臺代碼:測試
@RequestMapping(value = {"/photoJson"}) @ResponseBody public PhysicalIdCheckDto testPhotoJson(@RequestBody PhysicalIdCheckDto physicalIdCheckDto) { String imgStr = physicalIdCheckDto.getCargoReceiptNo(); BASE64Decoder decoder = new BASE64Decoder(); try { byte[] b = decoder.decodeBuffer(imgStr); for (int i = 0; i < b.length; ++i) { if (b[i] < 0) { b[i] += 256; } } OutputStream out = new FileOutputStream("E:\\img\\logo.jpg"); out.write(b); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } System.out.println("輸出JSON:"+physicalIdCheckDto); return physicalIdCheckDto; }
base64轉圖片編碼
/** * @Description: 將base64編碼字符串轉換爲圖片 * @Author: * @CreateTime: * @param imgStr base64編碼字符串 * @param path 圖片路徑-具體到文件 * @return */ public static boolean generateImage(String imgStr, String path) { if (imgStr == null) return false; BASE64Decoder decoder = new BASE64Decoder(); try { // 解密 byte[] b = decoder.decodeBuffer(imgStr); // 處理數據 for (int i = 0; i < b.length; ++i) { if (b[i] < 0) { b[i] += 256; } } OutputStream out = new FileOutputStream(path); out.write(b); out.flush(); out.close(); return true; } catch (Exception e) { return false; } }
圖片轉base64加密
/** * @Description: 根據圖片地址轉換爲base64編碼字符串 * @Author: * @CreateTime: * @return */ public static String getImageStr(String imgFile) { InputStream inputStream = null; byte[] data = null; try { inputStream = new FileInputStream(imgFile); data = new byte[inputStream.available()]; inputStream.read(data); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } // 加密 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data); }
測試:spa
/** * 示例 */ public static void main(String[] args) { String strImg = getImageStr("F:/86619-106.jpg"); System.out.println(strImg); generateImage(strImg, "F:/86619-107.jpg"); }