Jsp 如何将图像文件上载到特定驱动器位置并存储到数据库的路径

Jsp 如何将图像文件上载到特定驱动器位置并存储到数据库的路径,jsp,tomcat,servlets,server,upload,Jsp,Tomcat,Servlets,Server,Upload,我想上传一个图像到一个磁盘驱动器的位置,在未来它可能是服务器驱动器的位置,它将显示在网页上使用的文件路径位置,应该从数据库中获取相同的位置 我试图将图像上载到项目目录,但图像文件无法上载。但是,当我刷新项目目录时,它将出现在那里 我提供的路径是一个项目位置目录 @MultipartConfig //@WebServlet("/NewAddProject") public class NewAddProject extends HttpServlet { private File file

我想上传一个图像到一个磁盘驱动器的位置,在未来它可能是服务器驱动器的位置,它将显示在网页上使用的文件路径位置,应该从数据库中获取相同的位置

我试图将图像上载到项目目录,但图像文件无法上载。但是,当我刷新项目目录时,它将出现在那里

我提供的路径是一个项目位置目录

@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();
        }
    }
}
}
我希望图像文件应该上传到特定的驱动器位置。它可能是本地或服务器驱动器位置。该路径应存储在数据库中,并在网页上显示相同的路径

您所需要做的就是打开eclipse 然后转到Windows选项卡,单击首选项 然后展开“常规”,然后单击“工作区” 然后检查:

使用本机挂钩和池刷新 访问时刷新
单击“应用”,然后单击“确定”

刷新后显示文件是什么意思?你的意思是你的IDE在没有刷新的情况下就无法获取它?这并不意味着文件没有保存,请自己检查文件夹。文件在文件夹中,但我保存到数据库的路径来自项目目录,我正在获取该路径,因此刷新后项目目录中没有图像。图像文件显示在文件夹中,因此刷新后您的获取工作正常?你正在刷新什么我正在使用eclipse IDE的项目目录或项目目录中的文件夹IDE可能不会更新文件夹的内容,但当文件保存后,无论IDE显示什么,您的获取功能都应该能够再次拾取它。那么你的取回工作到底有没有?