aspose file tools*
The moose likes Java in General and the fly likes convert bytes[] to string for html Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "convert bytes[] to string for html" Watch "convert bytes[] to string for html" New topic
Author

convert bytes[] to string for html

Mobio Dev
Greenhorn

Joined: Nov 03, 2010
Posts: 6
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

Joined: Oct 14, 2008
Posts: 1337
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

Joined: Jul 21, 2008
Posts: 59
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: convert bytes[] to string for html