2012-02-17 56 views
0

我是一个REST包装服务,当我调用后端服务时,在某些情况下,它们生成了一个文件,我可以使用特定的URL检索:https://localhost:1234/ .. 。如何最有效地使用javax.ws.core.Response将链接中的内容发送给调用者?我可能自己读取URL到本地文件并以此方式发送,但我想知道REST是否会为我做到这一点。谢谢,作为jaxRS响应中的文件返回URL的内容

回答

0

当你的后端网址是公众对你的客户,你可以发送重定向301

当它不是

@Get 
@Produces({...}) 
public Response readFile() { 

    return Response.ok().entity(new StreamingOutput() { 
     @Override public void write(OutputStream output) 
      throws IOException, WebApplicationException { 
      // open the back-end url and copy the bytes to given output 
      final InputStream input = new URL(back-end-url).openStream(); 
      final byte[] buf = new byte[1024]; 
      for (int read = -1; (read = input.read(buf)) != -1;) { 
       output.write(buf, 0, read); 
      } 
      output.flush(); 
     } 
    }).build(); 
}