2016-03-15 29 views
0

我使用了球衣服務器,並且我希望端點根據參數重定向到文件的下載。如何給實現另一種方法的方法提供參數

我有一個具有如下功能的困難:

@GET 
@Path("/get/{id}/{chunk}") 
public Response getDescription(@PathParam("id") String id, @PathParam("chunk") String chunk) { 
{ 

    StreamingOutput fileStream = new StreamingOutput() 
    { 
     @Override 
     public void write(java.io.OutputStream output, String id) throws IOException, WebApplicationException 
     { 
      try 
      { 
       if (Objects.equals(chunk, new String("init"))) { 
        java.nio.file.Path path = Paths.get("src/main/uploads/example/frame_init.pdf"); 
       } 
       else { 
        java.nio.file.Path path = Paths.get("src/main/uploads/example/"+ id +".pdf"); 
       } 
       byte[] data = Files.readAllBytes(path); 
       output.write(data); 
       output.flush(); 
      } 
      catch (Exception e) 
      { 
       throw new WebApplicationException("File Not Found !!"); 
      } 
     } 
    }; 
    return Response 
      .ok(fileStream, MediaType.APPLICATION_OCTET_STREAM) 
      .header("content-disposition","attachment; filename = myfile.pdf") 
      .build(); 
} 

我有一個參數傳遞給函數寫一個問題。我有我的參數ID和端點的塊,但我不能在寫入方法中使用它,因爲它實現了StreamingOutput()。

我該如何處理?謝謝

回答

1

對於java來說,final關鍵字應該可以解決你的問題。

更新後的代碼;

@GET 
@Path("/get/{id}/{chunk}") 
public Response getDescription(@PathParam("id") final String id, @PathParam("chunk") final String chunk) { 
{ 

    StreamingOutput fileStream = new StreamingOutput() 
    { 
     @Override 
     public void write(java.io.OutputStream output, String id2) throws IOException, WebApplicationException 
     { 
      try 
      { 
       if (Objects.equals(chunk, new String("init"))) { 
        java.nio.file.Path path = Paths.get("src/main/uploads/example/frame_init.pdf"); 
       } 
       else { 
        java.nio.file.Path path = Paths.get("src/main/uploads/example/"+ id2 +".pdf"); 
       } 
       byte[] data = Files.readAllBytes(path); 
       output.write(data); 
       output.flush(); 
      } 
      catch (Exception e) 
      { 
       throw new WebApplicationException("File Not Found !!"); 
      } 
     } 
    }; 
    return Response 
      .ok(fileStream, MediaType.APPLICATION_OCTET_STREAM) 
      .header("content-disposition","attachment; filename = myfile.pdf") 
      .build(); 
} 
+1

你說得對,謝謝:) – ImbaBalboa

相關問題