我尝试使用Java创建一些REST Web服务,以便发送数据,在服务器上进行计算并返回结果。在第一阶段,我作为excel文件发送和接收信息(将来我更喜欢使用XML或JSON)。 好吧,经过大量的时间尝试,并阅读了很多帖子,似乎我非常接近实现它,但我不知道如何获得服务器的最终响应。 我有这样的服务:
@GET
@Path("/test")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getFile(@QueryParam("IDfile") String IDfile) {
if(IDfile.trim().length() == 0 || IDfile == null) {
return Response.status(Response.Status.BAD_REQUEST).entity("IDfile cannot be blank").build();
}
String uploadedFileLocation = "C:\\FilesWebservice\\" + IDfile;
Boolean sortida = false;
try {
prova prueba = new prova();
sortida = prueba.prova(uploadedFileLocation); //this creates an xls file as response
} catch (Exception ex) {
System.out.println("error" + ex.toString());
Logger.getLogger(ServiceResource.class.getName()).log(Level.SEVERE, null, ex);
}
if (sortida) {
File file = new File("C:\\FilesWebservice\\out\\prediction.xls"); // the File path you want to serve.
return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM)
.header("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"" ) //optional
.build();
} else
return Response.status(500).entity("It was unable to calculate (Ask God for the reason)").build();
}
它工作正常,如果我通过浏览器发送GET,我会在我的下载文件夹中收到该文件,但我需要将该服务与其他应用程序一起使用。因此,我正在使用Netbeans开发客户端,然后,NB根据我的Web服务创建了自动代码。在这种情况下,我有:
public <T> T getFile(Class<T> responseType, String IDfile) throws ClientErrorException {
WebTarget resource = webTarget;
if (IDfile != null) {
resource = resource.queryParam("IDfile", IDfile);
}
resource = resource.path("test");
Builder builder = resource.request(MediaType.APPLICATION_OCTET_STREAM_TYPE);
Invocation invocation = builder.buildGet();
return resource.request(MediaType.APPLICATION_OCTET_STREAM_TYPE).get(responseType);
}
也许我添加了一些内容,我现在不记得了。无论如何,该服务返回状态代码,自定义消息和文件作为附件。我想至少阅读状态代码并显然保存文件,但我不知道我该怎么做。 我试着这样做:
MyJerseyClientAlgA client = new MyJerseyClientAlgA("192.168.1.30");
Object response = client.getFile(Response.class, "3cphkhfu.xls");
但是从“回复”中提取我需要的信息是不成功的。 任何帮助或想法将不胜感激。
非常感谢提前
编辑:
感谢@LutzHorn的回复。我不确定我是否理解你的建议,我会做一些测试,如果我找到解决方案,我会在我的问题下发帖。无论如何,我再次生成了使用REST服务的自动代码,即:
public <T> T getFile(Class<T> responseType, String IDfile) throws ClientErrorException {
WebTarget resource = webTarget;
if (IDfile != null) {
resource = resource.queryParam("IDfile", IDfile);
}
resource = resource.path("test");
return resource.get(responseType);
}
但是我在最后一行有一个错误,它表示: 找不到符号
symbol:方法get(Class)
所以我改变了这一行
return resource.request(MediaType.APPLICATION_OCTET_STREAM_TYPE).get(responseType);
但我不确定这是否正确。
答案 0 :(得分:0)
好吧,经过几个小时的搜索和测试,这段代码可以运行。我不知道它是否是最好的解决方案,但它完全符合我的要求:提取状态并保存Web服务返回的文件。
public void getFile(String IDfile) throws ClientErrorException {
WebTarget resource = webTarget;
if (IDfile != null) {
resource = resource.queryParam("IDfile", IDfile);
}
resource = resource.path("test");
Invocation inv = resource.request(MediaType.APPLICATION_OCTET_STREAM_TYPE).buildGet();
Response rp = inv.invoke();
InputStream attachment = null;
try {
if (rp.getStatus() == 200) {
attachment = rp.readEntity(InputStream.class); //This method can be invoked only once unless you buffer the response...
ReadableByteChannel rbc = Channels.newChannel(attachment); //website.openStream()
FileOutputStream fos = new FileOutputStream("C://FilesWebservice/solution.xls");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
} else {
System.out.println(rp.getStatus());
}
} catch ( Exception ex) {
ex.printStackTrace();
} finally {
rp.close();
}
}