2011-11-01 69 views
1

我有一个运行我的CXF-based webapp的tomcat。Tomcat:如何访问Apache CXF-webapp中的本地文件?

方法我一起工作的有签名这样的:

@GET 
@Path ("/handler/contract/{ItemId}") 
@Produces ("application/xml") 
public Item getItem(@PathParam("ItemId") Integer ItemId) throws Exception{ 
    //... 
    return item; 
} 

这通常是非常有用的,因为我没有处理效应初探/请求对象和覆盖的业务逻辑。

但现在我需要访问本地文件,以便将其插入到PDF中。我查看了这里和文档中的所有项目,但无法找到可靠的easy方式访问上下文请求查找web应用程序的当前路径,以便我可以在分解的WAR中访问该文件。

任何帮助如何获得CXF-webapp的当前本地路径?

回答

1

一如既往,如果你最终写了一篇文章,你会自己找到解决方案。通过@Context注释,您将获得正确的上下文。

@GET 
@Path ("/handler/contract/{ItemId}") 
@Produces ("application/xml") 
public Item getItem(@Context ServletContext context, @PathParam("ItemId") Integer ItemId) throws Exception{ 
    //retrieve the local path from the context 
    String currentLocalPath = context.getRealPath("."); 
    //... 
    return item; 
} 

感谢您的访问。 :)