Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Convert Raw Binary String to InputStream

843842Oct 22 2008 — edited Oct 24 2008
Hi i want to upload a {color:#003366}JPG image file{color} in a {color:#003366}BLOB{color} field in database. At first i was using {color:#003366}AJA{color}X to send file {color:#003366}path{color} to the {color:#003366}servlet{color} creating a {color:#003366}file Object{color} from it, then {color:#003366}FileInputStream{color} from it, and finally passing it to {color:#003366}preparedstatement.setBinaryStream({color}) method. It worked great. But since{color:#003366} Firefox 3{color} stopped sending full path to database i have to use another method.
So instead of sending image path i used {color:#ff0000}nsIDOMFile{color} property of Firefox 3. It has a method {color:#003366}getAsBinary(){color} which Returns a DOMString containing the file's data in {color:#003366}raw binary{color} format. Now i access this {color:#003366}string{color} from servlet using{color:#003366} request.getParameter(){color} method which creates a {color:#003366}String containing bit code of the image file{color} (I suppose). Now how to convert this string to {color:#003366}InputStream{color} so that i can store this in BLOB field in database using prepared statement.
Right now I am using following code but it doesn't seem to work.
String image=request.getParameter("image");
    try{
      InitialContext context = new InitialContext();
      dsource = (DataSource)context.lookup("java:comp/env/jdbc/DataSource");
      con = dsource.getConnection();
      ps = con.prepareStatement("SOME STATEMENT");
      ByteArrayInputStream imagestream = new ByteArrayInputStream(image.getBytes()); 
      ps.setBinaryStream(1, imagestream, imagestream.available());
      int i = ps.executeUpdate();
Edited by: Wonder01 on Oct 22, 2008 12:24 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 21 2008
Added on Oct 22 2008
6 comments
330 views