How to store images in database and retrieve them back to page?

Well I don't know how to store an image file to a database (say MSSQL) from the JSP and retrieve it back whenever needed and put it on the JSP page? Please help me.

I am not sure how to store images in database but what you can do is store the image into particular folder using FileOutputStream and its unique name into the database...and than retrieve it from the folder using that name retrieved from database...
<%
response.setContentType("text/html");
response.setHeader("Cache-control","no-cache");
String err = "";
String lastFileName = "";
String contentType = request.getContentType();
String boundary = "";
final int BOUNDARY_WORD_SIZE = "boundary=".length();
System.out.println("contentType --> "+contentType);
System.out.println("BOUNDARY_WORD_SIZE --> "+BOUNDARY_WORD_SIZE);
if(contentType == null || !contentType.startsWith("multipart/form-data"))
err = "Ilegal ENCTYPE : must be multipart/form-data\n";
err += "ENCTYPE set = " + contentType;
else
boundary = contentType.substring(contentType.indexOf("boundary=") + BOUNDARY_WORD_SIZE);
System.out.println("boundary --> "+boundary);
boundary = "--" + boundary;
try
ServletInputStream sis = request.getInputStream();
byte[] b = new byte[1024];
int x=0;
int state=0;
String name=null,fileName=null,contentType2=null;
java.io.FileOutputStream buffer = null;
while((x=sis.readLine(b,0,1024))>-1)
     System.out.println("************ x ********** "+x);
     String s = new String(b,0,x);
               System.out.println("************ s ********** \n"+s);
     if(s.startsWith(boundary))
     state = 0;
     System.out.println("name="+name);
     System.out.println("filename="+fileName);
     name = null;
     contentType2 = null;
     fileName = null;
     else if(s.startsWith("Content-Disposition") && state==0)
          System.out.println("-- 1 --");
          state = 1;
          System.out.println("s.indexOf(filename=) --> "+s.indexOf("filename="));
          if(s.indexOf("filename=") == -1)
               name = s.substring(s.indexOf("name=") + "name=".length(),s.length()-2);
               System.out.println("after name substring 1 "+name);
          else
               name = s.substring(s.indexOf("name=") + "name=".length(),s.lastIndexOf(";"));
               System.out.println("after name substring 2 "+name);
               fileName = s.substring(s.indexOf("filename=") + "filename=".length(),s.length()-2);
               System.out.println("fileName --> "+fileName);
               //String fileName1 = s.substring(s.indexOf("filename=") + "filename=".length(),s.length());
               //System.out.println("fileName1 -->"+fileName1);
               if(fileName.equals("\"\""))
               fileName = null;
               else
                    String userAgent = request.getHeader("User-Agent");
                    System.out.println("userAgent --> "+userAgent);
                    String userSeparator="/"; // default
                    if (userAgent.indexOf("Windows")!=-1)
                    System.out.println("test --> "+"\\");
                    userSeparator="\\";
                    if (userAgent.indexOf("Linux")!=-1)
                    userSeparator="/";
                    System.out.println("userSeparator "+userSeparator);
                    System.out.println("fileName before inserting userSeparators "+fileName);
                    fileName = fileName.substring(fileName.lastIndexOf(userSeparator)+1,fileName.length()-1);
                    System.out.println("fileName after userSeparators "+fileName);
                    if(fileName.startsWith( "\""))
                    fileName = fileName.substring( 1);
          name = name.substring(1,name.length()-1);
          System.out.println("name 2 --> "+name);
          System.out.println("final file name "+fileName);
          if (name.equals("file"))
               if (buffer!=null)
               buffer.close();
               lastFileName = fileName;
               buffer = new java.io.FileOutputStream("/Documents and Settings/sunil/Desktop/images/"+fileName);
     else if(s.startsWith("Content-Type") && state==1)
                         System.out.println("-- 2 --");
          state = 2;
          contentType2 = s.substring(s.indexOf(":")+2,s.length()-2);
          System.out.println("contentType2 --> "+contentType2);
     else if(s.equals("\r\n") && state != 3)
               System.out.println("-- 3 --");
          state = 3;
     else
          System.out.println("-- 4 --");     
          if (name.equals("file"))
          System.out.println("Final x :: "+x);     
          buffer.write(b,0,x);
}     // while closing
sis.close();
buffer.close();
}catch(java.io.IOException e)
err = e.toString();
boolean ok = err.equals("");
if(!ok)
out.println(err);
else
%>
          <SCRIPT language="javascript">
          history.back(1);
          alert('Uploaded <%=lastFileName%>');
          window.location.reload(false);
          </SCRIPT>
