如何读取 RESTFul Web 服务响应(作为 MediaType 的 zip 文件)?
How to read RESTFul webservice reponse ( zip file as a MediaType)?
当我点击服务 URL 时,它会给我一个需要移动到某个位置的 ZIP 文件。
请告诉我如何从 Response 中读取 zip 文件。下面是我的代码。
@Path("/folder")
public class FileDownloadService {
@GET
@Path("zipFile")
@Produces("application/zip")
public Response getFile() {
File f = new File("/home/mpasala/Documents/Example.zip");
if (!f.exists()) {
throw new WebApplicationException(404);
} else {
Boolean success = moveFile();
}
return Response
.ok(f)
.header("Content-Disposition",
"attachment; filename=server.zip").build();
}
InputStream response = webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(InputStream.class,
formData);
OutputStream out = null;
int read = 0;
byte[] bytes = new byte[1024];
File file = new File("D:/Today/");
if (!file.exists()) {
file.mkdir();
out = new FileOutputStream(new File(file + ".zip"));
while ((read = response.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
// new File(file +".zip").createNewFile();
} else {
out = new FileOutputStream(new File(file+ ".zip"));
while ((read = response.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
}
out.flush();
out.close();
当我点击服务 URL 时,它会给我一个需要移动到某个位置的 ZIP 文件。
请告诉我如何从 Response 中读取 zip 文件。下面是我的代码。
@Path("/folder")
public class FileDownloadService {
@GET
@Path("zipFile")
@Produces("application/zip")
public Response getFile() {
File f = new File("/home/mpasala/Documents/Example.zip");
if (!f.exists()) {
throw new WebApplicationException(404);
} else {
Boolean success = moveFile();
}
return Response
.ok(f)
.header("Content-Disposition",
"attachment; filename=server.zip").build();
}
InputStream response = webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(InputStream.class,
formData);
OutputStream out = null;
int read = 0;
byte[] bytes = new byte[1024];
File file = new File("D:/Today/");
if (!file.exists()) {
file.mkdir();
out = new FileOutputStream(new File(file + ".zip"));
while ((read = response.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
// new File(file +".zip").createNewFile();
} else {
out = new FileOutputStream(new File(file+ ".zip"));
while ((read = response.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
}
out.flush();
out.close();