on 08-14-2006 1:57 PM
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[] ???
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Nikita,
Check http://jdbc.postgresql.org/documentation/80/binary-data.html
Best regards, Maksim Rashchynski.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
<%
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.
User | Count |
---|---|
83 | |
24 | |
12 | |
9 | |
7 | |
6 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.