Cxf Rest服务读取客户端中的zip文件
我正在尝试实现一个读取zip文件的rest服务。客户端调用此服务,并根据响应构建zip文件。我有这个服务:Cxf Rest服务读取客户端中的zip文件,cxf,jax-rs,Cxf,Jax Rs,我正在尝试实现一个读取zip文件的rest服务。客户端调用此服务,并根据响应构建zip文件。我有这个服务: @Produces({ "application/zip" }) @GET @Path("/fetchZip") public Response getZip() { try { InputStream theFile = new FileInputStream("test.zip"); ZipInputStream zis = new ZipInputStream(the
@Produces({ "application/zip" })
@GET
@Path("/fetchZip")
public Response getZip() {
try {
InputStream theFile = new FileInputStream("test.zip");
ZipInputStream zis = new ZipInputStream(theFile);
return Response.ok(zis).build();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
有人能告诉我这样做是否正确吗 尝试以下操作,从响应的InputStream读取并写入文件的FileOutputstream:
URL url = new URL("YOUR_URL");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "application/json");
if (conn.getResponseCode() == 200) {
InputStream inputStream = conn.getInputStream();
OutputStream output = new FileOutputStream("C:/yourFile.zip");
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
output.close();
}
conn.disconnect();
这种方法有效吗?您可能必须指明返回的内容类型。我尝试添加内容类型,但似乎不起作用。您可以发布您正在执行的请求和收到的错误吗?这是我的客户端代码HttpGet request=new HttpGet;request.addHeadernew BasicHeaderAccept,application/zip;HttpResponse response=client.executerequest;InputStream is=response.getEntity.getContent;ZipInputStream zip=新ZipInputStreamis;错误是什么?我也试过了,但当我试图打开zip文件时,它说它已损坏