java - 如何在 Angular2+ 中将 Blob xml 转换为 doc
问题描述
我成功地从后端(java)获得了 xml 格式的响应并且我下载了但是当我尝试打开它时,我得到了无效的输入/输出错误。
//这是我的服务中的方法
getDoc(){
return this.http.get("http://localhost:17080/files/doc",{responseType:
'text'});
}
//这是我在.ts文件中的方法
openDoc(){
this.dataService.getDoc().subscribe((response)=>{
console.log(response);
let blob = new Blob([response], { type:
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
});
fs.saveAs(blob, "name.doc");
}
//这是我后端的方法
@RequestMapping(value = "/doc", method = RequestMethod.GET)
public byte[] getDoc(){
return readFile("somelink");
}
//读取文件方法
private byte[] readFile(String url) {
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new URL(url).openStream());
byte[] dataToReturn = new byte[ARRAY_SIZE];
bis.read(dataToReturn, 0, ARRAY_SIZE);
return dataToReturn;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if(bis != null) {
try {
bis.close();
} catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
解决方案
推荐阅读
- c++ - 在 C++ 中抛出异常的替代方法是什么
- javascript - 如何忽略我正在使用 JavaScript 读取的表中的第一行?
- kubernetes - 具有自定义主机的多条路径上的 Kubernetes 入口导致 404
- r - 即使是最简单的 glm 函数也出现“未使用的参数”错误
- mysql - 如果 WHERE 不满足,则返回计数值为零
- featuretools - 我究竟做错了什么?featuretools 的性能估计似乎异常
- python - 获取计划包中执行下一个作业的剩余时间?
- javascript - 如何让我的网页显示来自 JavaScript 函数的图像?
- g1ant - 为什么具有隐藏值的 setattribute 不隐藏徽标?
- ios - Cordova IOS 不会构建