Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从java后端服务下载angular中的文件_Java_Angular_Rest_File_Download - Fatal编程技术网

从java后端服务下载angular中的文件

从java后端服务下载angular中的文件,java,angular,rest,file,download,Java,Angular,Rest,File,Download,我正在尝试下载从后端服务器发送的文件。 get方法在postman上运行得很好,但在我的前端应用程序(angular 5)中无法实现 这是我的后端: @GET @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Path("/recupererDocument/{id}") @ResponseStatus(value = {Response.Status.OK, Response.Status.BAD_REQUE

我正在尝试下载从后端服务器发送的文件。 get方法在postman上运行得很好,但在我的前端应用程序(angular 5)中无法实现

这是我的后端:

@GET
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("/recupererDocument/{id}")
@ResponseStatus(value = {Response.Status.OK, Response.Status.BAD_REQUEST,
                Response.Status.INTERNAL_SERVER_ERROR,})
@ElementClass(request = Long.class, response = Response.class)
public Response recupererDocument(
    @NotNull(message = "L'id (identifiant du document) est obligatoire") @PathParam("id") final String id);



public Response recupererDocument(String id) {
File file = null;
final WebTarget target = ...;

Builder builderDoc = target.request(typeMime);

final Response reponseDocument = builderDoc.accept(typeMime).get();
    LOGGER.debug(reponseDocument.getStatus());

if (reponseDocument.getStatus() == 200) {
    file = reponseDocument.readEntity(new GenericType<File>() {
    });
}
reponseDocument.close();
return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM).build();
}
@GET
@使用({MediaType.APPLICATION\uxml,MediaType.APPLICATION\ujson})
@路径(“/RecuperDocument/{id}”)
@ResponseStatus(值={Response.Status.OK,Response.Status.BAD_请求,
Response.Status.INTERNAL_SERVER_ERROR,})
@ElementClass(请求=Long.class,响应=response.class)
公众回应文件(
@NotNull(message=“L'id(文档标识)est obligatoire”)@PathParam(“id”)最终字符串id);
公共响应RecuperDocument(字符串id){
File=null;
最终WebTarget目标=。。。;
builderDoc=target.request(typeMime);
最终响应reponseDocument=builderDoc.accept(typeMime.get();
debug(reponseDocument.getStatus());
if(reponseDocument.getStatus()==200){
file=reponseDocument.readEntity(新的GenericType(){
});
}
reponseDocument.close();
返回Response.ok(文件,MediaType.APPLICATION_OCTET_STREAM).build();
}
和我的前端:

telechargerPieceJointe(id: string): void {
    this.s
        .telechargerPieceJointe(id)
        .then(pj => {
            this.downloadFile(pj);
        })
        .catch(e => {
            if (isDevMode()) {
                console.log('Erreur : ' + e);
            }
        });
}

downloadFile(data: Response) {
    const blob = new Blob([data], {type: 'application/octet-stream'});
    const url = window.URL.createObjectURL(blob);
    window.open(url);
}


telechargerPieceJointe(id: string): Promise<any> {
    const url = environment.rest.local.recupererDocument + '/' + id;
    const headers = new HttpHeaders({
        Accept: 'application/pdf',
        responseType: 'blob'
    });

    return this.httpClient.get(url, {headers}).toPromise();
}
TelechargerPieceJoint(id:string):无效{
这个
.电话计费单点(id)
.然后(pj=>{
这个.downloadFile(pj);
})
.catch(e=>{
if(isDevMode()){
console.log('Erreur:'+e);
}
});
}
下载文件(数据:响应){
const blob=new blob([data],{type:'application/octet stream'});
constURL=window.url.createObjectURL(blob);
窗口打开(url);
}
电信资费单点(id:string):承诺{
const url=environment.rest.local.recuperdocument+'/'+id;
const headers=新的HttpHeaders({
接受:'application/pdf',
响应类型:“blob”
});
返回这个.httpClient.get(url,{headers}).toPromise();
}
现在在调试的前端,我没有输入“.then”方法,而是输入.catch

我有以下信息:

JSON中位于位置0的意外标记%

但是在chrome开发工具的“网络”选项卡中,请求是好的(200),我可以在“响应”部分看到我的对象。我不明白为什么它不工作:/


如果有人看到我做错了什么,这可能会让我保持理智。

Angular尝试将http响应正文解析为JSON(这是默认的响应类型)。
responseType
不是在
HttpHeaders
中指定的,而是在httpOptions对象中指定的

在传递给
httpClient.get的httpOptions中指定
responseType:'blob'

telecochargerpiecejoint(id:string):可观察{
const url=environment.rest.local.recuperdocument+'/'+id;
const headers=新的HttpHeaders({
接受:'application/pdf'
});
返回这个.httpClient.get(url,{headers,responseType:'blob'});
}

它可以工作!我了解问题所在,但不知道如何使其工作。谢谢!