Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Apache JSP ImageProcessing , +
I am building an application that extracts the feature of an image and saves it to the database (using JSP, Java & PostgreSQL ).
 
I have based it on Caliph & Emir website. ( http://www.semanticmetadata.net/features/ )
 
I open PostgreSQL and the database it being automatically created. I created this project in NetBeans. I have loaded the Apache Tomcat plugin into NetBeans and I am running Apache Tomcat ( Catalina ) from cmd as well (even though I don't know if it is necessary ).
 
The thing is though that when the interface through my project opens, I can load an image through it but then despite getting a success comment out of it
 
( Successfully uploaded image "734961_10151351065770042_1001776232_n.jpg" at path "bla1/bla2/Project1/Me/WebApp/data/img/".
 
Successfully added image "bla1/bla2/Project1/Me/WebApp/data/img/".734961_10151351065770042_1001776232_n.jpg" at database.)
 
the images' features wont load into database.
 
This is manager_op.jsp code (manage operation) If an additional code excerpt is needed tell me and I will give it to you.
 
Because it is already too big, I will only place manager_op.jsp because I believe that the mistake is somewhere in here. But then again, I may be wrong. It may be an Apache issue (some setting that I didn't do or something else - and I think that is quite possible). Help me if you can. Thanks in advance.
 

<pre lang="xml"><%@page import="java.sql.Statement"%>
<%@page import="org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload" %>
<%@page import="org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory" %>
<%@page import="java.io.*,java.util.*, javax.servlet.*" %>
<%@page import="javax.servlet.http.*" %>
<%@page import="org.apache.tomcat.util.http.fileupload.*" %>
<%@page import="org.apache.tomcat.util.http.fileupload.disk.*" %>
<%@page import="org.apache.tomcat.util.http.fileupload.servlet.*" %>
 
<%@page contentType="text/html" pageEncoding="UTF-8"%>
 
<!DOCTYPE html>
<jsp:useBean id="mdbm" class="mdb.MManager" scope="session"/>
<html>
    <head>
        <title>Image Retrieval System</title>
        <link rel='stylesheet' type='text/css' href='base.css'>
    </head>
    <body>
        <%@include file="path_handler.jsp" %>
 
        <table class="main" cellspacing="0px">
            <tr>
                <td class="title" colspan="4">Image Retrieval System</td>
            </tr>
            <tr>
                <td class="menu" width="200px">
                    <a href="main.jsp">Main</a>
                </td>
                <td class="menu_active" width="200px">
                    <a href="manage.jsp">Image Management</a>
                </td>
                <td class="menu" width="200px">
                    <a href="query.jsp">Query images</a>
                </td>
                <td class="menu" width="*"></td>
            </tr>
            <tr>
                <td class="content_q" colspan="4">
                    <table class="tab" cellspacing="0px">
                        <tr>
                            <td width="240px">
                                <p class="tab_title">Manage operation results</p>
                                <div class="q_opt">
                                    <%
                                        if (request.getParameter("upload") != null) {
                                            String sUploadFName = "";
                                            int r = 0;
                                            File file;
                                            // Verify the content type
                                            String contentType = request.getContentType();
                                            if ((contentType.indexOf("multipart/form-data") >= 0)) {
                                                DiskFileItemFactory factory = new DiskFileItemFactory();
                                                // Create a new file upload handler
                                                ServletFileUpload upload = new ServletFileUpload(factory);
                                                try {
                                                    // Parse the request to get file items.
                                                    List fileItems = upload.parseRequest(request);
                                                    if (fileItems.size() == 1) {
                                                        FileItem fi = (FileItem) fileItems.get(0);
                                                        if (!fi.isFormField()) {
                                                            // Get the uploaded file parameters
                                                            sUploadFName = fi.getName();
                                                            // Write the file
                                                            String sUploadPath = sImagesDir + sUploadFName;
                                                            // out.println(sNewFilePath);
                                                            file = new File(sUploadPath);
                                                            fi.write(file);
                                                            // insert image properties at database
                                                            r = mdbm.AddImage(sImagesDir, sUploadFName);
                                                        }
                                                    }
                                                } catch (Exception ex) {
                                                    // out.println("Error adding image<br>" + ex);
                                                }
                                            } else {
                                                // out.println("No file uploaded");
                                            }
 
                                            if (r == 0) {
                                    %>
                                    <p>
                                        <b>Successfully</b> uploaded image "<%= sUploadFName %>"
                                        at path "<%= sImagesDir %>".<br><br>
 
                                        <b>Successfully</b> added image "<%= (sImagesDir + sUploadFName) %>"
                                        at database.<br><br>
 
                                        <img class="added" src="<%= (sHtmlImagesDir+sUploadFName) %>">
                                    </p>
                                    <%
                                            } else {
                                    %>
                                    <p>
                                        Error adding image (<%= r %>).<br>
                                    </p>
                                    <%
                                            }
                                        }
                                    %>
 
                                    <%
                                        if (request.getParameter("create_db") != null) {
                                            // build database
                                            int r = mdbm.CreateTASdb();
                                            if (r == 0) {
                                    %>
                                    <p>Database has been created <b>successfully</b>.</p>
                                    <%
                                            } else {
                                    %>
                                    <p>Error creating database (<%= r %>).</p>
                                    <%
                                            }
                                        }
                                    %>
 
                                    <%
                                        if (request.getParameter("add_all_images") != null) {
                                            // build database
                                            int r = mdbm.AddAllImages(sImagesDir);
                                            if (r == 0) {
                                    %>
                                    <p>All images added to database <b>successfully</b>.</p>
                                    <%
                                            } else {
                                    %>
                                    <p>Error adding images to database (<%= r %>).</p>
                                    <%
                                            }
                                        }
                                    %>
 
                                    <%
                                        if (request.getParameter("clear_db") != null) {
                                            // drop database
                                            int r = mdbm.DropTasDB();
 
                                            if (r == 0) {
                                    %>
                                    <p>Database cleared <b>successfully</b>.</p>
                                    <%
                                            } else {
                                    %>
                                    <p>Error clearing database (<%= r %>).</p>
                                    <%
                                            }
                                        }
                                    %>
                                    <p>Return to <a class="link1" href="manage.jsp">manage</a> page.</p>
                                </div>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td class="footer" colspan="4">
                    <p class="footer_title">About</p>
                    <p class="center">
                        blablabla
                        <br><br>
                        Postgraduate students:
                        <br>blabla
                        <br>blablabla
                    </p>
                </td>
            </tr>
        </table>
    </body>
</html>
 

 

Below is the code for addImage() and addAllImages() in my java file.
 

public int AddImage(String targetPath, String mFilename)
 {
     int r;
 
     CloseConn();
     r = ConnToDb();
     if (r != 0) return r;
 
     try {
         _InsertImage(targetPath, mFilename);
     } catch (Exception ex) {
         r = -10;
     }
 
     CloseConn();
     return r;
 }
 
public int AddAllImages(String sImagesPath)
 {
     int r;
 
     CloseConn();
     r = ConnToDb();
     if (r != 0) return r;
 
     try {
         String sFilename;
         File folder = new File(sImagesPath);
         File[] listOfFiles = folder.listFiles();
 
         // determine number of images to load

         int iCount = 0;
         if ((iCount <= 0) || (iCount > listOfFiles.length))
             iCount = listOfFiles.length;
 
         // get each image from dir and add to db

         for (int i = 0; i < iCount; i++) {
             if (listOfFiles[i].isFile()) { // only files
                 // get image filename
                 sFilename = listOfFiles[i].getName();
                 // WriteLog("GetImagePropertiesFromPath: image file " + sFilename);
                 // load image
                 _InsertImage(sImagesPath, sFilename);
             }
         }
     } catch (Exception ex) {
         r = -10;
     }
 
     CloseConn();
     return r;
 }
Posted 23-Jan-13 11:53am
Edited 24-Jan-13 1:51am
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,680
2 CPallini 4,850
3 George Jonsson 3,454
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 24 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100