’字符在返回时转换为问号

时间:2016-11-27 22:08:19

标签: java amazon-web-services encoding compression amazon-sqs

我有一个非常奇怪的问题。 我正在从Amazon AWS SQS发送消息并从中获取消息。 虽然我正在压缩和编码消息,如下所示:

String responseMessageBodyOriginal = gson.toJson(responseData);
String responseMessageBodyCompressed = compressToBase64String(responseMessageBodyOriginal);
AmazonSqsHelper.sendMessage(responseMessageBodyCompressed, queue, null);

压缩和编码功能,如下所示:

public static String compressToBase64String(String data) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length());
    GZIPOutputStream gzip = new GZIPOutputStream(bos);
    gzip.write(data.getBytes());
    gzip.close();
    byte[] compressedBytes = bos.toByteArray();
    bos.close();
    return new String(Base64.encodeBase64(compressedBytes));
}

另一方面,在接收消息时,这是代码:

List<Message> sqsMessageList = AmazonSqsHelper.receiveMessages(queueUrl, max_message_read_count,
                    default_visibility_timeout);
int num_messages = sqsMessageList.size();
if (num_messages > 0) {
   for (Message m : sqsMessageList) {
       String responseMessageBodyCompressed = m.getBody();
       String responseMessageBodyOriginal = decompressFromBase64String(responseMessageBodyCompressed);
   }
}

用于解码和解压缩的函数是这样的:

public static String decompressFromBase64String(String compressedString) throws IOException {
    byte[] compressedBytes = Base64.decodeBase64(compressedString);
    ByteArrayInputStream bis = new ByteArrayInputStream(compressedBytes);
    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();
}

但问题是,有时如果我传递“â”这样的字符,那么这些字符会被转换为???? ,解码后如果我正在打印消息。

无法弄清楚为什么编码和解码表现得很奇怪。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

问题是使用平台的默认字符集(data.getBytes())完成编码,同时解码 - 使用UTF-8。

compressToBase64String中将data.getBytes()更改为data.getBytes(StandardCharsets.UTF_8)