我正在尝试实现一个读取zip文件的休息服务。客户端调用此服务,并从响应中构建zip文件。我有这个用于我的服务:
@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;
}
如果这是正确的方法,有人可以告诉我吗?
答案 0 :(得分:0)
尝试这样的方法,从响应的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();