内存中创建一个图片,将其编码为png的输出流
@Controller
@Scope("prototype")//线程安全
public class PhotoAction {
private InputStream photo;
public InputStream getPhoto() {
return photo;
}
public void setPhoto(InputStream photo) {
this.photo = photo;
}
public String execute() throws IOException{
System.out.println("execute");
//照片,理解为大号的二维数组
BufferedImage img=new BufferedImage(400, 300, BufferedImage.TYPE_3BYTE_BGR);
img.setRGB(0, 0,0xffff00);
//将照片编码为PNG格式的数组
ByteArrayOutputStream out=new ByteArrayOutputStream();
ImageIO.write(img, "png", out);
out.close();
byte[] png = out.toByteArray();
//再将数组包装为InputStream
photo=new ByteArrayInputStream(png);//byte当流来读取
return "success";
}
}
Struts.xml配置文件
<struts>
<package name="demo" extends="json-default" namespace="/demo">
<action nam