Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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
Jakarta ee 使用Java返回现有文件_Jakarta Ee_Jersey 2.0 - Fatal编程技术网

Jakarta ee 使用Java返回现有文件

Jakarta ee 使用Java返回现有文件,jakarta-ee,jersey-2.0,Jakarta Ee,Jersey 2.0,我目前正在使用一个指向本地文件夹的绝对路径从JavaJersey返回一个文件,如下所示 @Path("/database") @Stateful public class DBResource { @GET @Path("/get/7zip") @Produces("application/7z") public StreamingOutput getDatabase() { java.io.

我目前正在使用一个指向本地文件夹的绝对路径从JavaJersey返回一个文件,如下所示

@Path("/database")
@Stateful
public class DBResource
{
        @GET
        @Path("/get/7zip")
        @Produces("application/7z")
        public StreamingOutput getDatabase()
        {
            java.io.File file = new java.io.File("/home/jack.jude/myfile.7z");
            return new FileStreamingOutput(file);
        }
}
考虑到这一点,我想知道如何从应用程序中返回资源

Thread.currentThread().getContextClassLoader().getResource("");
不返回有意义的路径,并且

   String rootPath = getServletConfig().getServletContext().getRealPath("/");

无法使用,因为我不是在servlet中,而是在资源中。

可以在资源中插入
ServletContext

@Path("/database")
@Stateful
public class DBResource {

    @Context
    private ServletContext servletContext;

    @GET
    @Path("/get/7zip")
    @Produces("application/7z")
    public StreamingOutput getDatabase() {
        servletContext.getRealPath(...);

        ...
    }
}