如何通过 REST 从我的 Jhipster 应用程序下载文件?
How can I download a file from my Jhipster application via REST?
我使用 Jhipster 开发了一个应用程序,我有一种方法可以从我的应用程序下载文件。当我通过 api REST 运行 此方法时,文件已成功下载,但如果我按下应用程序中的按钮进行下载,那将不起作用,调试一切正常。
他们的日志是一样的:
2019-04-12 19:20:51.971 DEBUG 940 --- [ XNIO-6 task-6] c.g.aop.logging.LoggingAspect : Enter: com.app.web.rest.TicketResource.downloadFile() with argument[s] = []
2019-04-12 19:20:51.973 DEBUG 940 --- [ XNIO-6 task-6] c.g.aop.logging.LoggingAspect : Exit: com.app.web.rest.TicketResource.downloadFile() with result = <200 OK,Byte array resource [resource loaded from byte array],{Content-Disposition=[attachment; filename=ticket.pdf], Cache-Control=[no-cache, no-store, must-revalidate], Pragma=[no-cache], Expires=[0], Content-Length=[92672], Content-Type=[application/pdf]}>
log via application
2019-04-12 19:23:43.341 DEBUG 940 --- [ XNIO-6 task-7] c.g.aop.logging.LoggingAspect : Enter: com.app.web.rest.TicketResource.downloadFile() with argument[s] = []
2019-04-12 19:23:43.343 DEBUG 940 --- [ XNIO-6 task-7] c.g.aop.logging.LoggingAspect : Exit: com.app.web.rest.TicketResource.downloadFile() with result = <200 OK,Byte array resource [resource loaded from byte array],{Content-Disposition=[attachment; filename=ticket.pdf], Cache-Control=[no-cache, no-store, must-revalidate], Pragma=[no-cache], Expires=[0], Content-Length=[92672], Content-Type=[application/pdf]}>
TicketResource.java
@RestController
@RequestMapping("/api")
public class TicketResource {
@GetMapping("/ticket-download")
public ResponseEntity<Resource> downloadFile() {
File file = new File("D:/Tickets/ticket.pdf");
HttpHeaders header = new HttpHeaders();
header.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=ticket.pdf");
header.add("Cache-Control", "no-cache, no-store, must-revalidate");
header.add("Pragma", "no-cache");
header.add("Expires", "0");
Path path = Paths.get(file.getAbsolutePath());
ByteArrayResource resource;
try {
resource = new ByteArrayResource(Files.readAllBytes(path));
return ResponseEntity.ok().headers(header).contentLength(file.length())
.contentType(MediaType.parseMediaType("application/pdf")).body(resource);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
我希望通过单击我的应用程序中的 "download" 按钮,文件将被正确下载
我是 Spring 的新手,但是您附上了下载控制器的代码,而不是实际进行下载的代码。
文件实际创建了吗?
路径存在(路径下的所有目录都存在)?
编辑:
我没意识到他说的是 web 应用程序,而不是 swing 或 javafx,所以以前的代码没用。对不起。
可能功能不完善,试试这个:
downloadFile(): Observable<any> {
return this.http
.get(SERVER_API_URL + 'api/ticket-download')
.subscribe((data) => {
this.blob = new Blob([data], {type: 'application/pdf'});
var downloadURL = window.URL.createObjectURL(this.blob);
window.open(downloadUrl);
});
或者使用 FileSaver 库。
您必须修改 service.component.ts
和 ticket.component.ts
才能使用资源响应。我建议您使用 FileSaver
库。下面是一个 Angular 下载文件 Spring 引导的示例,可以帮助您完成以下操作:http://roufid.com/angular-download-file-spring-boot/
我使用 Jhipster 开发了一个应用程序,我有一种方法可以从我的应用程序下载文件。当我通过 api REST 运行 此方法时,文件已成功下载,但如果我按下应用程序中的按钮进行下载,那将不起作用,调试一切正常。 他们的日志是一样的:
2019-04-12 19:20:51.971 DEBUG 940 --- [ XNIO-6 task-6] c.g.aop.logging.LoggingAspect : Enter: com.app.web.rest.TicketResource.downloadFile() with argument[s] = []
2019-04-12 19:20:51.973 DEBUG 940 --- [ XNIO-6 task-6] c.g.aop.logging.LoggingAspect : Exit: com.app.web.rest.TicketResource.downloadFile() with result = <200 OK,Byte array resource [resource loaded from byte array],{Content-Disposition=[attachment; filename=ticket.pdf], Cache-Control=[no-cache, no-store, must-revalidate], Pragma=[no-cache], Expires=[0], Content-Length=[92672], Content-Type=[application/pdf]}>
log via application
2019-04-12 19:23:43.341 DEBUG 940 --- [ XNIO-6 task-7] c.g.aop.logging.LoggingAspect : Enter: com.app.web.rest.TicketResource.downloadFile() with argument[s] = []
2019-04-12 19:23:43.343 DEBUG 940 --- [ XNIO-6 task-7] c.g.aop.logging.LoggingAspect : Exit: com.app.web.rest.TicketResource.downloadFile() with result = <200 OK,Byte array resource [resource loaded from byte array],{Content-Disposition=[attachment; filename=ticket.pdf], Cache-Control=[no-cache, no-store, must-revalidate], Pragma=[no-cache], Expires=[0], Content-Length=[92672], Content-Type=[application/pdf]}>
TicketResource.java
@RestController
@RequestMapping("/api")
public class TicketResource {
@GetMapping("/ticket-download")
public ResponseEntity<Resource> downloadFile() {
File file = new File("D:/Tickets/ticket.pdf");
HttpHeaders header = new HttpHeaders();
header.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=ticket.pdf");
header.add("Cache-Control", "no-cache, no-store, must-revalidate");
header.add("Pragma", "no-cache");
header.add("Expires", "0");
Path path = Paths.get(file.getAbsolutePath());
ByteArrayResource resource;
try {
resource = new ByteArrayResource(Files.readAllBytes(path));
return ResponseEntity.ok().headers(header).contentLength(file.length())
.contentType(MediaType.parseMediaType("application/pdf")).body(resource);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
我希望通过单击我的应用程序中的 "download" 按钮,文件将被正确下载
我是 Spring 的新手,但是您附上了下载控制器的代码,而不是实际进行下载的代码。
文件实际创建了吗?
路径存在(路径下的所有目录都存在)?
编辑: 我没意识到他说的是 web 应用程序,而不是 swing 或 javafx,所以以前的代码没用。对不起。
可能功能不完善,试试这个:
downloadFile(): Observable<any> {
return this.http
.get(SERVER_API_URL + 'api/ticket-download')
.subscribe((data) => {
this.blob = new Blob([data], {type: 'application/pdf'});
var downloadURL = window.URL.createObjectURL(this.blob);
window.open(downloadUrl);
});
或者使用 FileSaver 库。
您必须修改 service.component.ts
和 ticket.component.ts
才能使用资源响应。我建议您使用 FileSaver
库。下面是一个 Angular 下载文件 Spring 引导的示例,可以帮助您完成以下操作:http://roufid.com/angular-download-file-spring-boot/