2014-10-09 40 views
0

我開發了一個使用Jersey和grizzly嵌入式服務器的RESTful應用程序。多部分文件上傳正在工作,但我不知道如何在客戶端請求時提供上傳的靜態文件。使用grizzly嵌入式服務器上傳靜態文件

我的問題聽起來有點普遍,但我認爲我的問題也相當普遍。如果有必要,我可以提供我的項目的詳細信息,但我正在尋找一個通用答案。

回答

2

下面是示例如何添加靜態和動態請求處理:

ResourceConfig rc = new ResourceConfig().register(TestResource.class); 
URI jerseyAppUri = UriBuilder.fromUri("http://0.0.0.0/api") 
     .port(8080).build(); 

// Jersey app at http://localhost:8080/api/... 
HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(
     jerseyAppUri, rc, false); 

// Static content handler at http://localhost:8080/static/... 
httpServer.getServerConfiguration().addHttpHandler(
     new StaticHttpHandler("/home/user1/myapp/www/"), "/static"); 

// Dynamic content handler at http://localhost:8080/dynamic 
httpServer.getServerConfiguration().addHttpHandler(
    new HttpHandler() { 
      @Override 
      public void service(Request request, Response response) throws Exception { 
       response.getWriter().write("Hello World!"); 
      } 
    }, "/dynamic"); 

httpServer.start();