您的位置:首页 > 编程语言 > Java开发

Can not find a java.io.InputStream with the name [inputStream] in the invocation stack

2013-12-05 19:17 691 查看



今天实现Excel导出功能,结果总是提示下面的错误:

18:32:04,793 ERROR StreamResult:23 - Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action.

看下源码:

if (inputStream == null) {
// Find the inputstream from the invocation variable stack
inputStream = (InputStream) invocation.getStack().findValue(conditionalParse(inputName, invocation));
}
if (inputStream == null) {
String msg = ("Can not find a java.io.InputStream with the name [" + inputName + "] in the invocation stack. " + "Check the tag specified for this action.");
LOG.error(msg);
throw new IllegalArgumentException(msg);
}


我的代码是/**

* 打印excel

*/

@SuppressWarnings("unchecked")

public String createExcelLength() throws IOException {

System.out.println(inputStream);

List<Map> list = null;

Map map = new HashMap();// 装载分页函数需要的参数,里边包含每页多少条,第几页,查询条件

map.put("rows", (rows)); // 放入每页条数

map.put("page", (this.page)); // 放入当前页码

map.put("begdate", this.begdate); // 放入开始时间

map.put("enddate", this.enddate); // 放入结束时间

map.put("chanelId", this.chanelId); // 放入频道id

map.put("saleId", this.saleId); // 放入sale_id

list = amsRemainLengthService.selectAmsMeuInfo(map);

System.out.println("数据长度:" + list.size());

String columnNameArray = "销售品,月份,类型,1,2,3,4,5,6,7 ,8,9 ,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31";

String[] showNameAry = columnNameArray.split(",");

System.out.println(showNameAry.length);

HSSFWorkbook workbook = new HSSFWorkbook(); // 产生工作簿对象

HSSFSheet sheet = workbook.createSheet(); // 产生工作表对象

HSSFFont columnHeadFont = workbook.createFont();

columnHeadFont.setFontName("宋体");

columnHeadFont.setFontHeightInPoints((short) 12);

columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

// 列头的样式

HSSFCellStyle columnHeadStyle = workbook.createCellStyle();

columnHeadStyle.setFont(columnHeadFont);

columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中

columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

// 设置第一个工作表的名称为firstSheet

// 为了工作表能支持中文,设置字符编码为UTF_16

workbook.setSheetName(0, "firstSheet");

// 产生一行

HSSFRow row = sheet.createRow((short) 0);

// 产生第一个单元格

for (int i = 0; i < showNameAry.length; i++) {

HSSFCell cell = row.createCell(i);

// 设置单元格内容为字符串型

cell.setCellType(HSSFCell.CELL_TYPE_STRING);

cell.setCellValue(showNameAry[i]);

}

// 为了能在单元格中写入中文,设置字符编码为UTF_16。

// cell.setEncoding(HSSFCell.ENCODING_UTF_16);

int rowNum = 1;

// 循环往单元格中写入信息

// 根据list的size循环创建行

for (int i = 0; i < list.size(); i++) {

HSSFRow row1 = sheet.createRow((short) rowNum);

for (int j = 0; j < showNameAry.length; j++) {

HSSFCell cell1 = row1.createCell(j);

Map mapData = (Map) list.get(i);

// 设置单元格内容为字符串型

cell1.setCellType(HSSFCell.CELL_TYPE_STRING);

System.out.println(mapData.get(showNameAry[j]).toString());

cell1.setCellValue(mapData.get(showNameAry[j]).toString());

}

rowNum += 1;

}

this.filename = "AmsRemainLength.xls";

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

try {

workbook.write(outputStream);

inputStream = new ByteArrayInputStream(outputStream

.toByteArray());

outputStream.flush();

outputStream.close();

return "excel";

} catch (Exception e) {

e.printStackTrace();

return "excel";

}

}

调了半天断点到

cell1.setCellValue(mapData.get(showNameAry[j]).toString()); 抛出异常

纠结了半天最后在

mapData里看到columnNameArray对应的字段,我拿着中文去取数组里的英文,低级错误啊~!!!

最后改为String columnNameArray = "salename,mo,ty,1,2,3,4,5,6,7 ,8,9 ,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31";就取到

网上的解释为:

1.文件路径不对,根本就没有取到文件。这种情况下,可以将获得InputStream的那条语句放在system.out.println()中输出一下,若为null,那就是路径不对了,或者说得准确些就根本没有找到文件。

2.在action中没有写配置文件中"<paramname="inputName">"后面属性的那个get方法.

当以上两种情况都正确的情况下,问题就在这里了:

当采用 returnServletActionContext.getServletContext().getResourceAsStream("...")这种方法获得输入流的时候,要保证文件位置在 ServletContext 当中,就是说要在当前的应用上下文中,

如果想要获得外部文件 譬如 D盘中的某个文件,那么就要自己创建输入流才可以,如:

File file = new File("D:\\strust.doc");

InputStream is = new FileInputStream(file);

return is;

File file = new File("D:\\spring.doc"); InputStream is = newFileInputStream(file); return is;



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