<%
     out.println("done");
%>
</BODY>
</HTML>
I think it will solve ur problem..

Similar Messages

  • How to Store bollean selection values and retrieve them in runtime?

    I have an array of bolleans representing a channel of a device. Now , when the user turns on the channel the selected channel array are supposed to be stored. Now when the user selects device 2 (using a ring function) the list refreshes t5he channel names and resets the bollean array to their default values and the new selection values are to be stored. Now if the user wants, he should revert back to device 1 and can review what all channels he had selected for Device 1. How can this functionality be achieved?
    Labview Learner
    Attachments:
    1.PNG ‏14 KB
    2.PNG ‏14 KB

    You need to store the boolean array somewhere.  I would likely just use a shift register
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines

  • How to store images in database using java

    hii
    how to store images in database using java..........
    Message was edited by:
    naveennaga

    I asked a similar question once and the answer I got was - Don't.
    Instead, store the images on a drive somewhere and store the path in the database.
    But I'm no expert - that's just what I was told by someone who does this kind of stuff a lot.
    Hope that helps.

  • How to store images in database

    Can anyone help me with storing images in database, problem with me is that i m designing a site in which u need to display product information and administrator can add and delete products. now how can i make that administrator can add a image to database, Can u tell me that can image itself can be stored in a database or just the path of it can be stored in a database.
    Waiting for quick reply,
    thanks

    can u plss tell me how to store the path of the image in database , do we just have to mention it as varchar field and write the path thats it or do we have to do anything else . and pls also tellme when do we have to set connections ? only at the time to retrieving the images since we are not uploading the image sin DB just mentioning their path . pls reply ASAP

  • How t store images in database

    hi
    i want coding to store images,files in database

    hi,
    You can use the BLOB data type provided by oracle to store images in the database. BLOB (Binary Large Object). To Store data into the table, we need to use the DBMS_LOB package to manipulate the data and then store it in the table.
    BFILE are used in combination with the BLOB to get the images into the database.
    Thanks, VIkram

  • I had apps from 2 different apple ids.  When I synched my ipod touch with my computer, I lost apps from other id.  It backed them up to my computer.  How do I access those apps and put them back on ipod touch?

    I had apps from 2 different apple ids on my son's ipod touch.  When I synched it to my computer, it backed the apps from other id onto my computer and deleted them from the ipod.  How do I find the apps and how do I put them back on the ipod touch?

    Apps can be re-downloaded, and you won't be charged again for a purchsased app as long as you use the same iTunes account to re-download the app that was used to purchase the app originally.
    All apps - all paid and free apps include DRM protection which is tied to the iTunes account that was used to download the apps. Apps cannot be transferred to or combined with another iTunes account.
    http://support.apple.com/kb/ht2519

  • How to place an image in database and how to retrieve and display it in the front end

    how to place an image in database and how to retrieve and display it in the front end
    and to place an image in database and retrieve the image from database using xml
    please,help me out.

    Create a table with a Long RAW Datatype column for storing the Image Column Data.
    Create the form based on the table , which by defaults the column with LONG RAW atatype to a Image Item.
    You can use Forms Built in function READ_IMAGE_FILE to read a Image file stored on the file system in to the image item.
    A save on the form saves the image in the Image item in the long raw column.

  • How to store image in the oracle database 10.2. using File Maker 10.

    Hi.,
    I want to store image, media file in the oracle database using File Maker as a front end.
    I connect Oracle using odbc from file maker. There are table and in that table there a column "pict" of blob type. but this column is not showing in the file maker.
    2. Here I can not change the data type of any column.
    Now how to store images in the oracle table using odbc or any other tool.
    Regards,
    Shyam

    I wrote an example for my students, you can find it here. It's using PHP as the front end but all you need to do is know how and leverage the stored procedures. All code is downloadable in zip files from the blog.
    http://blog.mclaughlinsoftware.com/php-programming/oracle-lob-processing/

  • To store PDF into BLOB and retrieve into OLE/OCX

    We would like to:
    1. Store a PDF file (output of a report saved in file server) into a BLOB
    column.
    2. Retrieve previously stored PDF in the BLOB column and display using either
    OLE automation or ActiveX (OCX) control.
    I read all related documents in metalink that addresses this issue however cant
    get this to work. We have Acrobat Reader 4.0. Here are some issues from the
    tests that I did:
    1. To load the PDF file the program code that uses BFILE and LOADFROMFILE does
    not work since the database is on a remote server and the PDF file is on a
    local file server. The DIRECTORY created on the database is unable to find the
    PDF file.
    2. Though I want the loading of the file to be programmatic, I tried
    right-click on the OLE container and inserted object from file. This loaded the
    file into the container (Icon on the container appeared) however I was not able
    to view the document by double-clicking. An error in Adobe occured: 'There was
    an error opening this document. A file read error occured'. My guess is the
    'Insert Object' did not insert the file properly into the BLOB.
    3. When trying to display the PDF file stored in file server (in case storing
    into BLOB does not work), I used an OCX control, right-clicked and Insert
    Object and select Adobe Control for ActiveX. The problem here is when I run the
    form, the OCX control shows up empty and I have to 'Insert Object' again in
    runtime. Once I manually insert object from runtime the GET_INTERFACE POINTER &
    SET_PROPERTY works fine is displaying the document. Is there any way to
    maintain the control property of the OCX when the form is run?
    4. Are there any workarounds and better solution to store PDFs into BLOBs and
    retrieve and display in OLE/OCX controls? Using temporary file into local drive
    is not an issue.
    5. Would I be able to make use of 'PDF.PdfCtrl.1' OLE class?
    6. Does OLE automation work for Acrobat Reader 4.0.?
    I would appreciate all the help.
    Shyam

    Im facing a similar kind of problem. I want to store and retrieve Office files, PDF and Jpegs into/from the database to view them on web in disconnected mode. Please reply as I cant find any help/documentation regarding saving BLOB data into files. I was able to store file data into BLOB, using DBMS_LOB package.
    Shahzad

  • How to store images in access

    please tell me how to store images in access

    The JDBC-ODBC Bridge?
    Or, better yet, a Google (or whatever engine) search for a real JDBC Driver for access (I do believe at least one exists).
    The JDBC tutorials and, maybe, a little Google (or whatever engine) searching for examples, should provide more than enough to get started.
    Once you have something, and it still has a problem, post it, along with a complete, and concise, description of the problem, along with all error messages, and we will help you further.

  • Problem in Retrieve Image from DB and display in the JSP page

    Hi All,
    I did one JSP Program for retriveing image from DB and display in the JSP Page. But when i run this i m getting "String Value" output. Here i have given my Program and the output. Please any one help to this issue.
    Database Used : MS Access
    DSN Name : image
    Table Name: image
    Image Format: bmp
    Output : 1973956
    Sample Program:_
    <%@ page contentType="text/html;charset=windows-1252"%>
    <%@ page import="java.io.*" %>
    <%
         try{
              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
              Connection conn = DriverManager.getConnection("jdbc:odbc:image");
              Statement st = conn.createStatement();
              ResultSet rs = st.executeQuery("SELECT images FROM image");
              String imgLen="";
              if(rs.next()){
                   imgLen = rs.getString(1);
                   out.println(imgLen.length());
              if(rs.next()){
                   int len = imgLen.length();
                   byte [] rb = new byte[len];
                   InputStream readImg = rs.getBinaryStream(1);
                   int index=readImg.read(rb, 0, len);
                   System.out.println("index"+index);
                   st.close();
                   response.reset();
                   response.setContentType("image/jpg");
                   response.getOutputStream().write(rb,0,len);
                   response.getOutputStream().flush();
         }catch(Exception ee){
              out.println(ee);
    %>
    Thanks,
    Senthilkumar S

    vishruta wrote:
    <%
    %>Using scriptlets is asking for trouble. Java code belongs in Java classes. Use a Servlet.
                   out.println(imgLen.length());Your JSP was supposed to write an image to the output and you wrote some irrelevant strings to the output before? This will corrupt the image. It's like opening the image in a text editor and adding some characters before to it.
                   byte [] rb = new byte[len];Memory hogging. Don't do that.
              out.println(ee);You should be throwing exceptions and at least printing its trace, not sending its toString() to the output.
    You may find this article useful to get an idea how this kind of stuff ought to work: [http://balusc.blogspot.com/2007/04/imageservlet.html].

  • How to store image files in oracle DB

    Hi,
    I am new to working with database.
    Please let me know how to store image files in Database using insert command.
    Thanks,
    Ramesh Yakkala.

    Hi,
    You need to create a directory object to import these files:
    Take a look on the example below:
    eg:
    CREATE TABLE MY_IMAGE_TABLE (
    ID NUMBER,
    NAME VARCHAR2(20),
    IMAGE BLOB);
    CREATE OR REPLACE DIRECTORY IMAGES AS '/tmp';
    GRANT READ, WRITE ON DIRECTORY IMAGES TO PUBLIC;
    CREATE OR REPLACE PROCEDURE load_file_to_my_table (p_file_name IN MY_IMAGE_TABLE.NAME%TYPE) AS
    v_bfile BFILE;
    v_blob BLOB;
    BEGIN
    INSERT INTO MY_IMAGE_TABLE (id, name, image)
    VALUES (1, p_file_name, empty_blob())
    RETURN doc INTO v_blob;
    v_bfile := BFILENAME('IMAGES', p_file_name);
    Dbms_Lob.Fileopen(v_bfile, Dbms_Lob.File_Readonly);
    Dbms_Lob.Loadfromfile(v_blob, v_bfile, Dbms_Lob.Getlength(v_bfile));
    Dbms_Lob.Fileclose(v_bfile);
    COMMIT;
    END;
    SQL> execute load_file_to_my_table('myfhoto.jpg');Cheers

  • Backed up my songs from itunes onto a hard drive and now my playlists in itunes are gone! How do I retrieve them back to the way it was before?

    Help! My husband backed up my songs from itunes onto a hard drive and now my playlists in itunes are gone from my library! How do I retrieve them back to the way it was before?

    Hello Nataroo007,
    Thank you for using Apple Support Communities!
    There is a file in the iTunes library folder that contains your playlist information is the iTunes Library.xml file.
    Try and find the file, usually located in one of these locations depending on your OS:
    Operating System
    Default location of iTunes Folder
    Mac OS X
    /Users/[your username]/Music
    Microsoft Windows XP
    \Documents and Settings\[your username]\My Documents\My Music\
    Microsoft Windows Vista
    \Users\[your username]\Music\
    Microsoft Windows 7
    \Users\[your username]\My Music\
    Then you can import this file to your iTunes Library like this:
    1. Choose File > Library > Import Playlist.
    2. Navigate to the "iTunes Library.xml" file
    3. Windows users: Click Open.
    If your Podcasts list in iTunes is empty after following these steps, learn how to add them back into iTunes.
    Note: information in this post comes from article:
    iTunes: How to re-create your iTunes library and playlists
    http://support.apple.com/kb/HT1451
    Cheers,
    Sterling

  • HT4859 The guy at the verizon store programmed my phone before I got the chance to restore my apps, etc. from icloud- therefore i did not get to go through the setup assistant..  How do i get my apps and app data back on my phone w/o having to go through

    The guy at the verizon store programmed my phone before I got the chance to restore my apps, etc. from icloud- therefore i did not get to go through the setup assistant..  How do i get my apps and app data back on my phone w/o having to go through and re-

    To restore from an iCloud backup you have to erase the phone and start over.  To do this, go to Settings>General>Reset, tap Erase All Content and Settings, then go through the setup assistant again and when given the option, choose Restore from iCloud Backup.  There is no other way to do this.
    Be sure you phone is connected to wifi and your charger as this is likely to take some time to finish.

  • Saving word doc in database and retrieving

    hai all,
    Is it possible to save a word file in database and retrieve the word file in web dynpro application?
    Thanks n Regards
    Sharanya.R

    Hi,
    To save the wordfile to the database make use of lob types.
    Make use of JDBC to do this.
    retreiving
    Make use of JDBC to fetch the previously saved doc.
    Populate a context of type IWDResource(Nw04s onwards) or  Binary(Nwo4).
    Set the appropriate  mime type
    Create a filedownload and bind this.
    Regards
    Ayyapparaj

Maybe you are looking for

  • Parameter for current year and previous year

    Hi all, I currently have 2 reports where the query is the same except in the where clause I have it filtered to current year and previous year based on the List_Date. I would like to make this one report with a parameter of Current or Previous year.

  • Reshape Java Array

    Is there a simple way of reshaping arrays in Java? (From A to B) From A =     1    4    7    10     2    5    8    11     3    6    9    12           ... to this: B =     1    3    5    7    9   11     2    4    6    8   10   12I've looked at the mat

  • How to  add a new System status?

    Hi, I am working on the Bid invitation process. When we process the Bid submitted, we have 'approve' , 'reject' buttons which when clicked wil set the system status to 'I1014' & I1013' respectively.these r maintained in TJ02.   I have added one more

  • Getting an error message TypeError: Components.classes['@softage.ru/skype/SkypeFfExtension;1'] is undefined

    on starting up i get this message TypeError: Components.classes['@softage.ru/skype/SkypeFfExtension;1'] is undefined i have also got other warnings from adobe issue 10 when i try to view anything

  • Repositioning edit transaction wizard buttons - Agentry

    Hi; I am working on SAP Work Manager 6.0 customizing using Agentry 6.1.3, I would like to find out, on an edit transaction screen there is the wizard buttons Finish, Done and Cancel. On a windows .NET cleint, these show at the bottom of the screen an