2017-11-14 8 views
1

下記のコードはjspで試行されました。以下のコードは、ファイルを.xlsxに保存するためにウィンドウエクスプローラを開きます(Chromeブラウザを使用)。ファイルをクリックしている間、スタックオーバーフローで投稿された同様の質問のアイコン解決済みの解決策が、質問に対する正しい解決策を見つけることができませんでした。ブラウザFirefoxでクロムコンテンツタイプを設定して.xlsxファイルを保存する方法

ここ
<%@ 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> 
+1

ブラウザは何でもできます。サーバーはどこにでも自動的に保存するように指示することはできません。 –

答えて

0
@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(); 
     } 
} 
0

でチェック

ソリューション私はjqueryのAjaxはシナリオのこの種をサポートしていませんので、あなたは、JavaScriptの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(""); 
}; 

変更をあなたが実行する前に。

更新

明示的にブラウザの設定や構成を変更しない限り、あなたは、途中でブラウザの動作を変更することはできません。私は、これらの設定が誰もが変えることができる共通の設定だと言っているわけではありません。

+0

jsp自体で扱う可能性のあるコードはありますか – user8248649

+0

はい、JSPファイルもサーブレット仕様書に記載されているようにする必要があります。これを行うには、 '<%! ボイド_jspService(HttpServletRequestのリクエスト、HttpServletResponseの応答){// サービス処理コード...} %>ここで ' このリンクは詳細、[リンク]を有する(https://www.tutorialspoint.com/jsp /jsp_life_cycle.htm) –

関連する問題