[英]Open a pdf file using javax.ws and angular
我正在嘗試使用Java Restful服務和angularjs打開保存在服務器中的PDF文件。 我在Java中的服務代碼是:
@GET
@Path("/getPDF")
@Produces("application/pdf")
public Response getPDF() throws FileNotFoundException {
File file = new File("/path/to/file.pdf");
FileInputStream fileInputStream;
fileInputStream = new FileInputStream(file);
long contentLength = file.length();
ResponseBuilder responseBuilder = Response.ok((Object) fileInputStream);
responseBuilder.type("application/pdf");
responseBuilder.header("Content-Disposition", "inline; filename=file.pdf");
responseBuilder.header("Content-Length", contentLength);
responseBuilder.header("charset", "utf-8");
return responseBuilder.build();
}
一旦返回Response,我使用angularjs處理請求:
MyJavaService.getPDF().success(function(data){
var file = new Blob([data], {type: 'application/pdf'});
var fileURL = URL.createObjectURL(file);
$window.open(fileURL);
});
結果是打開一個新窗口,頁面數量與PDF文件相同(正確),但不顯示內容,所有頁面都是白色(不正確)。
有沒有人知道我做錯了什么?
我假設你正在使用$ http.get()調用。
你可能有以下幾點:
$http.get(<URL>, {params: <stuff>})
你需要告訴angular,responseType是一個arrayBuffer,如下所示:
$http.get(<URL>, {responseType: 'arraybuffer', params: <stuff>})
不要猶豫,告訴我我的假設是不正確的。
ps:盡量避免使用.success()而是使用.then()。 前者被棄用1.4+並在1.6+中完全刪除
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.