首页 > 解决方案 > 通过jsp错误生成从服务器获取的docx文件

问题描述

我正在尝试使用 jsp 返回从服务器获取的 docx 文件

if(pathfile != null){
        String mimetype=application.getMimeType(pathfile);
        System.out.println("mimeType: "+ mimetype);
        response.setContentType("application/ms-word");     
        response.setHeader("Content-disposition", "attachment; filename=Resultfile.docx");

        File file = new File(pathfile);
        FileInputStream fileIn = new FileInputStream(file);
        //OutputStream output = response.getOutputStream();

        int i;   
        while ((i=fileIn.read()) != -1) {  
        out.write(i);   
      }   
      fileIn.close(); 

    }else{

        %> <div>Khong co file</div> <%

    }

但是当我尝试打开文件时,MSW 告诉我这个 错误 1 ​​错误 2

单击确定后我仍然可以阅读内容,但我不知道是什么问题,我该如何解决?我正在使用 word 2016,服务器是 websphere V9

标签: javajspservletsjakarta-eewebsphere

解决方案


代替

response.setContentType("application/ms-word");

利用

response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");

因为您的文件类型是 doc x

请参阅此处的microsoft 文档。


推荐阅读