Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从java类文件获取apache WebContent文件夹的绝对路径_Java_File_Jsp_Absolute Path - Fatal编程技术网

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

从java类文件获取apache WebContent文件夹的绝对路径,java,file,jsp,absolute-path,Java,File,Jsp,Absolute Path,需要获取java类文件中的绝对路径,在动态web应用程序中 实际上,我需要获取apache webapps文件夹的路径。。。部署webapps的位置 e、 g./apacheroot/webapps/my-deployed-app/WebContent/images/imagetosave.jpg 需要在java类文件中获取,而不是在jsp页面或任何视图页面上 有什么想法吗?如果您有javax.servlet.ServletContext,您可以调用: servletContext.getR

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

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

  • e、 g./apacheroot/webapps/my-deployed-app/WebContent/images/imagetosave.jpg

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


有什么想法吗?

如果您有javax.servlet.ServletContext,您可以调用:

servletContext.getRealPath("/images/imagetosave.jpg")
获取图像存储位置的实际路径

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

但是,您可能需要考虑使用:

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


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

您可以编写一个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中对其进行配置

<listener>
    <listener-class>my.package.MyServletContextListener</listener-class>
</listener>

my.package.MyServletContextListener

我能够在过滤器中获得对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:

<filter>
    <filter-name>MyFilter</filter-name>
    <filter-class>com.myapp.servlet.MyFilter</filter-class>
    <init-param>
        <param-name>templatepath</param-name>
        <param-value>/templates</param-value>
    </init-param>
</filter>    
<filter-mapping>
    <filter-name>MyFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

我的过滤器
com.myapp.servlet.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
}
实际上,我需要获取apache webapps文件夹的路径。。。部署webapps的位置

e、 g./apacheroot/webapps/my-deployed-app/WebContent/images/imagetosave.jpg

正如许多其他答案所提到的,您可以使用将相对web内容路径转换为绝对磁盘文件系统路径,以便可以在
文件
文件输入流
中进一步使用它。
ServletContext
位于可通过继承方法获得的servlet中:

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

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

String uploadsFolder = getItFromConfigurationFileSomehow(); // "/var/webapp/uploads"
File file = new File(uploadsFolder, "imagetosave.jpg");
// ...
另见:
方法-1: //步骤1:导入java.net.InetAddress

InetAddress ip = InetAddress.getLocalHost();
//步骤2:提供您的文件路径

String filepath="GIVE YOUR FILE PATH AFTER WEB FOLDER something like /images/grid.png"
//第三步:把所有的东西都拿在一起

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;
我的建议
  • 将要打开的文件保留在源文件夹的默认包中,并直接打开该文件,以使事情变得简单明了。
    注意:之所以会发生这种情况,是因为它存在于IDE的类路径中。如果您在没有IDE的情况下编码,则将其保存在java编译的类文件中,或者保存在您可以访问的公共文件夹中

  • 玩得开心

    救了我的命!谢谢在这种情况下,浏览器是否能够异步下载图像?对于WebContent,我们可以简单地将img src=“”?这里有什么?@params:请看最后一个“也看”链接。我做了,但在我的评论之后:准确可靠的答案。谢谢
    String path = request.getRequestURL().toString();
    
    path = path.substring(0, path.indexOf(request.getContextPath()));
    
    String finalPath = "GIVE YOUR FILE PATH AFTER WEB FOLDER something like /images/grid.png"
    path +=finalPath;