java - RESTful 服务方法可以向客户端发送对象吗?

标签 java rest

我有一个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/

相关文章:

java.nio.file.Path 与 sun.nio.fs.UnixPath

java - 我在一个运行良好的 EJB3 项目中收到 404 错误

java - OSGI 无法使用 javax.ws.rs 启动 bundle 'missing requirement'

java - Spring API 中的日志请求和响应

java - Spring Restful Service将所有url重定向到index.html

java - 如何支持变量上下文路径?

java - 使用方法调用合并两个数组

java - Spring Boot 在作为传统 war 部署时尝试运行嵌入式 Tomcat

java - 为什么 REST Controller 返回 404 状态代码?

database - rest:修改与资源关联的资源的顺序