Java Spring - 如何在 DTO 中发送 byte[]
Java Spring - how to send byte[] within a DTO
我为服务器端应用程序工作,我遇到了一个问题:在前端,如果字节 [] 在 dto[= 中发送,则 Blob 无法正确转换为 Excel 文件22=]
对于POST请求,从后端(spring)如果我发送byte[],它很好地到达了前端
(angular,打字稿),但如果我在 DTO 对象中发送 byte[],它就会出错,我不明白为什么。 “变得错误”的含义:如果我尝试从 byte[](打字稿中的 Blob)创建一个 Excel 文件,这将不起作用 - 无法打开生成的 excel 文件,并且如果我尝试用文本编辑器(例如 notedpad++)打开它,我看到有字节字符(例如“UEsDBBQACAgIAC2oPVQAAAAAAAAAAAA”等等)这是错误的,因为正常的 Excel 文件以不同的方式显示文本编辑器。
现在代码:
1.没有 DTO 的变体(有效)
Java:
@PostMapping("/exportData")
@Transactional
public byte[] exportData(@RequestBody ExportRequestDTO exportRequestDTO){
// other stuff
byte[] a = new byte[5];
return a;
}
打字稿:
requestFileForExport(exportRequestDTO : ExportRequestDTO):Observable<HttpResponse<Blob>>{
return this.httpClient.post(`${this.exportDataURL}`,exportRequestDTO, {responseType: 'blob',observe: 'response'});
}
并且:
this.exportDataService.requestFileForExport(exportRequestDTO).subscribe((data:HttpResponse<Blob>)=>{
let blob = new Blob([data.body], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
saveAs(blob,"file.xlsx");
});
我可以打开生成的 excel 文件(使用 MS Excel)并且看起来不错。这是我用文本编辑器打开时的样子:
2 .DTO 的变体(它创建了一个错误的 excel 文件 - 所以这个变体不起作用。为什么?)
Java:
public class FileDTO {
private byte bytes[];
private String fileName;
// constructors, getters and setters
}
并且:
@PostMapping("/exportData")
@Transactional
public FileDTO exportData(@RequestBody ExportRequestDTO exportRequestDTO){
FileDTO a = new FileDTO();
// other stuff
return a;
}
打字稿:
export class FileDTO {
bytes:Blob;
fileName:string;
constructor(bytes: Blob, fileName: string) {
this.bytes = bytes;
this.fileName = fileName;
}
}
并且:
requestFileForExport(exportRequestDTO : ExportRequestDTO):Observable<FileDTO>{
return this.httpClient.post(`${this.exportDataURL}`,exportRequestDTO);
}
并且:
this.exportDataService.requestFileForExport(exportRequestDTO).subscribe(data=>{
let res = data;
let blob = new Blob([res.bytes], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
saveAs(blob,"file.xlsx");
});
我无法用 MS Excel 打开生成的 excel 文件。另外,这是我用文本编辑器打开时的样子
saveAs 来自 file-saver js,但如果我尝试手动保存文件,结果是相同的(变体 1 有效,变体 2 无效)- 问题出在文件本身,而不是保存部分
Jackson 默认将字节数组序列化为 base64,因此您必须先对其进行解码..
我为服务器端应用程序工作,我遇到了一个问题:在前端,如果字节 [] 在 dto[= 中发送,则 Blob 无法正确转换为 Excel 文件22=]
对于POST请求,从后端(spring)如果我发送byte[],它很好地到达了前端 (angular,打字稿),但如果我在 DTO 对象中发送 byte[],它就会出错,我不明白为什么。 “变得错误”的含义:如果我尝试从 byte[](打字稿中的 Blob)创建一个 Excel 文件,这将不起作用 - 无法打开生成的 excel 文件,并且如果我尝试用文本编辑器(例如 notedpad++)打开它,我看到有字节字符(例如“UEsDBBQACAgIAC2oPVQAAAAAAAAAAAA”等等)这是错误的,因为正常的 Excel 文件以不同的方式显示文本编辑器。
现在代码:
1.没有 DTO 的变体(有效)
Java:
@PostMapping("/exportData")
@Transactional
public byte[] exportData(@RequestBody ExportRequestDTO exportRequestDTO){
// other stuff
byte[] a = new byte[5];
return a;
}
打字稿:
requestFileForExport(exportRequestDTO : ExportRequestDTO):Observable<HttpResponse<Blob>>{
return this.httpClient.post(`${this.exportDataURL}`,exportRequestDTO, {responseType: 'blob',observe: 'response'});
}
并且:
this.exportDataService.requestFileForExport(exportRequestDTO).subscribe((data:HttpResponse<Blob>)=>{
let blob = new Blob([data.body], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
saveAs(blob,"file.xlsx");
});
我可以打开生成的 excel 文件(使用 MS Excel)并且看起来不错。这是我用文本编辑器打开时的样子:
Java:
public class FileDTO {
private byte bytes[];
private String fileName;
// constructors, getters and setters
}
并且:
@PostMapping("/exportData")
@Transactional
public FileDTO exportData(@RequestBody ExportRequestDTO exportRequestDTO){
FileDTO a = new FileDTO();
// other stuff
return a;
}
打字稿:
export class FileDTO {
bytes:Blob;
fileName:string;
constructor(bytes: Blob, fileName: string) {
this.bytes = bytes;
this.fileName = fileName;
}
}
并且:
requestFileForExport(exportRequestDTO : ExportRequestDTO):Observable<FileDTO>{
return this.httpClient.post(`${this.exportDataURL}`,exportRequestDTO);
}
并且:
this.exportDataService.requestFileForExport(exportRequestDTO).subscribe(data=>{
let res = data;
let blob = new Blob([res.bytes], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
saveAs(blob,"file.xlsx");
});
我无法用 MS Excel 打开生成的 excel 文件。另外,这是我用文本编辑器打开时的样子
saveAs 来自 file-saver js,但如果我尝试手动保存文件,结果是相同的(变体 1 有效,变体 2 无效)- 问题出在文件本身,而不是保存部分
Jackson 默认将字节数组序列化为 base64,因此您必须先对其进行解码..