使用 Java apache poi 和 React 通过 API 发送 xls
Sending xls via API with Java apache poi and React
我正在尝试从我的 java spring 服务器发送一个 xls 文件来响应客户端。
使用默认的 Apache POI 构造函数创建 xlsx 文件,这并不好。为了覆盖它,我必须使用 FileOutputStream 创建文件。
FileOutputStream outputStream = new FileOutputStream("file.xls");
但是我无法通过网络发送文件。我尝试使用以下答案: 我引用:“下载文件时,您的代码需要逐块流式传输文件 - 这就是 Java 流的用途。”
return ResponseEntity.ok().contentLength(inputStreamWrapper.getByteCount())
.contentType(MediaType.parseMediaType("application/vnd.ms-excel"))
.cacheControl(CacheControl.noCache())
.header("Content-Disposition", "attachment; filename=" + "file.xls")
.body(new InputStreamResource(inputStreamWrapper.getByteArrayInputStream()));
所以我的控制器正在发送 InputStreamResource
。
如何使用我的 FileOutputStream
构建 InputStreamResource
?
P.S 这是我的 React 客户端:
axios.get('/issues/export', { responseType: 'arraybuffer' }).then(response => {
if (response && !response.error) {
const blob = new Blob([response.payload.data], {type: 'application/vnd.ms-excel'});
saveAs(blob);
}
});
来源:
编辑:
我设法用一个技巧做到了这一点,在我写入 FileOutputStream 之后,我打开了一个 FileInputStream 并返回了值。
FileOutputStream outputStream = new FileOutputStream("file.xls");
workbook.write(outputStream);
workbook.close();
final InputStream fileInputStream = new FileInputStream("file.xls");
return fileInputStream;
但是现在,作为对客户端的响应返回的 xls 文件已损坏,并且里面有奇怪的字符:
excel 文件应如下所示(发送后取自我的 java 服务器):
问题已解决。最后,为了解决损坏的 xls 文件,我所做的是使用字节数组。控制器看起来完全一样,但现在 return 类型是 ResponseEntity<byte[]>
。为了将 InputStream
转换为字节数组,我使用了 IOUtils.toByteArray()
方法。
客户端代码也发生了一些变化,因为现在类型不再是 responseType: 'arraybuffer'
而是 'blob'
.
axios.get('/issues/export', { responseType: 'blob' }).then(response => {
if (response && !response.error) {
const blob = new Blob([response.payload.data]);
saveAs(blob);
}
});
就这些了。
我正在尝试从我的 java spring 服务器发送一个 xls 文件来响应客户端。
使用默认的 Apache POI 构造函数创建 xlsx 文件,这并不好。为了覆盖它,我必须使用 FileOutputStream 创建文件。
FileOutputStream outputStream = new FileOutputStream("file.xls");
但是我无法通过网络发送文件。我尝试使用以下答案: 我引用:“下载文件时,您的代码需要逐块流式传输文件 - 这就是 Java 流的用途。”
return ResponseEntity.ok().contentLength(inputStreamWrapper.getByteCount())
.contentType(MediaType.parseMediaType("application/vnd.ms-excel"))
.cacheControl(CacheControl.noCache())
.header("Content-Disposition", "attachment; filename=" + "file.xls")
.body(new InputStreamResource(inputStreamWrapper.getByteArrayInputStream()));
所以我的控制器正在发送 InputStreamResource
。
如何使用我的 FileOutputStream
构建 InputStreamResource
?
P.S 这是我的 React 客户端:
axios.get('/issues/export', { responseType: 'arraybuffer' }).then(response => {
if (response && !response.error) {
const blob = new Blob([response.payload.data], {type: 'application/vnd.ms-excel'});
saveAs(blob);
}
});
来源:
编辑:
我设法用一个技巧做到了这一点,在我写入 FileOutputStream 之后,我打开了一个 FileInputStream 并返回了值。
FileOutputStream outputStream = new FileOutputStream("file.xls");
workbook.write(outputStream);
workbook.close();
final InputStream fileInputStream = new FileInputStream("file.xls");
return fileInputStream;
但是现在,作为对客户端的响应返回的 xls 文件已损坏,并且里面有奇怪的字符:
excel 文件应如下所示(发送后取自我的 java 服务器):
问题已解决。最后,为了解决损坏的 xls 文件,我所做的是使用字节数组。控制器看起来完全一样,但现在 return 类型是 ResponseEntity<byte[]>
。为了将 InputStream
转换为字节数组,我使用了 IOUtils.toByteArray()
方法。
客户端代码也发生了一些变化,因为现在类型不再是 responseType: 'arraybuffer'
而是 'blob'
.
axios.get('/issues/export', { responseType: 'blob' }).then(response => {
if (response && !response.error) {
const blob = new Blob([response.payload.data]);
saveAs(blob);
}
});
就这些了。