2017-07-31 21 views
0

サービスを呼び出し、xlsxを生成してapache-poiを使用してビューを返すコントローラメソッドです。ダウンロードされたspring mvcを持つXlsxファイルが壊れていて、コンテンツタイプが変更されました

@RequestMapping(value = "/getSummary", method = RequestMethod.GET) 
public String getSummary(Model model, HttpServletRequest request, 
              HttpServletResponse response, 
              @ModelAttribute("dateFrom") String dateFrom, 
              @ModelAttribute("dateTo") String dateTo) throws Exception { 

    logger.debug("date=" + dateFrom + "TO date=" + dateTo); 
    AuthParam authParam = (AuthParam) request.getSession().getAttribute("authParam"); 
    if (authParam == null) { 
     model.addAttribute("success", false); 
     model.addAttribute("error_msg", "Auth Failed."); 
     return "shipment/getSummary"; 
    } 
    try { 
     List<SummaryResponse> summaryBeanResponse = reportClient.getSummary(authParam, dateFrom, dateTo); 
     logger.debug("Result List Size : {}", summaryBeanResponse.size()); 
     if (summaryBeanResponse.size() > 0) { 
      byte[] reportBytes = buildSummaryXlsx(summaryBeanResponse, dateTo); 
      logger.debug("Xlsx Sheet Generated!!"); 
      String fileName = "summary_" + StringUtils.join(dateTo.split("-"), "_") + ".xlsx"; 
      model.addAttribute("success", true); 
      response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); 
      response.setHeader("content-disposition", "attachment; filename=" + fileName); 
      logger.debug("Content-Type : {}", response.getContentType()); 
      logger.debug("Header : {}", response.getHeader("Content-Disposition")); 
      response.getOutputStream().write(reportBytes); 
      logger.debug("Set ALL"); 
     } else { 
      model.addAttribute("success", false); 
      model.addAttribute("error_msg", "No data found by search."); 
     } 
    } catch (Exception ex) { 
     model.addAttribute("success", false); 
     model.addAttribute("error_msg", "Service error."); 
     logger.error("ERROR on report xlsx", ex); 
    } 
    return "shipment/getSummary"; 
} 

private byte[] buildSummaryXlsx(List<SummaryResponse> summaryResponseList, String date) throws Exception { 
    try { 
     Workbook workbook = new XSSFWorkbook(); 
     // create excel xls sheet 
     XSSFSheet sheet = (XSSFSheet) workbook.createSheet("summary"); 
     sheet.setDefaultColumnWidth(14); 

     // create style for header cells 
     XSSFCellStyle headerStyle = (XSSFCellStyle) workbook.createCellStyle(); 
     Font font = workbook.createFont(); 
     font.setFontName("Arial"); 
     headerStyle.setFont(font); 
     headerStyle.setAlignment(HorizontalAlignment.CENTER); 
     headerStyle.setVerticalAlignment(VerticalAlignment.CENTER); 

     int rowCount = 0; 
     int cellCount = 0; 

     // Manipulating Data Using Apache-POI 

     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
     workbook.write(byteArrayOutputStream); 
     return byteArrayOutputStream.toByteArray(); 
    } catch (Exception ex) { 
     logger.debug("Exception On Population Xlsx Sheet, {}", ex); 
    } 
    return null; 
} 

このJSPビュー名あるgetSummary.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<html> 
<head> 
    <title>Get Xlsx report</title> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      var isSuccess = ${success}; 
      if (isSuccess == false) { 
       var msg = "${error_msg}"; 
       alertify.alert(msg, function() { 
        window.location = "<%= request.getContextPath() %>/shipment/view.html"; 
       }); 
      } 
     }); 
    </script> 
</head> 
<body> 
${error_msg} 
</body> 
</html> 

私は、コンテンツタイプが、応答ヘッダーは

**Response Headers** 
HTTP/1.1 200 OK 
Cache-Control: no-cache 
Cache-Control: no-store 
Date: Mon, 31 Jul 2017 12:26:12 GMT 
Pragma: no-cache 
Transfer-Encoding: chunked 
Content-Type: text/html;charset=UTF-8 
Expires: Thu, 01 Jan 1970 00:00:00 GMT 
Content-Disposition: attachment; filename=summary_2017_07_31.xlsx 
Set-Cookie: JSSESSIONID=1501503972634; path=/ 
Content-Language: en 

プリントログのように見え設定しています:

2017-07-31 18:26:12,635 DEBUG [admin] (ShipmentController.java:522) - REPORT date=2017-07-1TO date=2017-07-31 
    2017-07-31 18:26:12,635 DEBUG [admin] (ReportServiceClient.java:178) - Calling [ReportService.getSummary] service for dateFrom[2017-07-1] and dateTo[2017-07-31] 
    2017-07-31 18:26:12,671 DEBUG [admin] (ShipmentController.java:531) - Result List Size : 4 
    2017-07-31 18:26:12,824 DEBUG [admin] (ShipmentController.java:534) - Xlsx Sheet Generated!! 
    2017-07-31 18:26:12,824 DEBUG [admin] (ShipmentController.java:539) - Content-Type : application/vnd.openxmlformats-officedocument.spreadsheetml.sheet 
    2017-07-31 18:26:12,824 DEBUG [admin] (ShipmentController.java:540) - Header : attachment; filename=summary_2017_07_31.xlsx 
    2017-07-31 18:26:12,824 DEBUG [admin] (ShipmentController.java:542) - Set ALL 

xlsxファイルをダウンロードできますが、データが破損しています。実際には、Webページのcontent/htmlページビューはxlsxファイルで記述されています。

データがない場合ポップアップを表示したい場合、データがある場合は、作成したxlsxをダウンロードしたいと思います。

私はコンテンツタイプを設定した後にそれがなぜ変わるのですか? どうすればこの問題を適切に解決できますか?

NB: 私はpdfファイルを作成し、コントローラメソッドの同様のタイプを持っているとresponse.setContentType("application/pdf") にコンテンツタイプを設定し、それは私のために正常に動作しています。

+2

クライアントに何かをストリームするか、Webページに移動します。あなたはいつもウェブページに行きます。あなたはそのような単一の接続で2つのことをすることはできません。 –

答えて

1

別のコントローラ@RequestMappingを追加すると、@ResponseBodyと注釈されたXLSXファイルバイトのみが返されます。このエンドポイントへのハイパーリンクをJSPの本文から作成して、ユーザーがクリックするとファイルをダウンロードできるようにすることができます。含まれているJSPの戻り値の型がtext/htmlに設定されているため、単一のRequestMappingにJSP本体とXLSXコンテンツの両方を返すことはできません。

関連する問題