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