【发布时间】:2016-01-03 08:38:12
【问题描述】:
我想使用字节流格式将多个图像放入 JSON 对象中,我编写了以下代码。
FileInputStream fin = new FileInputStream(pathToImages+"//"+"01.jpg");
BufferedInputStream bin = new BufferedInputStream(fin);
BufferedOutputStream bout = new BufferedOutputStream(out);
int ch =0; ;
sun.misc.BASE64Encoder encoder= new sun.misc.BASE64Encoder();
byte[] contents = new byte[5000000];
int bytesRead = 0;
String strFileContents;
while ((bytesRead = bin.read(contents)) != -1) {
bout.write(encoder.encode(contents).getBytes());
}
JsonObject myObj = new JsonObject();
我想把编码后的字节流放到myObj中,但不知道怎么做。
谢谢
【问题讨论】:
-
如果您使用的是 Java 8,您可能希望改用 docs.oracle.com/javase/8/docs/api/java/util/Base64.html。