java - 将其存储为字符串后如何获得相同的字节数组?
问题描述
我使用 Java 代码将字符串压缩为字节数组,然后将此字节数组作为字符串存储在云数据库中。解密后的消息需要与 compress 方法返回的相同字节数组。那么如何从存储的字符串中获取相同的字节数组。
public class Gzip {
public static byte[] compress(String data) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length());
GZIPOutputStream gzip = new GZIPOutputStream(bos);
gzip.write(data.getBytes());
gzip.close();
byte[] compressed = bos.toByteArray();
bos.close();
return compressed;
}
public static String decompress(String temp) throws IOException {
String decodeData = new String(Base64.getDecoder().decode(temp));
byte[] compressed = decodeData.getBytes();
ByteArrayInputStream bis = new ByteArrayInputStream(compressed);
GZIPInputStream gis = new GZIPInputStream(bis);
BufferedReader br = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line;
while((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
gis.close();
bis.close();
return sb.toString();
}
public static void main(String args[]) throws IOException{
Gzip gzip = new Gzip();
String plaintext = "Although there are many short stories. ";
byte[] enbyte = gzip.compress(l);
String enStr = String.valueOf(enbyte);
// then stored enStr in cloud database
//how to get same byte array as 'enbyte' from the 'enStr'
//if you could modify the methods to return and get string this will //also be helpfull
byte[] newbyte = ???? //
System.out.println(gzip.decompress(newbyte));
}
}
解决方案
推荐阅读
- excel - 在列表中查找重复项(不比较 2 个列表),并根据先前的外观将它们显示在第二列
- xhtml - 混合内容的 DTD 验证错误元素
- javascript - 反应中的登录身份验证和laravel不记录
- highcharts - Highmaps 工具提示入门
- javascript - JavaScript - 来自匹配 ID 的新对象数组
- c# - 如何在 .NET Core 2.2 应用程序中创建 CloudTableClient?
- android - IntelliJ 中的任务“:app:compileDebugJavaWithJavac”执行失败
- linux - Pyscreenshot 循环滞后显示
- ruby-on-rails - Rails:模态表单提交后页面处于空闲状态且不可点击
- sql - 查找字符串中数字的出现