【问题标题】:return existing file using Java Jersey使用 Java Jersey 返回现有文件
【发布时间】:2014-01-21 15:46:40
【问题描述】:

我目前使用本地文件夹的绝对路径从 Java jersey 返回一个文件,如下所示

@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 中,而是在 Resource 中。

【问题讨论】:

    标签: jakarta-ee jersey-2.0


    【解决方案1】:

    可以在你的资源中注入ServletContext

    @Path("/database")
    @Stateful
    public class DBResource {
    
        @Context
        private ServletContext servletContext;
    
        @GET
        @Path("/get/7zip")
        @Produces("application/7z")
        public StreamingOutput getDatabase() {
            servletContext.getRealPath(...);
    
            ...
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-23
      • 2014-12-17
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 2020-02-20
      • 1970-01-01
      • 2015-05-05
      相关资源
      最近更新 更多