struts2导出excel无法找到InputStream
错误:java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [] in the invocation stack. Check the <param name="inputName"> tag specified for this action
说是我栈中没有excelStream。
环境:struts2.3.15+spring3+hibernate4
其他环境有:druid+javamelody
使用的是jxl导出excel
从数据库查询数据,不是读取的文件。
action:
public String filename;
public InputStream excelStream;
get、set方法省略....
public String exportExcel() {
ByteArrayOutputStream targetFile = new ByteArrayOutputStream();
try {
WritableWorkbook workbook = Workbook.createWorkbook(targetFile);
WritableSheet sheet = workbook.createSheet("测试报告", 0);
......其他省略
workbook.write();
workbook.close();
} catch (Exception e) {
System.out.println("在输出到EXCEL的过程中出现错误,错误原因:" + e.toString());
}
excelStream = new ByteArrayInputStream(targetFile.toByteArray());
System.out.println("===================================="+excelStream);
}
<result name="excel" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="inputName">excelStream</param>
<param name="contentDisposition">attachment;filename="test.xls"</param>
<param name="bufferSize">1024</param>
</result>
<!-- 导出excel -->
<action name="daochuexcel" class="com.wuliuguanli.action.DaochuExcelAction">
<result name="excel" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="contentDisposition"> attachment;filename="${fileName}.xls"</param>
<param name="inputName">excelStream</param>
</result>
</action>
package com.wuliuguanli.action;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.annotation.Resource;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;
import com.wuliuguanli.WuliuguanliAction;
import com.wuliuguanli.dao.WuliuguanliDao;
import com.wuliuguanli.entity.Business;
import com.wuliuguanli.util.WuliuguanliException;
@Controller
public class DaochuExcelAction extends WuliuguanliAction{
@Resource WuliuguanliDao dao;
//input
private String ids;
private String fileName;//文件名
//output
private String tempPath;//临时文件目录
public String execute(){
initExcel();
return "excel";
}
public void initExcel(){
String[] ids1=ids.split("-");
//System.out.println(ids1.length);
//for(int i=0;i<ids1.length;i++){
//System.out.println(ids1[i]);
//}
List<Business> bs=new ArrayList<Business>();
int i=0;
for(i=0;i<ids1.length;i++){
Business b=new Business();
try {
b=dao.findBusinessById(Integer.parseInt(ids1[i]));
if(null!=b){
b.setState(1);
dao.insertBusiness(b);
bs.add(b);
}
//模版的绝对路径
String path="/Edu/exceltemplet/";
String filepath=ServletActionContext.getServletContext().getRealPath(path+fileName+".xls");
FileInputStream fis=new FileInputStream(filepath);
ExcelUtils eu=new ExcelUtils();
tempPath=eu.exportExcel(fis, "temp/"+createFileName(), fileName, bs);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WuliuguanliException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public InputStream getExcelStream() {
return ServletActionContext.getServletContext().getResourceAsStream("/Edu/exceltemplet/"+tempPath);
}
/**
* 以年月日时分秒毫秒+4位随机数的格式来创建一个文件名,不带扩展名
* @return 文件名
*/
public static String createFileName() {
StringBuffer sb = new StringBuffer();
Date date = new Date();
//获取年月日时分秒
sb.append(new SimpleDateFormat("yyyyMMddHHmmss").format(date));
//毫秒
String milli = String.valueOf(date.getTime() % 1000);
while (milli.length() < 3) {
milli = "0" + milli;
}
sb.append(milli);
//四位随机数
String rondom = String.valueOf(new Random().nextInt(10000));
while (rondom.length() < 4) {
rondom = "0" + rondom;
}
sb.append(rondom);
return sb.toString();
}
public String getIds() {
return ids;
}
public void setIds(String ids) {
this.ids = ids;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getTempPath() {
return tempPath;
}
public void setTempPath(String tempPath) {
this.tempPath = tempPath;
}
}
package com.wuliuguanli.action;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.apache.struts2.ServletActionContext;
import com.wuliuguanli.entity.Business;
/**
* 操作excel工具类
* @author think
*
*/
public class ExcelUtils {
/**
* @param sheet 要添加数据的工作表
* @param list 填充的数据
*/
public void addCellOfBug(WritableSheet sheet, List<Business> bs,int count,int page) {
if(count<=20){
try {
for(int i=0;i<bs.size();i++){
sheet.addCell(new Label(0,8+i,i+1+""));
sheet.addCell(new Label(1,8+i,bs.get(i).getProductName()));
sheet.addCell(new Label(2,8+i,bs.get(i).getSonghuoren()));
sheet.addCell(new Label(3,8+i,bs.get(i).getPeopleByReceiverPeopleId().getName()));
sheet.addCell(new Label(4,8+i,bs.get(i).getPeopleByReceiverPeopleId().getPhone()));
sheet.addCell(new Label(5,8+i,bs.get(i).getPeopleByReceiverPeopleId().getAddress()));
sheet.addCell(new Label(7,8+i,bs.get(i).getNumber()+""));
sheet.addCell(new Label(8,8+i,bs.get(i).getBeizhu()));
}
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
try {
for(int i=20;i<bs.size();i++){
sheet.addCell(new Label(0,8+i-20,i+1+""));
sheet.addCell(new Label(1,8+i-20,bs.get(i).getProductName()));
sheet.addCell(new Label(2,8+i-20,bs.get(i).getSonghuoren()));
sheet.addCell(new Label(3,8+i-20,bs.get(i).getPeopleByReceiverPeopleId().getName()));
sheet.addCell(new Label(4,8+i-20,bs.get(i).getPeopleByReceiverPeopleId().getPhone()));
sheet.addCell(new Label(5,8+i-20,bs.get(i).getPeopleByReceiverPeopleId().getAddress()));
sheet.addCell(new Label(7,8+i-20,bs.get(i).getNumber()+""));
sheet.addCell(new Label(8,8+i-20,bs.get(i).getBeizhu()));
}
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 得到实际保存文件根目录
*/
public static String getRootPath() {
return ServletActionContext.getServletContext().getRealPath("").replace("\", "/") + "/Edu/exceltemplet/";
}
/**
* 输出excel
* @param is 原始excel模版输入流
* @param path 临时文件目录
* @param fileName 文件名
* @param list 要填充的数据
* @return 返回相对临时文件的目录
*/
public String exportExcel(InputStream is, String path, String fileName, List<Business> fahuo) {
//临时目录,用于生成临时文件
String tempPath = null ;
WritableWorkbook wb = null;
File f = new File(getRootPath() + path);
//不存在则创建它
if (!f.exists())
f.mkdirs();
tempPath = getRootPath() + path + "/" + fileName + ".xls";
final File file = new File(tempPath);
OutputStream oss = null;
try {
//创建临时文件
if(file.createNewFile()){
oss = new FileOutputStream(file);
wb = Workbook.createWorkbook(oss, Workbook.getWorkbook(is));
int count=fahuo.size();//得到fahuo物品中的组件个数
if(count<=20){//分页 曹传奎
WritableSheet sheet = wb.getSheet(0);
if ("sample".equals(fileName)) {
addCellOfBug(sheet, fahuo,count,count/20);
}
wb.write();
}else{
WritableSheet[] sheet = wb.getSheets();
if ("sample".equals(fileName)) {
addCellOfBug(sheet[0], fahuo,count,count/20-1);
addCellOfBug(sheet[1], fahuo,count,count/20);
}
wb.write();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
wb.close();
oss.flush();
oss.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/***
*
* Thread.sleep(20000) 只是测试用的,当然实际情况可以设置为1小时,差不多了
* 还有在tomcat启动时,就删除Edu/exceltemplet/temp下所有文件,避免临时文件夹及文件某些特殊原因没有删除而导致太多
* **/
//过一段时间之后,删除临时文件
new Thread(new Runnable() {
public void run() {
try {
// 线程睡20秒
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 删除临时文件
file.delete();
}
}).start();
return path + "/" + fileName + ".xls";
}
}