This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

convert bytes[] to string for html

 
Mobio Dev
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi experts, i upload html file into db as byte[] in blob column, and in another part i have to retrieve this file and display in textarea, i'm able to retrieve from db as bytes and convert it into string, but when display its shows as encrypt format, this is the struts application.

this is my jsp
---------------
<tr>
<td colspan="3" class="searchinput">
<html:textarea property="template" cols="100" rows="10" name="sendEmailForm">
</html:textarea>
</td>
</tr>

this is my form bean
-----------------------
private String template = null;
public String getTemplate() {
return template;
}

public void setTemplate(String template) {
this.template = template;
}

this is my bean
--------------------
private byte[] templateContent = null;
public String getHtmlTemplateContent() {
return templateContent.toString();
}
public byte[] getTemplateContent() {
return templateContent;
}
public void setTemplateContent(byte[] templateContent) {
this.templateContent = templateContent;
}
public void setTemplateContent(Object templateContent) {
this.templateContent = (byte[])templateContent;
}

this is my action
-------------------
templatesDataBean = (TemplatesDataBean)SendEmailManager.getTemplate(action, actor, sendEmailBean);
sendEmailForm.setTemplate(new String(templatesDataBean.getHtmlTemplateContent()));

how this can be solve, thanks in advance
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
templateContent.toString() does not do what you think it does.

Also, the code seems to ignore encodings completely when converting between byte[] and String; this will most likely be a problem. (Hint: It's almost always an error to use "new String(byte[])" and "String.getBytes()" - you generally need to specify the encoding when using those calls.)
 
John Pradeep.v
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mobio Dev wrote:
this is my bean
--------------------
private byte[] templateContent = null;
public String getHtmlTemplateContent() {
return templateContent.toString();
}
public byte[] getTemplateContent() {
return templateContent;
}
public void setTemplateContent(byte[] templateContent) {
this.templateContent = templateContent;
}
public void setTemplateContent(Object templateContent) {
this.templateContent = (byte[])templateContent;
}

this is my action
-------------------
templatesDataBean = (TemplatesDataBean)SendEmailManager.getTemplate(action, actor, sendEmailBean);
sendEmailForm.setTemplate(new String(templatesDataBean.getHtmlTemplateContent()));

how this can be solve, thanks in advance


When you say toString() on the byte array it doesnt give you the actual string pushed into the array, you should ideally pass the byte[] to the String constructor... i think if you avoid calling toString in the getHtmlTemplateContent() and just create a new string by passing the byte array as parameter would solve the problem
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic