0

I have made a code to save the image on server folder but it runs on localhost. Now I want to save it on my company's server so it should get the real path on it while browsing. Below is my code which saves the image path in database and image at path given in Code.

enter code here
<%@page import="sqlCon.ConnectorClass"%>
<%@page import="java.sql.*"%>
<%@page import="java.io.*"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.*"%>


<%
    final String UPLOAD_DIRECTORY = "S:/Eclipse neon 2/PhotoWall /WebContent/img/image_data/";

    //process only if its multipart content
    if(ServletFileUpload.isMultipartContent(request))
    {
        try 
        {
            List<FileItem> multiparts = new ServletFileUpload(new  DiskFileItemFactory()).parseRequest(request);

            for(FileItem item : multiparts)
            {

                if(!item.isFormField())
                {              
                    String name = new File(item.getName()).getName();
                    item.write( new File(UPLOAD_DIRECTORY + File.separator + name));

                    Connection con = ConnectorClass.conn();

                    String storage_path ="img/image_data/"+name;

                    String qry="insert into file values('"+storage_path+"',"+(int)session.getAttribute("User_ID")+")";

                    //System.out.println(UPLOAD_DIRECTORY + "/" + name);

                    PreparedStatement ps = con.prepareStatement(qry);
                    //ps.setString(1,UPLOAD_DIRECTORY + File.separator + name);
                    //ps.setInt(2,(int)session.getAttribute("User_ID"));
                    ps.executeUpdate(qry);

                    qry="update user set image_c="+((int)session.getAttribute("Image_c")+1)+" where id="+(int)session.getAttribute("User_ID");
                    ps = con.prepareStatement(qry);
                    //ps.setInt(1,(int)session.getAttribute("Image_c")+1);
                    //ps.setInt(2,(int)session.getAttribute("User_ID"));
                    ps.executeUpdate(qry);
                    session.setAttribute("Image_c",(int)session.getAttribute("Image_c")+1);
                }
            }

           //File uploaded successfully
           request.setAttribute("message", "File Uploaded Successfully...Check in "+UPLOAD_DIRECTORY);
        } catch (Exception ex) 
        {
           request.setAttribute("message", "File Upload Failed due to " + ex);
        }          

    }
    else
    {
        request.setAttribute("message",
                             "Sorry this Servlet only handles file upload request");
    }

    request.getRequestDispatcher("/result.jsp").forward(request, response);


  %>
3
  • This is great ! I'm happy it works! ;D Commented Jun 26, 2017 at 12:09
  • Have a look at <context-param> and <init-param> . stackoverflow.com/questions/14665037/…
    – Arnaud
    Commented Jun 26, 2017 at 12:13
  • FYI : Please see how to use Servlet and EL, this would seems complicated at first but you will notice that your code will be much more readable, Here you clearly have a jsp just to do some logic then redirect to the result page, that's a old concept. Without mentionning that you should also read about MVC, having some database access from the business class is also not the best approch
    – AxelH
    Commented Jun 26, 2017 at 13:01

0

Browse other questions tagged or ask your own question.