Struts2下载浏览器没响应,求指教解决问题思路


我看别人之前写的代码,发现仔细比对后一样的,而且与我自己的代码能替换部分进行了替换,发现还是不行。本地有Excel生成,并且数据正常,就是浏览器没反应。
大家先看我代码struts2的配置文件 图片描述

在看我的代码部分:
`
public InputStream getDownloadFile() throws Exception {


 // 第一步,创建一个webbook,对应一个Excel文件
            HSSFWorkbook wb = new HSSFWorkbook();
            // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
            HSSFSheet sheet = wb.createSheet("数据表");
            // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
            HSSFRow rowTitle = sheet.createRow((int) 0);
            HSSFRow rowTime = sheet.createRow((int) 1);
            HSSFRow rowEmpty = sheet.createRow((int) 2);
            HSSFRow row = sheet.createRow((int) 3);
            // 第四步,创建单元格,并设置值表头 设置表头居中
            HSSFCellStyle styleTitle = wb.createCellStyle();
            styleTitle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
            // 标题字体
            HSSFFont fontTitle = wb.createFont();
            // fontTitle.setColor(HSSFColor.VIOLET.index);
            fontTitle.setFontHeightInPoints((short) 15);
            fontTitle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            styleTitle.setFont(fontTitle);
            HSSFCellStyle style = wb.createCellStyle();
            // 表头字体
            HSSFFont font = wb.createFont();
            font.setFontHeightInPoints((short) 10);
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            font.setColor(HSSFColor.WHITE.index);// 白色头文字
            style.setFont(font);
            // 设置这些样式
            style.setFillForegroundColor(HSSFColor.DARK_TEAL.index);
            style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            style.setBorderRight(HSSFCellStyle.BORDER_THIN);
            style.setBorderTop(HSSFCellStyle.BORDER_THIN);
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            // 内容字体
            HSSFCellStyle styleContent = wb.createCellStyle();
            styleContent.setAlignment(HSSFCellStyle.ALIGN_LEFT);
            HSSFFont fontContent = wb.createFont();
            fontContent.setFontHeightInPoints((short) 9);
            styleContent.setFont(fontContent);
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    String tsStartTime = df.format(datestart);
    String tsEndTime = df.format(dateend);

    HSSFCell cellTilte = rowTitle.createCell((short) 0);
    cellTilte.setCellValue("易宝数据报表");
    cellTilte.setCellStyle(styleTitle);

    HSSFCell cellTime = rowTime.createCell((short) 0);
    cellTime.setCellValue("查询日期: " + tsStartTime + " - "+ tsEndTime);
    cellTime.setCellStyle(styleTitle);

    HSSFCell cell = row.createCell((short) 0);
    cell.setCellValue("序号");
    cell.setCellStyle(style);
    cell = row.createCell((short) 1);
    cell.setCellValue("下单时间");
    cell.setCellStyle(style);
    cell = row.createCell((short) 2);
    cell.setCellValue("支付时间");
    cell.setCellStyle(style);
    cell = row.createCell((short) 3);
    cell.setCellValue("商户订单号");
    cell.setCellStyle(style);
    cell = row.createCell((short) 4);
    cell.setCellValue("订单金额");
    cell.setCellStyle(style);
    cell = row.createCell((short) 5);
    cell.setCellValue("收款方名称");
    cell.setCellStyle(style);
    cell = row.createCell((short) 6);
    cell.setCellValue("订单状态");
    cell.setCellStyle(style);
    cell = row.createCell((short) 7);
    cell.setCellValue("支付类型");
    cell.setCellStyle(style);
    cell = row.createCell((short) 8);
    cell.setCellValue("大客户编码");
    cell.setCellStyle(style);
    cell = row.createCell((short) 9);
    cell.setCellValue("PNR");
    cell.setCellStyle(style);
    cell = row.createCell((short) 10);
    cell.setCellValue("起始票号");
    cell.setCellStyle(style);
    cell = row.createCell((short) 11);
    cell.setCellValue("终止票号");
    cell.setCellStyle(style);

    List<Object[]> lstData = SearchRes();// 获取要下载的数据
    List list = new ArrayList();
    int intNo = 1;
    for (Object[] obj : lstData) {
        AirLineDownloadDatas data = new AirLineDownloadDatas();
        data.setNo(String.valueOf(intNo));
        data.setOrderingTime(obj[0].toString());
        data.setPayTime(obj[1].toString());
        data.setMerchantOrderId(obj[2].toString());
        data.setOrderAmount(obj[3].toString());
        data.setPayeeName(obj[4].toString());
        if (obj[5].toString().equals("0")){
            data.setOrderState("等待出票");
        }else if (obj[5].toString().equals("1")){
            data.setOrderState("已支付");
        }else if (obj[5].toString().equals("2")){
            data.setOrderState("已出票");
        }else if (obj[5].toString().equals("3")){
            data.setOrderState("已退款");
        }else{
            data.setOrderType(obj[5].toString());
        }
        if (obj[6].toString().equals("0")){
            data.setOrderType("会员支付");
        }else if (obj[6].toString().equals("1")){
            data.setOrderType("银行卡支付");
        }else{
            data.setOrderType(obj[6].toString());
        }
        data.setBigCustomerId(obj[7].toString());
        data.setPNR(obj[8].toString());
        data.setTicketNoStart(obj[9].toString());
        data.setTicketNoEnd(obj[10].toString());
        list.add(data);
        intNo++;
    }
    for (int i = 0; i < list.size(); i++) {
        row = sheet.createRow((int) i + 4);// 从第四行开始
        AirLineDownloadDatas dataGet = (AirLineDownloadDatas) list.get(i);
        // 第四步,创建单元格,并设置值
        row.createCell((short) 0).setCellValue(dataGet.getNo());
        row.createCell((short) 1).setCellValue(dataGet.getOrderingTime());
        row.createCell((short) 2).setCellValue(dataGet.getPayTime());
        row.createCell((short) 3)
                .setCellValue(dataGet.getMerchantOrderId());
        row.createCell((short) 4).setCellValue(dataGet.getOrderAmount());
        row.createCell((short) 5).setCellValue(dataGet.getPayeeName());
        row.createCell((short) 6).setCellValue(dataGet.getOrderState());
        row.createCell((short) 7).setCellValue(dataGet.getOrderType());
        row.createCell((short) 8).setCellValue(dataGet.getBigCustomerId());
        row.createCell((short) 9).setCellValue(dataGet.getPNR());
        row.createCell((short) 10).setCellValue(dataGet.getTicketNoStart());
        row.createCell((short) 11).setCellValue(dataGet.getTicketNoEnd());
    }

    sheet.setColumnWidth((short) 0, 3500);
    sheet.setColumnWidth((short) 1, 5000);
    sheet.setColumnWidth((short) 2, 5000);
    sheet.setColumnWidth((short) 3, 5000);
    sheet.setColumnWidth((short) 4, 3500);
    sheet.setColumnWidth((short) 5, 3500);
    sheet.setColumnWidth((short) 6, 3500);
    sheet.setColumnWidth((short) 7, 2500);
    sheet.setColumnWidth((short) 8, 5000);
    sheet.setColumnWidth((short) 9, 2500);
    sheet.setColumnWidth((short) 10, 5000);
    sheet.setColumnWidth((short) 11, 5000);

    try {
        String strFn = java.util.UUID.randomUUID().toString()
                .replaceAll("-", "");
        String strOutFn = GlobalParams.strPath + strFn + ".xls";// "/mnt/temp/"
                                                                // + strFn +
                                                                // ".xls";//
        //这个地方在本地都已经生成了文件,数据也正常                                                    
        FileOutputStream fout = new FileOutputStream(strOutFn);
        wb.write(fout);
        fout.close();
        ActionContext.getContext().getSession().put("file", strOutFn);
        this.fileName = "Excel" + df.format(new Date()).toString() + ".xls";
        this.fileName = new String(this.fileName.getBytes("GBK"),
                "ISO-8859-1");
        //就是这块,debug调试没问题,不知道为啥浏览器就没反应
        //而且之前的代码也是这么写的,浏览器是可以的,不知道为啥突然就不行了
        File file = new File(strOutFn);
        this.fileLength = file.length() + "";
        InputStream is = new FileInputStream(file);
        return is;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

`
求大神给出解决问题的思路或指教下错误的地方,谢谢!

下载 java struts2 excel

瞪大我的星星眼 8 years, 9 months ago

Content-Type不能配成 text/plain 要配成 application/octet-stream

upset answered 8 years, 9 months ago

Your Answer