首页 > 解决方案 > 如何在 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();
                }
            }
         }

标签: javaangularspringtypescriptspring-boot

解决方案


推荐阅读