cancel
Showing results for 
Search instead for 
Did you mean: 

Images In DataBase

Former Member
0 Kudos

Hello, i have one question.

I have j2ee application, which takes from the servlet an image( InputStream) and send it to my Session Bean, how can i write this Stream in my database if String field reads this how gif89a?????????????????????, in database this puts how gif89a, other words my stream breaks on first "?", can i write to DB as byte[] ???

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi,

you can read the byte streams of image and convert that to character stream(as a String) using Base64 utilities, you can store this string in database. This avoids the restrictions using byte array.

thanks

Kiran

Message was edited by: Kiran Kumar D V S

former_member182372
Active Contributor
0 Kudos

Hi Nikita,

Check http://jdbc.postgresql.org/documentation/80/binary-data.html

Best regards, Maksim Rashchynski.

Former Member
0 Kudos

Yes, but if i working with ejbQL?

Former Member
0 Kudos

If you are using Data Dictionary, you can specify the file type as buit in type as String and jdbc type as CLOB.

Before storing in database convert the byte stream to String.

Kiran

Former Member
0 Kudos

Yes, but my String consists of gif89!????????????????????????????ssome text...

And database ake only gif89! , and ignore "?" symbols

former_member182372
Active Contributor
0 Kudos

Hi Nikita,

According to <a href="http://help.sap.com/saphelp_nw04/helpdata/en/13/dbb2b66146934a9662918755038ea1/frameset.htm">Object/Relational Mapping Rules</a>

byte[] can be mapped to VARBINARY, BINARY, LONGVARBINARY, BLOB.

Best regards, Maksim Rashchynski.

Former Member
0 Kudos

<%

System.out.println("Start...");

String contentType = request.getContentType();

System.out.println("Content type is :: " + contentType);

if ((contentType != null)

&& (contentType.indexOf("multipart/form-data") >= 0))

{

DataInputStream in =

new DataInputStream(request.getInputStream());

int formDataLength = request.getContentLength();

byte dataBytes[] = new byte[formDataLength];

int byteRead = 0;

int totalBytesRead = 0;

while (totalBytesRead < formDataLength)

{

byteRead =

in.read(dataBytes, totalBytesRead, formDataLength);

totalBytesRead += byteRead;

}

out.println("<BR><BR>");

out.println("<BR><BR>");

//out.println(dataBytes.toString());

out.println("<BR><BR>");

out.println("<BR><BR>");

String file = new String(dataBytes);

String saveFile =

file.substring(file.indexOf("filename=\"") + 10);

saveFile = saveFile.substring(0, saveFile.indexOf("\n"));

saveFile =

saveFile.substring(

saveFile.lastIndexOf("
") + 1,

saveFile.indexOf("\""));

//out.println(saveFile);

out.print(dataBytes);

int lastIndex = contentType.lastIndexOf("=");

String boundary =

contentType.substring(lastIndex + 1, contentType.length());

//out.println(boundary);

out.println("<BR><BR>");

out.println("<BR><BR>");

int pos;

pos = file.indexOf("filename=\"");

pos = file.indexOf("\n", pos) + 1;

pos = file.indexOf("\n", pos) + 1;

pos = file.indexOf("\n", pos) + 1;

int boundaryLocation = file.indexOf(boundary, pos) - 4;

int startPos = ((file.substring(0, pos)).getBytes()).length;

int endPos =

((file.substring(0, boundaryLocation)).getBytes()).length;

String data = file.substring(startPos, endPos);

out.println(data);

out.println("<BR><BR>");

out.println("<BR><BR>");

FileOutputStream fileOut = new FileOutputStream(saveFile);

//fileOut.write(dataBytes);

fileOut.write(data.getBytes());

fileOut.flush();

fileOut.close();

out.println("File saved as " + saveFile);

}

%>

out.println(dataBytes.toString()); - has a value gif89@??????????????????????, and "?" symbols doesnt writed to database.

Former Member
0 Kudos

Hi,

Instead of directly creating a string with binary data. You can download any Base64 utility class(freely available). Convert the binary data to String using above utility and save that data and while retriving back you can call reverse to display binary data.

regards,

Kiran

former_member182372
Active Contributor
0 Kudos

Hi Nikita,

Why do you trying to convert byte[] to string? Use byte[] as type for one of your property on entity bean.

Best regards, Maksim Rashchynski.