在tomcat webapplication中打开一个文件

Max*_*011 0 java tomcat file jersey

我想打开一个文件并返回其内容.虽然它与想要打开文件的类在同一目录中,但找不到该文件.如果你能帮助我解决这个问题会很酷.

这是代码:

@GET @Produces("text/html") @Path("/{partNO}/") @Consumes("text/html")
public String getPartNoResponseHTML(@PathParam("partNO") String parID) throws WebApplicationException {
PartNoTemplate partNo = getPartNoResponse(parID);

String result = "";

try {
  result = readFile(PART_NO_TEMPLATE_FILE);
} catch (FileNotFoundException e) {
  e.printStackTrace(System.out);
  return e.getMessage() + e.toString();
  // throw new WebApplicationException(Response.Status.NOT_FOUND);
} finally {
  result = result.replace("{partNO}", parID);
  result = result.replace("{inputFormat}", partNo.getFormat().toString());
}

return result;
}
Run Code Online (Sandbox Code Playgroud)

我猜它找不到文件,因为它在tomcat上运行.我也在使用Jersey和JAX-RS.谢谢您的帮助,

马克西

小智 5

如果文件在应用程序WAR内(或在jar中),您可以尝试使用

InputStream input = servletContext.getClass().getClassLoader().getResourceAsStream("my_filename.txt");
Run Code Online (Sandbox Code Playgroud)

您的问题与我的问题类似(我认为)如何从WAR中的classes目录中读取文件?