依赖于声纳的默认编码

时间:2015-06-16 09:47:14

标签: java

找到一个方法的调用,该方法将执行字节到String(或String to byte)转换,并假设默认平台编码是合适的。这将导致应用程序行为在平台之间变化。使用备用API并明确指定charset名称或Charset对象。

try {           
 OutputStream outputStream = pResponse.getOutputStream();             
 if (ticketList != null && !ticketList.isEmpty()) {                    
    outputStream.write(ticketList.getBytes());                     
    outputStream.flush();                 
    outputStream.close();             
  }         
} catch (IOException e) {             
  Logger.logInfo(getClass(), "ticket list download", e);                 
  Logger.logError(getClass(), "ticket list download",    
     e.getMessage());        
 }

如何解决此问题?

1 个答案:

答案 0 :(得分:3)

您需要明确定义要使用的字符集。通常UTF-8是一个不错的选择。

try {
 Charset charset = Charset.forName("UTF-8");           
 OutputStream outputStream = pResponse.getOutputStream();             
 if (ticketList != null && !ticketList.isEmpty()) {                    
    outputStream.write(ticketList.getBytes(charset));                     
    outputStream.flush();                 
    outputStream.close();             
  }         
} catch (IOException e) {             
  Logger.logInfo(getClass(), "ticket list download", e);                 
  Logger.logError(getClass(), "ticket list download",    
     e.getMessage());        
 }