---
title: "jboss的webservices中读取文件"
author: "nowing"
date: "2011-05-25T09:33:11Z"
tags: []
categories: ["企业架构"]
slug: "jboss-webservices"
draft: false
lastmod: "2011-05-25T09:33:50Z"
id: 1057819
url: "https://nowing.iteye.com/blog/1057819"
---

在使用jboss中的webservices时，读取war中的文件，可以使用this.getClass().getResourceAsStream(filename)，这种方法比较方便，但要读取的文件必须在class路径内。
但有时文件不在class路径内，就需要用realpath的方式。
    @Resource
    WebServiceContext wsCtx;

        String realPath = ((ServletContext) wsCtx.getMessageContext().get(
                MessageContext.SERVLET_CONTEXT)).getRealPath("/");
之后再加上相对路径即可。