0

I want to upload an image file in my project directory folder named Images and store the image name in a database.

The file name has been saved in the database and my image retrieved by the browser properly, but when I check my directory folder named Images it is empty.

My questions is: Where is my file stored and why can't I see the image file in my folder?

This is my Index.jsp file

<%-- 
    Document   : index
    Created on : Mar 15, 2018, 7:30:15 PM
    Author     : Lenovo
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

        <form name="form1" method="post" enctype="multipart/form-data" action="insertimage.jsp">

        <p>
        <input type="file" name="ImageFile" id="ImageFile" />
        </p>
        <p>
        <input type="submit" name="submit" value="submit" />
        </p>
        </form>

    </body>
</html>

This is my insertimage.jsp file

<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="java.util.*, java.io.*" %>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.util.List"%>
<%@ page import="java.io.File"%>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="getcon.jsp"%> <!-- to connect a database-->

<%
try
{
    String ImageFile="";
    String itemName = "";
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (!isMultipart)
    {
    }
    else
    {
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        List items = null;
        try
        {
            items = upload.parseRequest(request);
        }
        catch (FileUploadException e)
        {
            e.getMessage();
        }

        Iterator itr = items.iterator();
        while (itr.hasNext())
        {
            FileItem item = (FileItem) itr.next();
            if (item.isFormField())
            {
                String name = item.getFieldName();
                String value = item.getString();
                if(name.equals("ImageFile"))
                {
                    ImageFile=value;
                }
            }
            else
            {
                try
                {
                    itemName = item.getName();
                    File savedFile = new File(config.getServletContext().getRealPath("/") + "Images\\" + itemName);
                    out.println("File Uploaded.." + itemName);
                    item.write(savedFile);
                }
                catch (Exception e)
                {
                    out.println("Error" + e.getMessage());
                }
            }
        }
        try
        {
            st.executeUpdate("insert into test(image) values ('" + itemName + "')");
        }
        catch(Exception el)
        {
            out.println("Inserting error" + el.getMessage());
        }
    }
}
catch (Exception e)
{
    out.println(e.getMessage());
}
%>

This is my retrieveimage.jsp file

<%@ include file="getcon.jsp"%>
<html>
    <head>
        <title>View Image Page</title>
    </head>
    <body>
        <table width="100%" border="0">
<!-- main content -->
<%
    ResultSet rs=null;
    try
    {
        rs = st.executeQuery("select image from test");

        while(rs.next())
        {
%>
        <table width="70%" height="160" border="1" align="center">
            <tr>
                <!-- Mention Directory where your images has been saved-->
                <td><img src="Images/<%=rs.getString("image") %>" width="115" height="128" /></td>
            </tr>
        </table>
<%
        }
    }
    catch(Exception e)
    {
        out.print("" + e.getMessage());
    }
%>
        </table>
    </body>
</html>

This is my connection file

<%@page import="java.sql.*" %>

<%
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample", "root", "root");
Statement st = con.createStatement();
%>
Krisztián Balla
  • 19,223
  • 13
  • 68
  • 84
Saim Khan
  • 31
  • 1
  • 3
  • Did you try searching for an image name in your file system? Unsure but when you save the image to this location `config.getServletContext().getRealPath("/")` could that be a nested directory? Okay it is the root of the project. [Found it here!](https://stackoverflow.com/questions/12160639/what-does-servletcontext-getrealpath-mean-and-when-should-i-use-it) But you did not declare it in your script, unless it is defined in an import. Refer to my reference. – mcv Mar 15 '18 at 14:53

1 Answers1

0

The following has more chance of working.

File savedFile = new File(config.getServletContext().getRealPath("/Images/" + itemName));
savedFile.getParentFile().mkdirs();
...

Not sure whether the server is also Windows; on other systems path names are case-sensitive (Images with capital i).

You could provide a fixed itemName for testing.

The img tag might need to be:

<img src="/Images/ ...
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138