0
我試圖通過添加Content-Type
和Content-Disposition
執行以下操作從REST發送downloada的zip文件的文件:從REST返回zip文件到客戶端不彈出
服務器端
@Produces("application/x-zip-compressed")
public Response export(@PathParam("username") String username,
@Context UriInfo ui) {
long timeStamp = new Date().getTime();
String exportedFolder = "/home/madmin/pods/"+username+"/download/";
String Path = ui.getRequestUri().toString()
.replace(replaceMe + username, "");
Path = Path.replace("http://", "https://");
Path = Path.replace(",system/export", "");
String outputZipFile = exportedFolder+"exported_on_"+timeStamp+".zip";
String sourceFolder = null;
File file = new File(exportedFolder);
if (!file.exists()) {
if (file.mkdirs()) {
System.out.println("Directory is created!");
} else {
System.out.println("Failed to create directory for exporting!");
//return ;
}
}
constructGraphs cGraphs = new constructGraphs(Path, username);
sourceFolder = cGraphs.writeGraphFiles();
generateZipFile appZip = new generateZipFile(sourceFolder,outputZipFile);
appZip.generateFileList(new File(sourceFolder));
appZip.zipIt(outputZipFile);
//Read the outputZipFile as inputStream and return it
FileInputStream fileIs = null;
try {
fileIs = new FileInputStream(outputZipFile);
} catch (IOException e) {
throw new WebApplicationException(404);
}
String fileName= outputZipFile.substring(outputZipFile.lastIndexOf("/")+1, outputZipFile.length());
return Response.status(Status.OK).entity(fileIs).header("Content-Disposition","attachment; filename = "+fileName).build();
}
客戶端:
在另一方面的客戶端側被期待作爲application/x-zip-compressed
如下:
$http({
method: 'GET',
url: uri,
headers: {
'Accept': 'application/x-zip-compressed'
},
withCredentials: true
}).
success(function(data, status, headers) {
if (status == 200 || status == 201) {
notify('Success', 'Node exported.');
}
}).
error(function(data, status) {
if (status == 401) {
notify('Forbidden', 'Authentication required to edit the resource.');
} else if (status == 403) {
notify('Forbidden', 'You are not allowed to edit the resource.');
} else {
notify('Failed', status + " " + data);
}
});
該文件沒有彈出,而是在響應中看到輸入流。
我在這裏做什麼錯了?任何幫助將非常感激。
試試這個response.setContentType(「應用程序/ x-ZIP壓縮」)返回響應 – userRaj
@userRaj檢查它默認情況下,在頭設置響應,這就是爲什麼我沒有」前不得不明確地做。 – mzereba