11 Replies Latest reply: Aug 12, 2008 7:55 AM by 843785 RSS

    Hosting images on a server

    843785
      Is there a database like mySQL in which I can store images on a server and pull it off as needed?

      Thanks.
        • 1. Re: Hosting images on a server
          843785
          Why not use MySql? For a database like MySql try the Derby database that is part of Java 1.6

          An image is a just a whole lot of bytes stored in the database along with any other information you want to associate with the image. In this respect images aren't special, so use whatever database suits you.

          Edited by: pbrockway2 on Aug 11, 2008 12:27 PM
          • 2. Re: Hosting images on a server
            843785
            How would I use my SQL? Isn't there too many bytes to fit in the tables?
            • 3. Re: Hosting images on a server
              843785
              Read the [MySql docs|http://dev.mysql.com/doc/refman/5.1/en/index.html] specifically the BLOB type.
              • 4. Re: Hosting images on a server
                843785
                Krytical wrote:
                How would I use my SQL? Isn't there too many bytes to fit in the tables?
                I've not looked in the structures on MySQL, at least not since I asked them about it handling our 3/4 TB DB with 100's of millions of records in each table--and they didn't respond, but even your data or quantity of data gets too big, you can always start another table, or if data structures are not large enough, you can always segment the data and store it in multiple records. I had to do this back int he olden days when that database the company I was working for would not handle thing like blobs and clobs. Just number each one of the segments and pull them back in order.
                • 5. Re: Hosting images on a server
                  843785
                  The below is SQL Server 2000 Code but with sum minor changes will work in MySQL
                  CREATE TABLE TBL_IMAGE
                  (
                  [ID] INT NOT NULL IDENTITY PRIMARY KEY,
                  [NAME] VARCHAR(256) NOT NULL,
                  [DATA] IMAGE NOT NULL
                  )
                  GO
                  public void addImage(String imageName, byte[] imageData) throws Exception{
                    PreparedStatement prep = conn.prepareStatement("INSERT INTO [TBL_IMAGE] VALUES(?,?)");
                    prep.setString(1,imageName);
                    prep.setBytes(2,imageData);
                    prep.execute();
                  }
                  public byte[] getImage(String imageName) throws Exception{
                    PreparedStatement prep = conn.prepareStatement("SELECT [DATA] FROM TBL_IMAGE WHERE [NAME] = ?");
                    prep.setString(1,imageName);
                    ResultSet rs = prep.executeQuery();
                    rs.next();
                    return rs.getBytes(1);
                  }
                  • 6. Re: Hosting images on a server
                    843785
                    Goto [Balusc's ImageServlet|http://balusc.blogspot.com/2007/04/imageservlet.html] to see how to use the image data
                    Replace
                    input = new BufferedInputStream(new FileInputStream(imageFile));
                    with
                    input = new BufferedInputStream(new ByteArrayInputStream(imageData));
                    Where imageData is the byte[] recieved from the database.
                    • 7. Re: Hosting images on a server
                      843785
                      Alternatively, why not just host the images in a web server, and keep the URLs in a database?
                      • 8. Re: Hosting images on a server
                        843785
                        georgemc wrote:
                        Alternatively, why not just host the images in a web server, and keep the URLs in a database?
                        georgemc,

                        This is a good solution, I've done this exact solution before and it works marvelously!

                        Les
                        • 9. Re: Hosting images on a server
                          843785
                          Yeah the URL thing sounds good but how would I upload the image from Java?

                          Edit: What I mean is that the images are not predetermined, the user selects them.
                          • 10. Re: Hosting images on a server
                            DrClap
                            Selects them from what?
                            • 11. Re: Hosting images on a server
                              843785
                              Krytical wrote:
                              Yeah the URL thing sounds good but how would I upload the image from Java?
                              Edit: What I mean is that the images are not predetermined, the user selects them.
                              I think most of the time the user will select what images to up/download.(Dumb)

                              Sounds like you want the user to upload the image/s to the server and the server stores them.
                              Then have the user/s be able to download them later.

                              If I understand you correctly (I am taking a big guess here) at what you are trying to do.
                              This is Web based, so you'll JSP or Servlets.
                              You will use an HTML <form> Object:
                              Eg: UploadImage.html
                               
                              <FORM Action="uploader" Enctype="multipart/form-data" Method="post"> 
                                <INPUT Type="file" Name="imageData"/> 
                                <INPUT ........ 
                                .... 
                                .... 
                                .... 
                                <INPUT Type="submit" Value="Send Data"/> 
                              </FORM> 
                              When the form is submitted the data will be sent to the uploader.java Servlet
                              Inside the uploader.java you will get a timestamp value,
                              the form data will be stored in the database along with the timestamp
                              value(for URL).
                              The image data will be written to the Physical Drive with the timestamp value as the file name.

                              The web page will display certain data from within the database to the user
                              Eg: DownloadImage.jsp
                               
                              HashMap<Integer,String> data //Get Images Name and PK_ID from database 
                              Set<Integer> keys = //get the keyset from data 
                              Iterator it = //get the iterator of the set 
                              while(it.hasNext()){ 
                                int key = (int)it.next(); 
                                out.println("<a href=\"imageLoader?ref=" + key + "\">" + data.get(key) + "</a><br/>"); 
                              } 
                              In the imageLoader.java Servlet you will retrieve the ref GET value and parse that to a SQL Query
                              to retrieve the image URL so that the image can be downloaded.
                              ------------
                              I don't know if the procedure to retrieve the data from type file is the same as from type Text or Password
                              but the Enctype sould be correct as this is what they say should be used

                              Hope this helps
                              PS - Please explain what it is you are doing as it will make it easy'r for others to help