从java类文件中获取apache webcontents文件夹的绝对路径

需要在动态Web应用程序内的java类文件中获取绝对路径…

  • 实际上我需要获得apache webapps文件夹的路径…部署webapps的位置

  • 例如/apache-root/webapps/my-deployed-app/WebContent/images/imagetosave.jpg

  • 需要在java类文件中获取它,而不是在jsp页面或任何视图页面上…

有任何想法吗?

实际上我需要获得apache webapps文件夹的路径…部署webapps的位置

例如/apache-root/webapps/my-deployed-app/WebContent/images/imagetosave.jpg

正如许多其他答案所提到的,您可以使用ServletContext#getRealPath()将相对Web内容路径转换为绝对磁盘文件系统路径,以便您可以在FileFileInputStream进一步使用它。 ServletContext是由inheritance的getServletContext()方法提供的servlet:

 String relativeWebPath = "/images"; String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath); File file = new File(absoluteDiskPath, "imagetosave.jpg"); // ... 

但是 ,文件名“imagetosave.jpg”表示您正在尝试通过FileOutputStream存储上载的图像。 公共webcontent文件夹是存储上传图像的错误位置! 无论何时重新部署webapp,或者甚至在服务器重新启动并进行清理时,它们都会丢失。 原因很简单,上传的图像根本不包含在待部署的WAR文件中。

您绝对应该在webapp部署文件夹之外寻找另一个位置,作为上传图像的更永久存储,以便在多个部署/重新启动时保持完整。 最好的方法是准备一个固定的本地磁盘文件系统文件夹,例如/var/webapp/uploads并将其作为一些配置设置提供。 最后只需将图像存储在那里。

 String uploadsFolder = getItFromConfigurationFileSomehow(); // "/var/webapp/uploads" File file = new File(uploadsFolder, "imagetosave.jpg"); // ... 

也可以看看:

  • servletcontext.getRealPath(“/”)是什么意思,什么时候应该使用它
  • 在基于servlet的应用程序中放置位置以及如何读取配置资源文件?
  • 在Java Web应用程序中从应用程序服务器外部提供静态数据的最简单方法

如果你有一个javax.servlet.ServletContext,你可以调用:

 servletContext.getRealPath("/images/imagetosave.jpg") 

获取图像存储位置的实际路径。

可以从javax.servlet.http.HttpSession访问ServletContext。

但是,您可能希望使用以下内容:

 servletContext.getResource("/images/imagetosave.jpg") 

要么

 servletContext.getResourceAsStream("/images/imagetosave.jpg") 
 String path = MyClass.getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); 

这应该根据类的文件位置返回绝对路径。

我能够在Filter中获得对ServletContext的引用。 我最喜欢这种方法的是它发生在init()方法中,该方法在第一次加载Web应用程序时调用,这意味着执行只发生一次。

 public class MyFilter implements Filter { protected FilterConfig filterConfig; public void init(FilterConfig filterConfig) { String templatePath = filterConfig.getServletContext().getRealPath(filterConfig.getInitParameter("templatepath")); Utilities.setTemplatePath(templatePath); this.filterConfig = filterConfig; } 

我通过web.xml将“templatepath”添加到filterConfig:

  MyFilter com.myapp.servlet.MyFilter  templatepath /templates    MyFilter /*  

您可以在控制器中获取路径:

 public class MyController extends MultiActionController { private String realPath; public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { realPath = getServletContext().getRealPath("/"); String classPath = realPath + "WEB-INF/classes/" + MyClass.ServletContextUtil.class.getCanonicalName().replaceAll("\\.", "/") + ".java"; // add any code } 

方法-1:

// step1:import java.net.InetAddress;

 InetAddress ip = InetAddress.getLocalHost(); 

// step2:提供您的文件路径

 String filepath="GIVE YOUR FILE PATH AFTER WEB FOLDER something like /images/grid.png" 

// step3:抓住所有的peices

 String a ="http://"+ip.getHostAddress()+":"+request.getLocalPort()+""+request.getServletContext().getContextPath()+filepath; 

方法-2:

//步骤:1 – 获取绝对url

 String path = request.getRequestURL().toString(); 

//步骤:2 – 然后使用上下文路径对其进行子字符串

 path = path.substring(0, path.indexOf(request.getContextPath())); 

//步骤:3 – 在web文件夹后提供文件路径

 String finalPath = "GIVE YOUR FILE PATH AFTER WEB FOLDER something like /images/grid.png" path +=finalPath; 

我的建议

  1. 保留要在源文件夹的默认包中打开的文件,并直接打开文件以使事情简单明了。
    注意:发生这种情况是因为它存在于IDE的类路径中,如果您在没有IDE的情况下进行编码,然后将其保存在您的Java编译类文件的位置或者您可以访问的公共文件夹中。

玩得开心

你可以写一个ServletContextListener:

 public class MyServletContextListener implements ServletContextListener { public void contextInitializedImpl(ServletContextEvent event) { ServletContext servletContext = event.getServletContext(); String contextpath = servletContext.getRealPath("/"); // Provide the path to your backend software that needs it } //... } 

并在web.xml中配置它

  my.package.MyServletContextListener