2014-05-21 33 views
0

我在輸出數據中遇到了麻煩。修剪結尾的檢票迴應

@Override 
protected ResourceResponse newResourceResponse(Attributes attributes) { 
    ResourceResponse response = new ResourceResponse(); 
    response.setContentDisposition(ContentDisposition.INLINE); 
    response.disableCaching(); 

    StringBuilder stringBuilder = new StringBuilder(DEFAULT_CONTENT_TYPE); 
    stringBuilder.append(";").append(charset == null ? DEFAULT_CHARSET : charset); 
    response.setContentType(stringBuilder.toString()); 
    response.setLastModified(Time.now()); 

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
    try { 
     fillOutputStream(outputStream); 
    } catch (IOException e) { 
     logger.error("Error when try to fill data for html report", e); 
    } 
    String message = null; 
    try { 
     message = outputStream.toString("UTF-8"); 
    } catch (UnsupportedEncodingException e) { 
     logger.warn("Unknown encoding"); 
     message = outputStream.toString(); 
    } 
    final CharSequence data = message; 

    response.setContentLength(data.length()); 
    response.setWriteCallback(new WriteCallback() { 
     @Override 
     public void writeData(Attributes attributes) { 
      attributes.getResponse().write(data); 
     } 
    }); 

    configureResponse(response, attributes); 
    return response; 
} 

這裏的數據是在fillOutputStream()方法中生成並轉換爲CharSequence的html頁面。

我已經記錄了數據,它有我期望的正確內容,但是在結尾我已經在最後一頁進行了修剪。

回答

2

字符串的長度不一定等於其字節數。

改爲使用CountingOutputStream(例如從番石榴)。