2012-03-30 87 views
1

我有一个restfull服务,在该服务中,我应该向客户端发送一个inputstream对象。所以我写了下面的代码...在服务方法..RESTfull服务方法可以将任何对象发送到客户端吗?

@GET 
@Path("/getFile") 
@Produces("application/pdf") 
public InputStream getFile() throws Exception { 
FileInputStream fin = null; 
FileOutputStream fout = null; 
DataInputStream dis = null; 
System.out.println("getFile called in server..."); 
File serverFile = null; 
System.out.println("getfile called.."); 
try { 
    serverFile = new File("E:\\Sample2.txt"); 
    fin = new FileInputStream(serverFile); 
    dis = new DataInputStream(fin); 
    fin.close(); 
    // dis.close(); 
} catch (Exception e) { 
    System.out.println("Exception in server appl..***************"); 
    e.printStackTrace(); 
} 
return dis; 
} 

在我的客户端应用程序的IM调用该服务为...

String clientURL = "http://xxxxxxx:xxxx/RestfullApp02/resources/LoadFile"; 
Client client = Client.create(); 
WebResource webResource = client.resource(clientURL); 

InputStream ob = webResource.path("getFile").get(InputStream.class); 

但我无法获得响应,它发送500错误..像下面errror ....

com.sun.jersey.api.client.UniformInterfaceException: GET http://myIp:myport/RestfullApp02/resources/LoadFile/getFile returned a response status of 500 

帮我

回答

2

请参考下面的链接,我感觉哟你可以通过它解决问题。

http://wpcertification.blogspot.in/2011/11/returning-binary-file-from-rest-service.html

+0

当我们返回的InputStream对象。只有在返回之前的VL关闭InputStream ..然后当v可以读取数据流.. 你能告诉我的客户如何调用此方法并获取InputStream对象.... – 2012-03-30 13:40:40

+0

您的问题中的一段代码将为此工作: InputStream ob = webResource.path(“getFile”)。get(InputStream.class); – NamingException 2012-03-30 13:47:15

+0

谢谢你naveen ..我终于得到了...救了我的一天... – 2012-03-30 13:56:20

相关问题