cancel
Showing results for 
Search instead for 
Did you mean: 

retrieve image datatype to byte[]

Former Member
0 Kudos

i have a table with 2 field are image datatype.

this image datatype is use to store

emailid | attachmentname | attachement | message

attachement and message are both image datatype

in my query..do i need a specify way to retrieve it..or just like normal select statement

select emailid, attachementname,attachment , message from MailTable

all the information should return to a bean

String emailid=""; getter/setter

String attachementname=""; getter / setter

byte[] attachement=null; getter/setter

byte[] message=null; getter/setter

bean.setemailid(emailid);

bean.setattachmentname(attachment);

how am i going to set the attachment and message with byte[];

Accepted Solutions (0)

Answers (2)

Answers (2)

Sigiswald
Contributor
0 Kudos

Hi yzme,

According to <a href="http://msdn2.microsoft.com/en-us/library/ms378813.aspx">Using Advanced Data Types</a>, the <u>image</u> SQL Server type maps to the <u>LONGVARBINARY</u> JDBC type, which in turn maps to several possible java types: <u>byte[] (default), Blob, CharacterStream, BinaryStream, String</u>. The link also contains some code snippets. Take into account that the maximum size of image data type is (2^31 - 1) bytes, which is 2GB.

I suppose you want to "display" the attachment using a Web Dynpro FileDownload UI element. In NW04, the only way is to bind the <i>data</i> property of the FileDownload element to a context attribute of type <i>binary</i>. This means you need to load the attachment in memory and you can as well use rs.getBytes("ATTACHMENT") instead of rs.getBinaryStream("ATTACHMENT") and manually converting the InputStream to a byte[]. In NW04s however, the recommended way is to use the <i>resource</i> attribute of the FileDownload element and bind it to a context attribute of type <i>Resource</i>. Then you can even lazy load the file, i.e. fetch it from the database (e.g. using a PreparedStatement "SELECT ATTACHMENT FROM EMAIL WHERE EMAILID = ?") when the user clicks on the download link. See <a href="http://help.sap.com/saphelp_nw2004s/helpdata/en/42/f6ec5a09321bc7e10000000a11466f/frameset.htm">Loading the InputStream at FileDownload on Demand</a>.

Second, I assume you want to display the message using a Web Dynpro TextView UI element. Then the <i>text</i> attribute of the TextView element must be bound to a context attribute of type <i>string</i> (and you probably also want to set wrapping=true). The first thing to do is to obtain the message from the database using rs.getBytes("MESSAGE") which gives you a byte[]. The second thing to do is convert the byte[] to a String. How to do this entirely depends on what exactly is stored in the database; there's no general way to convert a byte[] to a String. You absolutely need to know how the email message, presumably text, is stored in the database as binary data before you can extract it. In case you have byte[] message, you could try the following: new String(message, "UTF-8") or some other character encoding, e.g. "ISO-8859-1". Maybe the message was Base64 encoded before storing it in the database, in which case you need to Base64 decode it first (if the String ends with a few "=" characters, this might be the case). There're a zillion ways to convert characters to bytes and vice versa... Of course, if you own the application that stores the message in the database, it's easy because you know.

Kind regards,

Sigiswald

Former Member
0 Kudos

Check my blog

/people/anilkumar.vippagunta2/blog/2007/02/20/reading-and-writing-images-from-sqlserver-in-webdynpro

Regards,Anilkumar

Former Member
0 Kudos

image datatype doesnt really mean it contains image right...

if i have a long message ....i still can store in as image right ??

i want this message to be display in the webdynpro.........


ResultSet rs = stmt.executeQuery(squery);
	try {
					
	while (rs.next()) {
         OutMailBean outMailBean = new OutMailBean();	
         outMailBean.setEmailId(rs.getString("EMAILID"));
        outMailBean.setMessage(rs.getBytes("MESSAGE"));
      }

rs.getBytes("MESSAGE"); will return a unreadable character where later i want to bind it to the textbox

[ .......................... .....message here........................... ]

Former Member
0 Kudos

Hi ,

If the source of bytes is a string , asimple bytes.toString shud do the trick

Former Member
0 Kudos

Hi,

If the column is of type IMAGE that doens't mean that you can see the image directly !!

It will be written as byte array , so you can't read/display bytes directly.

Regards,ANilkumar

Former Member
0 Kudos

if i have a very long message...

what datatype should be stored in datatabase

image ? byte ?

ResultSet rs = stmt.executeQuery(squery);

try {

while (rs.next()) {

OutMailBean outMailBean = new OutMailBean();

outMailBean.setEmailId(rs.getString("EMAILID"));

// outMailBean.setMessage(rs.getBytes("MESSAGE"));

Blob blob=rs.getBlob("MESSAGE");

byte[] msg=blob.getBytes(1, (int) blob.length()); inMailBean.setMessage(msg);

}

BLOB object already retrieved.#1#BLOB#

if i try System.out.println(rs.getByte("MESSAGE").toString());

also not work

BLOB object already retrieved.#1#BLOB#

......................


try{
System.err.println("start reading message:");
readByte(inMailBean.getMessage());
}catch(Exception e){
 Systme.err.println("Exception:" +e);   //throw null exception here
}

public void readByte(byte[] msg){
	try{
	OutputStream os=null;
	os.write(msg,0,msg.length);
        System.err.println("message written: "+os.toString());
				}catch(IOException e){
					System.err.println("IOException : "+e);
				}
				
			}

Message was edited by:

yzme yzme

Message was edited by:

yzme yzme