大鹏鹏啊 2021-11-08 23:29 采纳率: 100%
浏览 114
已结题

springboot创建项目,并采用base传参,完成以后给我机试代码

编写一个类Person,该类拥有一个name属性(String)、一个hello方法,执行hello后在控制台输出name。使用spring boot搭建一个web服务,用户提交post数据(数据内容为Person实例序列化并进行base64编码后的数据,实例对象name值为icbc,使用Serializable实现序列化,序列化后进行base64 encode,将base64后的字符串作为服务输入),服务器端接收到数据先进行base64 decode,然后进行反序列化后执行hello方法。
写完以后给我具体的代码。

  • 写回答

1条回答 默认 最新

  • 关注

    用这个类进行转换即可:

    public class Base64Util {
        /**
         * 将二进制数据编码为BASE64字符串
         * 
         * @param binaryData
         * @return
         */
        public static String encode(byte[] binaryData) {
            try {
                return new String(Base64.encodeBase64(binaryData), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                return null;
            }
        }
    
        /**
         * 将BASE64字符串恢复为二进制数据
         * 
         * @param base64String
         * @return
         */
        public static byte[] decode(String base64String) {
            try {
                return Base64.decodeBase64(base64String.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                return null;
            }
        }
    
        /**
         * 将文件转成base64 字符串
         * 
         * @param path文件路径
         * @return *
         * @throws Exception
         */
    
        public static String encodeBase64File(String path) throws Exception {
            File file = new File(path);
            FileInputStream inputFile = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            inputFile.read(buffer);
            inputFile.close();
            return encode(buffer);
        }
    
        /**
         * 将base64字符解码保存文件
         * 
         * @param base64Code
         * @param targetPath
         * @throws Exception
         */
    
        public static void decoderBase64File(String base64Code, String targetPath) throws Exception {
            byte[] buffer = decode(base64Code);
            FileOutputStream out = new FileOutputStream(targetPath);
            out.write(buffer);
            out.close();
        }
    
        /**
         * 将base64字符保存文本文件
         * 
         * @param base64Code
         * @param targetPath
         * @throws Exception
         */
    
        public static void toFile(String base64Code, String targetPath) throws Exception {
    
            byte[] buffer = base64Code.getBytes();
            FileOutputStream out = new FileOutputStream(targetPath);
            out.write(buffer);
            out.close();
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
    1人已打赏

报告相同问题?

问题事件

  • 系统已结题 11月17日
  • 已采纳回答 11月9日
  • 创建了问题 11月8日

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line