• java base64加解密


    接上篇java Base64算法

    根据之前过程使用base64加解密,所以写成了工具类。

    代码示例;

    public class Base64Util {
        
        private static Logger logger = LoggerFactory.getLogger(Base64Util.class);
    
        /**
         * @param
         * @return String
         * @description BASE64解码
         */
        public static byte[] decode(String s) {
            if (StringUtils.isEmpty(s)) {
                return null;
            }
            BASE64Decoder decoder = new BASE64Decoder();
            try {
                return decoder.decodeBuffer(s);
            } catch (Exception e) {
                return null;
            }
        }
    
        /**
         * @param
         * @return String
         * @description base64编码
         */
        public static String encode(byte[] s) {
            if (s == null) {
                return null;
            }
            return (new sun.misc.BASE64Encoder()).encode(s);
        }
    	
        /**
         * @param filePath
         * @param base64Str
         */
        public static void base64ToFile(String base64Str,String filePath) {
            System.err.println(base64Str.length());
            try {
                byte[] b = Base64.decode(base64Str);
                for (int i = 0; i < b.length; ++i) {
                    if (b[i] < 0) {// 调整异常数据
                        b[i] += 256;
                    }
                }
                // 生成jpeg图片
                FileUtils.writeByteArrayToFile(new File(filePath), b);
    
                /**
                 * 生成缩略图
                 * 若图片横比200小,高比300小,不变
                 * 若图片横比200小,高比300大,高缩小到300,图片比例不变
                 * 若图片横比200大,高比300小,横缩小到200,图片比例不变
                 * 若图片横比200大,高比300大,图片按比例缩小,横为200或高为300
                 */
                System.err.println("OK");
            } catch (Exception e) {
                System.out.println("error");
            }
        }
    
        /**
         * @param outFilePath 输出文件的地址
         * @param file 需要输出的文件
         */
        public static void saveMultipartFileToFile(MultipartFile file, String outFilePath) throws BusinessException, IOException {
            FileOutputStream os = new FileOutputStream(outFilePath);
            try {
                byte[] data =file.getBytes();
                for (int i = 0; i < data.length; ++i) {
                    if (data[i] < 0) {// 调整异常数据
                        data[i] += 256;
                    }
                }
                os.write(data);
                os.close();
            } catch (Exception e) {
               throw new BusinessException("图片转换失败");
            }finally {
                os.flush();
                os.close();
            }
        }
    
        /**
         * base64 转 Multipart
         * @param base64
         * @return
         */
        public static MultipartFile base64ToMultipart(String base64) {
            try {
                String[] baseStr = base64.split(",");
                BASE64Decoder decoder = new BASE64Decoder();
                byte[] b = new byte[0];
                b = decoder.decodeBuffer(baseStr[1]);
                for(int i = 0; i < b.length; ++i) {
                    if (b[i] < 0) {
                        b[i] += 256;
                    }
                }
                return new BASE64DecodedMultipartFile(b, baseStr[0]);
            } catch (IOException e) {
                logger.error(e.getMessage());
                return null;
            } catch (ArrayIndexOutOfBoundsException a){
                logger.error(a.getMessage());
                return null;
            }
        }
    }
    
  • 相关阅读:
    切片/修改
    代码块,-- 循环结构--字符串的格式化--字符串相关函数功能
    format 填充符号 与 格式化
    字符串的格式化format
    python字符串相关函数 *title *upper *lower *swapcase *len *count *find *index *starts with *endswith *isalpha *isdecimal *split *center *strip *replace
    双层循环经典小项目题
    while 小项目练习
    for循环 | range 对象
    关键字的使用 pass break continue
    python字符串的切片
  • 原文地址:https://www.cnblogs.com/ghostwolf1/p/14036735.html
Copyright © 2020-2023  润新知