struts2 的转发问题 最近在做一个下载的东西,用的是struts2在action里面生成的是一个csv格式的文件,但为了让文件名不重复,所以用的是一个时间戳做为文件名的,请问一下struts2里有没有servlet中的sendredirect请求转发这个功能呢?因为这个可以直接跳转到csv的地址,可以直接跳出下载的窗口,谢谢.... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 那你把跳转的<result type="redirect">xxx.csv</result> 没用,因为我的文件名是时间戳,可变的,无法固定。再说,按照你这样的做法,也只是提示一个下载为action后缀的文件 实在没办法那就得用servlet去做生成文件了。 你是不是想做struts的文件下载啊,如果是这样,那你用<result type="stream"> <param name="contentType">application/vnd.ms-excel</param> <param name="contentDisposition">attachment;filename="${filename}"</param> <param name="inputName">downloadFile</param> </result>这个是下载excel的配置filename就是你下载弹出时的文件名用${filename}代表action中必须有getFilename();在action中你必须要有个get+你配置中inputName的返回InputStream的方法。package sdbusiness.sd_order.action;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.util.List;import jbpmds.util.DateUtil;import jxl.Workbook;import jxl.format.Colour;import jxl.format.UnderlineStyle;import jxl.write.Label;import jxl.write.Number;import jxl.write.WritableCellFormat;import jxl.write.WritableFont;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;import jxl.write.WriteException;import jxl.write.biff.RowsExceededException;import sdbusiness.sd_order.bean.Sd_order;import sdbusiness.sd_order.service.OrderService;import sdbusiness.sd_order.service.OrderServiceImpl;import com.opensymphony.xwork2.ActionSupport;@SuppressWarnings("serial")public class CreateExcelAction extends ActionSupport { // service private OrderService os = new OrderServiceImpl(); // 输入输出 private String year; private String month; private String filename; private String goods_amount; private String order_amount; public InputStream getDownloadFile() { ByteArrayOutputStream bos =createExcel(); InputStream is = null; is = new ByteArrayInputStream(bos.toByteArray()); try { bos.close(); } catch (IOException e) { e.printStackTrace(); } return is; } @Override public String execute() throws Exception { return SUCCESS; } private ByteArrayOutputStream createExcel(){ String startDate = year + "-" + month + "-1"; List<Sd_order> list = os.listOrderByMonth(startDate, Sd_order.received, -1, -1); int size = list.size(); filename = year + "年" + month + "月订单统计.xls"; ByteArrayOutputStream bos = new ByteArrayOutputStream(); WritableWorkbook wwb = null; try { wwb = Workbook.createWorkbook(bos); WritableSheet sheet = wwb.createSheet(year + "年" + month + "月订单统计", 0); //给标题字体渲染 WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.RED); WritableCellFormat wcf = new WritableCellFormat(wf); Label monthLabel1=new Label(0,0,"当前月份",wcf); Label goods_amountLabel1=new Label(1,0,"订单总价(不含邮费)",wcf); Label order_amountLabel1=new Label(2,0,"订单总价(含邮费)",wcf); Label monthLabel2=new Label(0,1,month); Label goods_amountLabel2=new Label(1,1,goods_amount); Label order_amountLabel2=new Label(2,1,order_amount); Label label0 = new Label(0, 2, "店铺名称", wcf); Label label1 = new Label(1, 2, "订单号", wcf); Label label2 = new Label(2, 2, "付款时间", wcf); Label label3 = new Label(3, 2, "买家名称", wcf); Label label4 = new Label(4, 2, "订单总价", wcf); Label label5 = new Label(5, 2, "支付方式", wcf); sheet.addCell(monthLabel1); sheet.addCell(monthLabel2); sheet.addCell(goods_amountLabel1); sheet.addCell(goods_amountLabel2); sheet.addCell(order_amountLabel1); sheet.addCell(order_amountLabel2); sheet.addCell(label0); sheet.addCell(label1); sheet.addCell(label2); sheet.addCell(label3); sheet.addCell(label4); sheet.addCell(label5); Label label = null; for (int i = 0; i < size; i++) { label = new Label(0, i + 3, list.get(i).getSeller_name()); sheet.addCell(label); label = new Label(1, i + 3, list.get(i).getOrder_sn()); sheet.addCell(label); String pay_time = DateUtil.IntegerToDateString(list.get(i) .getPay_time()); label = new Label(2, i + 3, pay_time); sheet.addCell(label); label = new Label(3, i + 3, list.get(i).getBuyer_name()); sheet.addCell(label); Number number = new Number(4, i + 3, list.get(i) .getOrder_amount().doubleValue()); sheet.addCell(number); label = new Label(5, i + 3, list.get(i).getPayment_name()); sheet.addCell(label); } wwb.write(); wwb.close(); } catch (IOException e) { e.printStackTrace(); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } return bos; } public String getYear() { return year; } public void setYear(String year) { this.year = year; } public String getMonth() { return month; } public void setMonth(String month) { this.month = month; } public String getFilename() { try { this.filename=new String(filename.getBytes(),"ISO-8859-1"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return filename; } public String getGoods_amount() { return goods_amount; } public void setGoods_amount(String goodsAmount) { goods_amount = goodsAmount; } public String getOrder_amount() { return order_amount; } public void setOrder_amount(String orderAmount) { order_amount = orderAmount; }} 生成的文件是csv,但是<param name="contentType">这个标签值怎么填? 你要知道csv的格式是什么样的可以去tomcat下的conf/web.xml里找里面有文本类型的有视频文件类型的有word类型的等等,你属于什么类型的就对应的其实这个填不对也不影响,这个只是你的文件的格式跟你系统上的哪个文件标识一样,反正下载下来的内容是正确的. ckeditor怎么固定其大小?求解!!! 关于jsp的自动更新问题 Java Web开发技术方案体验之旅 (活动分帖) 给个例子 救命啊 怎么在applet中引用一个类 java.lang.NoClassDefFoundError错误? IIS支持JSP吗? 如何在b/s模式的客户端用java创建文件? jsp + jstl标签做登录判断 web.xml java.lang.OutOfMemoryError: Java heap space 问题 JSP上传下载文件报错
如果是这样,那你用
<result type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="contentDisposition">attachment;filename="${filename}"</param>
<param name="inputName">downloadFile</param>
</result>
这个是下载excel的配置filename就是你下载弹出时的文件名用${filename}代表action中必须有getFilename();
在action中你必须要有个get+你配置中inputName的返回InputStream的方法。
package sdbusiness.sd_order.action;import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;import jbpmds.util.DateUtil;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;import sdbusiness.sd_order.bean.Sd_order;
import sdbusiness.sd_order.service.OrderService;
import sdbusiness.sd_order.service.OrderServiceImpl;import com.opensymphony.xwork2.ActionSupport;@SuppressWarnings("serial")
public class CreateExcelAction extends ActionSupport {
// service
private OrderService os = new OrderServiceImpl();
// 输入输出
private String year;
private String month;
private String filename;
private String goods_amount;
private String order_amount; public InputStream getDownloadFile() {
ByteArrayOutputStream bos =createExcel();
InputStream is = null;
is = new ByteArrayInputStream(bos.toByteArray());
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return is;
} @Override
public String execute() throws Exception {
return SUCCESS;
} private ByteArrayOutputStream createExcel(){
String startDate = year + "-" + month + "-1";
List<Sd_order> list = os.listOrderByMonth(startDate, Sd_order.received, -1, -1);
int size = list.size();
filename = year + "年" + month + "月订单统计.xls";
ByteArrayOutputStream bos = new ByteArrayOutputStream();
WritableWorkbook wwb = null;
try {
wwb = Workbook.createWorkbook(bos);
WritableSheet sheet = wwb.createSheet(year + "年" + month + "月订单统计", 0);
//给标题字体渲染
WritableFont wf = new WritableFont(WritableFont.ARIAL, 12,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
Colour.RED);
WritableCellFormat wcf = new WritableCellFormat(wf);
Label monthLabel1=new Label(0,0,"当前月份",wcf);
Label goods_amountLabel1=new Label(1,0,"订单总价(不含邮费)",wcf);
Label order_amountLabel1=new Label(2,0,"订单总价(含邮费)",wcf);
Label monthLabel2=new Label(0,1,month);
Label goods_amountLabel2=new Label(1,1,goods_amount);
Label order_amountLabel2=new Label(2,1,order_amount);
Label label0 = new Label(0, 2, "店铺名称", wcf);
Label label1 = new Label(1, 2, "订单号", wcf);
Label label2 = new Label(2, 2, "付款时间", wcf);
Label label3 = new Label(3, 2, "买家名称", wcf);
Label label4 = new Label(4, 2, "订单总价", wcf);
Label label5 = new Label(5, 2, "支付方式", wcf);
sheet.addCell(monthLabel1);
sheet.addCell(monthLabel2);
sheet.addCell(goods_amountLabel1);
sheet.addCell(goods_amountLabel2);
sheet.addCell(order_amountLabel1);
sheet.addCell(order_amountLabel2);
sheet.addCell(label0);
sheet.addCell(label1);
sheet.addCell(label2);
sheet.addCell(label3);
sheet.addCell(label4);
sheet.addCell(label5);
Label label = null;
for (int i = 0; i < size; i++) {
label = new Label(0, i + 3, list.get(i).getSeller_name());
sheet.addCell(label);
label = new Label(1, i + 3, list.get(i).getOrder_sn());
sheet.addCell(label);
String pay_time = DateUtil.IntegerToDateString(list.get(i)
.getPay_time());
label = new Label(2, i + 3, pay_time);
sheet.addCell(label);
label = new Label(3, i + 3, list.get(i).getBuyer_name());
sheet.addCell(label);
Number number = new Number(4, i + 3, list.get(i)
.getOrder_amount().doubleValue());
sheet.addCell(number);
label = new Label(5, i + 3, list.get(i).getPayment_name());
sheet.addCell(label);
}
wwb.write();
wwb.close();
} catch (IOException e) {
e.printStackTrace();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
return bos;
} public String getYear() {
return year;
} public void setYear(String year) {
this.year = year;
} public String getMonth() {
return month;
} public void setMonth(String month) {
this.month = month;
} public String getFilename() {
try {
this.filename=new String(filename.getBytes(),"ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return filename;
} public String getGoods_amount() {
return goods_amount;
} public void setGoods_amount(String goodsAmount) {
goods_amount = goodsAmount;
} public String getOrder_amount() {
return order_amount;
} public void setOrder_amount(String orderAmount) {
order_amount = orderAmount;
}}
里面有文本类型的有视频文件类型的有word类型的等等,你属于什么类型的就对应的
其实这个填不对也不影响,这个只是你的文件的格式跟你系统上的哪个文件标识一样,
反正下载下来的内容是正确的.