Java 从RESTWebServicesSpring下载文件
我正在尝试下载任何调用我的rest Web服务的文件。我将spring+jersey用于web服务,Angular 2用于前端。 因此,当我点击前端时,Web服务会获取我的文件,但下载窗口不会显示。 我的rest API:Java 从RESTWebServicesSpring下载文件,java,angular,rest,Java,Angular,Rest,我正在尝试下载任何调用我的rest Web服务的文件。我将spring+jersey用于web服务,Angular 2用于前端。 因此,当我点击前端时,Web服务会获取我的文件,但下载窗口不会显示。 我的rest API: @POST @Path("/download") @ApiOperation(value = "Download") @Produces(MediaType.APPLICATION_OCTET_STREAM) public Respon
@POST
@Path("/download")
@ApiOperation(value = "Download")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response downloadFile(@ApiParam(value = "File", required = true) String filePath) {
File file = new File("/webapps/pdf/upload/msg/1/gest.PNG");
Response.ResponseBuilder response = Response.ok((Object) file);
try {
String contentType = Files.probeContentType(file.toPath());
response.header("Content-Disposition", "attachment; filename="+file.getName());
response.header("Content-Type", contentType);
return response.build();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
我的角度发球:
downloadFile(path) {
const headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded', 'Accept': '*'});
const options = new RequestOptions({headers: headers});
options.responseType = ResponseContentType.Blob;
return this.http.post(apiUrl + "msg/download", path, options)
.catch(this.handleError);
}
我的角度分量:
downloadFile(documentPath) {
this.msgService.downloadFile(documentPath).subscribe(response => {
var contentType = response.headers('Content-Type');
let url = window.URL.createObjectURL(new Blob([response._body], {type: contentType}));
window.open(url);
});
}
Html:
<figure class="ui-g-12 " *ngFor="let document of msg.documents_path" (click)="downloadFile(document)">
<img [src]="selectImageByExtension(document.split('.').pop().toLowerCase())" />
<figcaption>{{document.split('/').pop().toLowerCase()}}</figcaption>
</figure>
{{document.split('/').pop().toLowerCase()}
当我点击我的图
时,我可以看到该文件已获得:
但是什么也没有出现。
我错过了什么 所以对我来说唯一有效的解决方案是使用GET请求,而不是将文件路径作为pathparam进行POST传递 Rest API:
@GET
@Path("/download/{filePath}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getdownloadFile(@PathParam("filePath") String filePath) {
String path = null;
byte [] barr = Base64.getDecoder().decode(filePath);
path = new String(barr);
File file = new File(path);
try {
String contentType = Files.probeContentType(file.toPath());
Response.ResponseBuilder response = Response.ok((Object) file);
response.header("Content-Disposition", "attachment; filename="+file.getName());
response.header("Content-Type", contentType);
response.header("Content-Length", file.length());
return response.build();
} catch (IOException e) {
e.printStackTrace();
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
}
}
角度服务:
downloadFile(path) {
const headers = new Headers({'Content-Type': 'text/plain', 'Accept': '*'});
const options = new RequestOptions({headers: headers});
options.responseType = ResponseContentType.Blob;
return this.http.get(apiUrl + "msg/download/"+btoa(path), options)
.map(res => res)
.catch(this.handleError);
}
角分量:
downloadFile(documentPath) {
this.msgService.downloadFile(documentPath).subscribe(response => {
let params = documentPath.split('/' );
var blob = new Blob([response._body]);
FileSaver.saveAs(blob, params[params.length-1]);
});
}
您是否检查了日志以查看WS是否得到了调用并且没有遇到IOException?是的,它可以工作,否则我无法从调试视图中获取它