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. 重新部署,测试