如何通过设置内容类型来保存.xlsx文件

时间:2017-11-14 10:56:21

标签: java jsp

在下面给出的jsp中尝试了代码。下面的代码打开窗口浏览器以将文件保存到.xlsx(使用chrome浏览器),但要求是在单击类似问题的图标.tried解决方案时自动下载文件。发布在堆栈溢出但无法找到问题的正确解决方案。

在浏览器Firefox和Chrome中检查解决方案

<%@ taglib uri="/struts-tags" prefix="s"%>
<%
    response.setContentType("application/application/vnd.openxmlformats-
     officedocument.spreadsheetml.sheet");
     response.setHeader ("Content-Disposition", 
     "attachment;fileName=tempAuthorizationCloseOutReportsResult.xlsx;");
 %>

 <s:set var="resultList" value="#request.tempAuthorizationCloseoutResult" />


 <div>
    <h3></h3>
 </div>
<br/>
<s:if test="#resultList.size <= 0">
     <table>
      <tr>
        <td>    
            <b> <s:text name="ui.label.text.norecordsfound" /> </b>
        </td>
      </tr>
    </table>
</s:if>

<s:elseif test="#resultList.size> 0">
 <table>
  <tr>
    <td>    
        <b><s:text name="ui.label.text.totalnumberofrecordsfound"/> : 
        <s:property value="#resultList.size" /> </b>
    </td>
  </tr>
</table>

2 个答案:

答案 0 :(得分:2)

 @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8");

        File xfile = < File>

        try (BufferedOutputStream bfos = new BufferedOutputStream(response.getOutputStream());
                FileInputStream fs = new FileInputStream(xfile)) {
            byte[] buffer = new byte[fis.available()];
            fs.read(buffer);

            bfos.write(buffer, 0, buffer.length);
            bfos.flush();
        }
 }

答案 1 :(得分:0)

在这里,我已经向您展示了使用javascript ajax进行文件下载,因为jquery ajax不支持这种情况。

服务器:

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {

            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8");

            File xlfile = <You File>

            try (BufferedOutputStream bfos = new BufferedOutputStream(response.getOutputStream());
                    FileInputStream fis = new FileInputStream(xlfile)) {
                byte[] buffer = new byte[fis.available()];
                fis.read(buffer);

                bfos.write(buffer, 0, buffer.length);
                bfos.flush();
            }
  }

ajax前端调用:

var download = function (path) {
    var xhr = new XMLHttpRequest();
    xhr.open('POST', encodeURI('/<servlet-path>'), true);
    xhr.responseType = 'blob';
    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=utf-8');
    xhr.onload = function (e) {
        if (this.status === 200) {
            var blob = new Blob([this.response], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
            var downloadUrl = URL.createObjectURL(blob);
            var a = document.createElement("a");
            a.href = downloadUrl;
            a.download = "file name need to be change";
            document.body.appendChild(a);
            a.click();
        } else if (this.status === 204) {
            window.location = '/login';
        } else {
            // errorNotify('Unable to download quotation, Please try again later.');
        }
    };
    xhr.send("");
};

执行前更改为适当的值。

<强>更新

除非您明确更改浏览器设置和配置,否则您无法更改浏览器行为。我不是说这些配置是每个人都可以改变的常见设置。