1

I want to upload an image to a disk drive location in future it may be server drive location and it will display the same on a webpage using file path location that should be fetched from the database.

I tried to upload the images to the project directory, but the image file is not getting uploaded. However, when I refresh the project directory it will appear there.

The path I provided is a project location directory.

@MultipartConfig

//@WebServlet("/NewAddProject")

public class NewAddProject extends HttpServlet {


private File file;

boolean flag1 = false, flag2 = false, flag3 = false;
String p_id;
String img_sr_no;
String project_name = null;
String project_type = null;
String project_location = null;
String project_photo = null;
String project_ststus = null;
javax.servlet.http.Part img1part;
Part imgpart;
String imgName = null;
// String pdfName = null;
String content_type1;
String store_id;
String created_date;
PreparedStatement pst;
int i;
/**
 * @see HttpServlet#HttpServlet()
 */
public NewAddProject() {
    super();
    // TODO Auto-generated constructor stub
}
public void init() {
    myOldFilePath = getServletContext().getInitParameter("file-upload");
    if (!new File(myOldFilePath).exists()) {
        (new File(myOldFilePath)).mkdir();
    }
    filePath = myOldFilePath + "\\" + "NewProjectDetails" + "\\";
    File directory1 = new File(filePath);
    if (directory1.exists()) {} else {
        directory1.mkdir();
    }
    System.out.println(filePath);
}


/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
 *      response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    java.io.PrintWriter out = response.getWriter();

    String imagePath = "D:\\JAVA PROJECTS 2019\\RealValueProperties\\RealValueProperties_04_03_2019\\RealValueProperties\\WebContent\\projectimages\\images\\img";

    String absolutePathToImages = request.getSession().getServletContext().getRealPath("/projectimages/");
    final String DIRECTORY_PATH = absolutePathToImages + "images" + File.separator + "";
    project_name = request.getParameter("project_name");
    System.out.println(project_name);
    project_type = request.getParameter("property_type");
    System.out.println(project_type);
    project_location = request.getParameter("project_location");
    System.out.println(project_location);
    project_ststus = request.getParameter("project_ststus");
    System.out.println(project_ststus);
    imgpart = request.getPart("project_image"); // for image

    if (imgpart != null) {
        try {
            imgName = imgpart.getSubmittedFileName();
            imgName = GetImageID.getImageID() + imgName;
            System.out.println("image name" + imgName);
            flag1 = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    System.out.println("path: " + filePath);

    imgpart.write(imagePath + imgName);

    //      imgpart.write(new File(DIRECTORY_PATH) + File.separator + "img" + File.separator + imgName);
    System.out.println("uploaded : " + DIRECTORY_PATH);
    try {
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("Driver loaded...");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/realvalueproperties", "root", "");
        System.out.println("Connected...");
        String query2 = "INSERT into addnewproject ( p_id, project_name, project_type, " +
            "project_location, project_image,project_ststus)" + "values(?,?,?,?,?,?)";
        PreparedStatement pst2 = conn.prepareStatement(query2);
        pst2.setString(1, null);
        pst2.setString(2, project_name);
        pst2.setString(3, project_type);
        pst2.setString(4, project_location);
        //          pst2.setString(5, filePath + imgName);
        pst2.setString(5, "projectimages/images/img" + imgName);
        pst2.setString(6, project_ststus);
        int i = pst2.executeUpdate();
        if (i > 0) {
            System.out.println("Insered to dbt ");
            response.sendRedirect("add_new_projects.jsp");
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            conn.close();
            // response.sendRedirect("student_home.jsp");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
}

I expect that the image file should be uploaded to the particular drive location. It may be local or server drive location. That path should be stored in a database and displayed the same on the web page.

Vikrant
  • 4,920
  • 17
  • 48
  • 72
Sach_in
  • 11
  • 1
  • 2
  • 1
    What do you mean the file appears after refresh? You mean your IDE is not picking it up without refreshing? That does not mean file is not being saved, go check the folder yourself. – Cray Apr 09 '19 at 05:06
  • the file is there in folder but the path i saved to database is from project directory and i am fetching that path so there is no image in project directory after refreshing the image file appear there – Sach_in Apr 09 '19 at 05:16
  • So your fetch works after you refresh? What are you refreshing exactly – Cray Apr 09 '19 at 05:21
  • the project directory or folder in project directory i am using eclipse ide – Sach_in Apr 09 '19 at 05:26
  • Your IDE might not update the folder's contents, but as the file gets saved your fetch function should be able to pick it up again regardless of what IDE displays. So does your fetch work at all or no? – Cray Apr 09 '19 at 05:30

1 Answers1

-1

enter image description here

All you need to do is open eclipse then go to Windows Tab click Preferences then expand General then click on Workspace then check :

  • Refresh using native hooks and pooling
  • Refresh on Access

click Apply then ok