Jakarta ee 如何在JavaEE应用程序中获取图像文件夹的相对路径?

Jakarta ee 如何在JavaEE应用程序中获取图像文件夹的相对路径?,jakarta-ee,Jakarta Ee,我正在将用户图像上传到我的注册表中。我想将其保存在图像文件夹中。如果给定绝对路径,我的应用程序将正常工作,但如何给出相对路径。 这是我的密码。 //chck如果是多部分内容*// if(!ServletFileUpload.isMultipartContent(request))//if1 在JavaEE应用程序中使用相对路径并不容易,因为有时它们可能引用IDE的根文件夹,并且当每个JSP文件被编译到IDE或Tomcat的工作目录中的Java文件时,它们可能会使用该路径作为相对路径 在Eclip

我正在将用户图像上传到我的注册表中。我想将其保存在图像文件夹中。如果给定绝对路径,我的应用程序将正常工作,但如何给出相对路径。 这是我的密码。 //chck如果是多部分内容*// if(!ServletFileUpload.isMultipartContent(request))//if1


在JavaEE应用程序中使用相对路径并不容易,因为有时它们可能引用IDE的根文件夹,并且当每个JSP文件被编译到IDE或Tomcat的工作目录中的Java文件时,它们可能会使用该路径作为相对路径

在Eclipse中编写代码时,我遇到了同样的问题

这个问题的解决方案是将web.xml中的绝对路径编码为上下文参数(或)init参数,如果您将来想更改目录,则只需更改一个位置

     {
           request.setAttribute("error", "No file is selected for upload");
           rd=request.getRequestDispatcher("error.jsp");       //dispatching to error page if no file is sent via request
           rd.forward(request,response);

        }  //end of if1


     //****Setting space and path where file will be uploaded on server*****//
     DiskFileItemFactory factory = new DiskFileItemFactory();    //object of class defined in commons package
     factory.setSizeThreshold(THRESHOLD_SIZE);
     factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
     ServletFileUpload upload = new ServletFileUpload(factory);
     upload.setFileSizeMax(MAX_FILE_SIZE);
     upload.setSizeMax(REQUEST_SIZE);
      String uploadPath = "c://"+ File.separator + UPLOAD_DIRECTORY;
       // creates the directory if it does not exist
      File uploadDir = new File(uploadPath);
      userbean.setUserimage(uploadPath);
          if (!uploadDir.exists())//if2
          {
                uploadDir.mkdir();

            }  //end of if2





          //*******check the type of form and process accordingly*****//

            try 
              {                //try1

                List formItems = upload.parseRequest(request);// parses the request's content to extract file data
                Iterator iter = formItems.iterator();        

                //******* iterates over form's fields**********//

                while (iter.hasNext()) //while1
                {
                    FileItem item = (FileItem) iter.next();



       // ********processes only fields that are not form fields*******//
                    if (!item.isFormField()) //if3
                    {
                        String fileName = new File(item.getName()).getName();
                        String filePath = uploadPath + File.separator + fileName;
                        File storeFile = new File(filePath);

                        // saves the file on disk
                        item.write(storeFile);
                        fileuploaded=true;

                    }//end of if3