我有一个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;
}
在我的客户端应用程序中,我将此服务称为...
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 错误.. 就像下面的错误....
com.sun.jersey.api.client.UniformInterfaceException: GET http://myIp:myport/RestfullApp02/resources/LoadFile/getFile returned a response status of 500
帮助我
最佳答案
请引用下面的链接,我觉得你可以通过它解决问题。
http://wpcertification.blogspot.in/2011/11/returning-binary-file-from-rest-service.html
关于java - RESTful 服务方法可以向客户端发送对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9943063/