Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

convert bytes[] to string for html

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic