2015-10-05 49 views
1

我正在發送一個json對象給servlet並通過函數調用servlet。這是我的jsp頁面和功能:無法獲取在servlet中工作的getOutputStream()

<script type="text/javascript"> 
function retrieveTicketsExcel(){ 
    var jsonData = {}; 
    jsonData["tower"]=$('#appTower').val(); 
    jsonData["sDate"]=$('#startDate').val(); 
    jsonData["eDate"]=$('#endDate').val(); 
    jsonData["apps"]=$('#appfilter').html(); 
    jsonData["duration"]=$('#duration').val(); 
    jsonData["severity"]=$('#severity').val(); 
    jsonData["releasetype"]=$('#releasetype').val(); 
    jsonData["query"]=$('#query').val(); 

     $.ajax 
      ({ 
       type: "GET", 
       url:"retrieveTicketsToExcel.htm", 
       data:'requestData='+JSON.stringify(jsonData), 
       dataType: "json", 

      }); 
     } 
</script> 
    <div class="modal-footer"> 
       <input type="button" class="btn btn-default" value="Export" onclick="javascript:retrieveTicketsExcel();"> 
       <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
      </div> 

現在我調用Servlet的方法是:

@RequestMapping(value = "/retrieveTicketsToExcel.htm", method = RequestMethod.GET) 
public void retrieveTickets(@RequestParam("requestData") String requestData, HttpServletRequest request, HttpServletResponse response) throws IOException { 
    // UserDAO userDAO = new UserDAO(); 
    // userDAO.setDataSource(dataSource); 
    System.out.println("*****************/GRTBDashboard/retrieveTicketsToExcel****************"); 
    Map<String, String> requestMap = new Gson().fromJson(requestData, Map.class); 

    response.reset(); 
    response.resetBuffer(); 

    response.setContentType("application/vnd.ms-excel"); 
    response.addHeader("Cache-control", "no-cache"); 
    response.setHeader("Content-disposition", "Attachment;filename=\"Ticket_Details.xls\""); 
    ServletOutputStream fileOut = response.getOutputStream(); 

    System.out.println("retrieveTickets - > " + requestData); 
    List<DashboardData> dashboardDataList = null; 
    String tower = requestMap.get("tower"); 
    System.out.println(tower); 
    try { 
     dashboardDataList = userDAO.retrieveTickets(requestMap); 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 

    final double noOfRecords = dashboardDataList.size(); 
    double count = Math.ceil(noOfRecords/30000); 
    try { 

     HSSFWorkbook workbook = new HSSFWorkbook(); 
     short dateFormat = workbook.createDataFormat().getFormat("YYYY-MM-DD HH:mm"); 

     Iterator<DashboardData> iterator = dashboardDataList.iterator(); 

     for (int i = 1; i <= count; i++) { 
      int c1 = 1; 
      HSSFSheet sheet = workbook.createSheet(tower); 

      HSSFCellStyle style = workbook.createCellStyle(); 
      HSSFFont font = workbook.createFont(); 
      font.setFontName("Verdana"); 
      style.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index); 
      style.setFillPattern(style.SOLID_FOREGROUND); 
      font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 
      font.setColor(HSSFColor.BLACK.index); 
      style.setFont(font); 
      HSSFRow header = sheet.createRow(0); 
      ; 

      header.createCell((short) 0).setCellValue(new HSSFRichTextString("Type")); 
      header.getCell((short) 0).setCellStyle(style); 

      header.createCell((short) 1).setCellValue(new HSSFRichTextString("Ticket Number")); 
      header.getCell((short) 1).setCellStyle(style); 

      header.createCell((short) 2).setCellValue(new HSSFRichTextString("Open Date")); 
      header.getCell((short) 2).setCellStyle(style); 

      header.createCell((short) 3).setCellValue(new HSSFRichTextString("Closed Date")); 
      header.getCell((short) 3).setCellStyle(style); 

      header.createCell((short) 4).setCellValue(new HSSFRichTextString("Reported By")); 
      header.getCell((short) 4).setCellStyle(style); 

      header.createCell((short) 5).setCellValue(new HSSFRichTextString("Severity")); 
      header.getCell((short) 5).setCellStyle(style); 

      header.createCell((short) 6).setCellValue(new HSSFRichTextString("Assigned App")); 
      header.getCell((short) 6).setCellStyle(style); 

      header.createCell((short) 7).setCellValue(new HSSFRichTextString("Status")); 
      header.getCell((short) 7).setCellStyle(style); 

      int rowIndex = 1; 

      while (iterator.hasNext() && c1 <= 30000) { 
       DashboardData dashboardData = iterator.next(); 
       System.out.println("Data : "+dashboardData.getTicketNumber()); 
       HSSFRow row = sheet.createRow(rowIndex++); 

       HSSFCell cell0 = row.createCell((short) 0); 
       cell0.setCellValue(new HSSFRichTextString(dashboardData.getType())); 

       HSSFCell cell1 = row.createCell((short) 1); 
       cell1.setCellValue(new HSSFRichTextString(dashboardData.getTicketNumber())); 

       HSSFCell cell2 = row.createCell((short) 2); 
       cell2.setCellValue(new HSSFRichTextString(dashboardData.getOpenDate().toString())); 

       HSSFCell cell3 = row.createCell((short) 3); 
       cell3.setCellValue(new HSSFRichTextString(dashboardData.getClosedate().toString())); 

       HSSFCell cell4 = row.createCell((short) 4); 
       cell4.setCellValue(new HSSFRichTextString(dashboardData.getReportedBy())); 

       HSSFCell cell5 = row.createCell((short) 5); 
       cell5.setCellValue(new HSSFRichTextString(dashboardData.getPriority())); 

       HSSFCell cell6 = row.createCell((short) 6); 
       cell6.setCellValue(new HSSFRichTextString(dashboardData.getAssignedGroup())); 

       HSSFCell cell7 = row.createCell((short) 7); 
       cell7.setCellValue(new HSSFRichTextString(dashboardData.getStatus())); 
       c1++; 

      } 
      for (int autosize = 0; autosize <= 10; autosize++) { 

       sheet.autoSizeColumn((short) autosize); 
      } 

     } 

     workbook.write(fileOut); 
    } catch (FileNotFoundException e2) { 
     e2.printStackTrace(); 
    } finally { 
     if (null != fileOut) 
      fileOut.close(); 
    } 
    fileOut.close(); 
    fileOut.flush(); 
} 

我試圖衝出數據爲Excel,我正在寫,但是,點擊導出按鈕的功能幾乎都在jsp頁面上下載。 如果我正在嘗試使用FileOutputStream()進行編寫,它在我的光盤上寫入完美。但我想用response.getOutputStream()在jsp頁面下載。

+0

您是否顯示錯誤? 這可能是一個例外,因爲在關閉後終於關閉和沖洗 –

+0

我沒有得到任何錯誤。我甚至試圖刪除finally塊,但沒有使用 –

+0

當你點擊你的按鈕時發生了什麼?你甚至知道你的控制器的方法是否被實際調用? –

回答

0

也許問題出在JQuery方面?

您可以通過在瀏覽器中手動打開retrieveTicketsToExcel.htm來下載工作嗎?如果是這樣,那麼嘗試使用window.location.href="retrieveTicketsToExcel.htm"而不是JQuery的ajax();

編輯: 我現在很確定問題是在查詢方面。首先,ajax請求不能啓動文件下載對話框。 window.location.href或提交表格是您的選擇。

另外,您在ajax調用中將json作爲dataType提供。這是錯誤的,因爲的dataType指定你從服務器期待回的數據,你發送請求的不是格式的類型(http://api.jquery.com/jquery.ajax/

EDIT2:

您使用的data:'requestData='+JSON.stringify(jsonData)也有問題。你不能簡單地將JSON字符串附加到一個url。你需要先對它進行編碼。嘗試使用 data:'requestData='+ encodeURI(JSON.stringify(jsonData))

或更好的是,使用POST請求,因爲這在語義上似乎更正確。這樣你不需要requestData=部分,但也必須稍微改變servlet代碼。您需要直接從Servlet inputStream或reader讀取json。

類似這樣的: new Gson().fromJson(request.getReader(), Map.class);;

+0

'window.location.href =「retrieveTicketsToExcel.htm」'給我找不到頁面。 –

+0

哎呀,你應該使用'window.location.href =「/ retrieveTicketsToExcel.htm」;' – Arnelism

+0

我試過兩個,但得到'客戶端發送的請求語法不正確()。', –