java – 如何从Web服务方法访问WebContent文件夹
发布时间:2020-12-15 02:32:39 所属栏目:Java 来源:网络整理
导读:我想从同一项目中的Web服务中的方法到达WebContent文件夹中的文件.例如: @WebMethodpublic String test() { File configFile = new File("config.xml"); return configFile.getAbsolutePath();} 它返回“/usr/share/glassfish3/glassfish/domains/domain1/c
我想从同一项目中的Web服务中的方法到达WebContent文件夹中的文件.例如:
@WebMethod public String test() { File configFile = new File("config.xml"); return configFile.getAbsolutePath(); } 它返回“/usr/share/glassfish3/glassfish/domains/domain1/config/config.xml”.我想到目录“/usr/share / glassfish3 / glassfish / domains / domain1 / applications / my_project_name /”文件夹中的文件.我该怎么做到? 解决方法
将以下参数添加到Web服务类:
@Context ServletContext context; 然后,假设您的config.xml文件位于WebContent文件夹中,您可以通过调用方法context.getRealPath(String)来获取其绝对路径.使用您的示例代码将是: @WebMethod public String test() { File configFile = new File(context.getRealPath("config.xml")); return configFile.getAbsolutePath(); } 或直接,不经过File对象: @WebMethod public String test() { return context.getRealPath("config.xml"); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |