Struts 2框架下载png、excel等格式的文件

BufferedImage动态创建图片在Struts 2框架下载图片

步骤:

step1. 编写控制器
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

public class ImageAction {
private InputStream image;
public InputStream getImage() {
return image;
}
public void setImage(InputStream image) {
this.image = image;
}

public String execute(){
    //生成图片...
    try {
        byte[] data = createPngImage();
        image = new ByteArrayInputStream(data);
        return "img";
    } catch (IOException e) {
        e.printStackTrace();
        return "error";
    }
}

public byte[] createPngImage() throws IOException{
    //创建 BufferedImage对象
    BufferedImage img = new BufferedImage(400, 200, BufferedImage.TYPE_3BYTE_BGR);
    img.setRGB(0, 0, 0xff0000); 
    img.setRGB(100, 500, 0x00ff00); 
    img.setRGB(200, 100, 0xffff00); 
    img.setRGB(300, 150, 0x0000ff); 
    //利用API将图片对象编码为png,out(变长byte数组)
    ByteArrayOutputStream out=new ByteArrayOutputStream();
    ImageIO.write(img, "png", out);
    //再获取PNG数据数组
    out.close();
    //从out中取出数据(byte[])
    byte[] data = out.toByteArray();
    return data;
}

}
step2. 配置: struts.xml


image
image/png


/WEB-INF/jsp/error.jsp

contentType要查阅tomcat/config/web.xml后缀是png的媒体类型

step3. 重新部署,测试

Apache POI提供的API在Struts 2框架下新建工作簿/表

Stream

Stream不仅可以动态图片, 它还可以处理任何类型的文件, 比如处理Excel文件.

步骤:

step1. 导入Excel API: Apache POI(下载:http://maven.aliyun.com)

    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.9</version>
    </dependency>

step2. 编写控制器
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExcelAction {
private InputStream excel;

public InputStream getExcel() {
    return excel;
}
public void setExcel(InputStream excel) {
    this.excel = excel;
}

public String execute(){
    try {
        byte[] data = createExcel();
        excel=new ByteArrayInputStream(data);
        return "exl";
    } catch (IOException e) {
        e.printStackTrace();
        return "error";
    }
}

public byte[] createExcel() throws IOException{
    // Apache poi
    // 工作簿   >工作表>行>格子
    //workbook> sheet>row>cell

    //创建工作簿
    HSSFWorkbook workbook=new HSSFWorkbook();
    //在工作簿中创建工作表
    HSSFSheet sheet = workbook.createSheet("演示");
    //工作表里创建数据行
    HSSFRow row = sheet.createRow(0);
    //在行中创建格子
    HSSFCell c1 = row.createCell(0);
    //在格子中添加内容
    c1.setCellValue("学号");
    row.createCell(1).setCellValue("姓名");
    row.createCell(2).setCellValue("语文");
    row.createCell(3).setCellValue("数学");
    row.createCell(4).setCellValue("英语");
    //再添加一行
    row = sheet.createRow(1);
    row.createCell(0).setCellValue("A001");
    row.createCell(1).setCellValue("张三");
    row.createCell(2).setCellValue("90");
    row.createCell(3).setCellValue("99");
    row.createCell(4).setCellValue("89");
    //将excel对象转换为数据
    ByteArrayOutputStream out= new ByteArrayOutputStream();
    workbook.write(out);
    out.close();
    byte[] data = out.toByteArray();
    return data;
}

}

step3. 配置: struts.xml

    <action name="excel" 
         class="cn.app.action.ExcelAction">
         <result  name="exl" type="stream">
            <param name="inputName">excel</param>
            <param name="contentType">application/vnd.ms-excel</param>
            <param name="contentDisposition">attachment;filename="hello.xls"</param>
         </result>
         <result name="error">
            /WEB-INF/jsp/error.jsp
         </result>
    </action>
> contentType要查阅tomcat/config/web.xml后缀是xls的媒体类型
> 其中 contentDisposition 属性用于指定下载文件名

step4. 重新部署,测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值