我正在尝试使用Google应用引擎更新照片。我有imageurl我将其转换为字节数组然后编码 它使用base64.i得到编码字符串,现在我试图使用目录API参考
更新photodatahttps://developers.google.com/admin-sdk/directory/v1/reference/users/photos#resource
更新后我得到错误无效byteString.I从昨天面对这个问题。所以让我知道我哪里做错了?以下是我的代码。
导入com.google.appengine.repackaged.org.apache.commons.codec.binary.Base64;
用于Base64的上述类。
URL url = new URL(myImageUrl);
ByteArrayOutputStream bais = new ByteArrayOutputStream();
InputStream is = null;
try {
is = url.openStream ();
byte[] byteChunk = new byte[4096]; // Or whatever size you want to read in at a time.
int n;
while ( (n = is.read(byteChunk)) > 0 ) {
bais.write(byteChunk, 0, n);
}
System.out.println(byteChunk);
byte[] encoded = Base64.encodeBase64(byteChunk);
String ecodedString = new String(encoded, "UTF-8");
ecodedString = ecodedString.replace("/", "_");
ecodedString = ecodedString.replace("+", "-");
ecodedString = ecodedString.replace("=", "*");
System.out.println(ecodedString);
答案 0 :(得分:4)
填充可能是问题,请尝试 不 将“=”替换为“*”。也可以看看: Converting string to web-safe Base64 format
P.S。在app引擎中不鼓励重新打包的lib;您可以改用DatatypeConverter.printBase64Binary()。