ITPUB??ì3
ITPUB论坛 » Java web开发及框架技术 » Struts2中使用Stream Result Type

新一届的微软MVP评选已经开始,欢迎各位推荐!

标题: Struts2中使用Stream Result Type
离线 cyntha80
中级会员



精华贴数 1
个人空间 0
技术积分 4577 (296)
社区积分 2 (30298)
注册日期 2007-2-8
论坛徽章:20
开发板块每日发贴之星生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠
生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠

发表于 2008-7-3 19:18 
Struts2中使用Stream Result Type

Stream result type是Struts2中比较有用的一个feature。特别是在动态生成图片和文档的情况下;例如动态验证码,各种报表图片生成等。鉴于网上使用struts2生成动态验证码,struts2+jfreechart的例子中很少使用到该feature,这里以生成动态验证码为例解释stream result的使用:

Action类,action主要要提供一个获取InputStrem的方法Java代码
public class CheckCodeAction extends ActionSupport implements SessionAware {   
    private Logger log = LoggerFactory.getLogger(this.getClass());   
    private InputStream imageStream;   
    private Map session;   
  
    public String getCheckCodeImage(String str, int show, ByteArrayOutputStream output) {   
        Random random = new Random();   
        BufferedImage image = new BufferedImage(80, 30, BufferedImage.TYPE_3BYTE_BGR);   
        Font font = new Font("Arial", Font.PLAIN, 24);   
        int distance = 18;   
        Graphics d = image.getGraphics();   
        d.setColor(Color.WHITE);   
        d.fillRect(0, 0, image.getWidth(), image.getHeight());   
        d.setColor(new Color(random.nextInt(100) + 100, random.nextInt(100) + 100, random.nextInt(100) + 100));   
        for (int i = 0; i < 10; i++) {   
            d.drawLine(random.nextInt(image.getWidth()), random.nextInt(image.getHeight()), random.nextInt(image.getWidth()),   
                    random.nextInt(image.getHeight()));   
        }   
        d.setColor(Color.BLACK);   
        d.setFont(font);   
        String checkCode = "";   
        char tmp;   
        int x = -distance;   
        for (int i = 0; i < show; i++) {   
            tmp = str.charAt(random.nextInt(str.length() - 1));   
            checkCode = checkCode + tmp;   
            x = x + distance;   
            d.setColor(new Color(random.nextInt(100) + 50, random.nextInt(100) + 50, random.nextInt(100) + 50));   
            d.drawString(tmp + "", x, random.nextInt(image.getHeight() - (font.getSize())) + (font.getSize()));   
        }   
        d.dispose();   
        try {   
            ImageIO.write(image, "jpg", output);   
        } catch (IOException e) {   
            log.warn("生成验证码错误.", e);   
        }   
        return checkCode;   
    }   
  
    public String execute() throws Exception {   
        ByteArrayOutputStream output = new ByteArrayOutputStream();   
        String checkCode = getCheckCodeImage("ABCDEFGHJKLMNPQRSTUVWXYZ123456789", 4, output);   
        this.session.put(Constants.CHECK_CODE_KEY, checkCode);   
        //这里将output stream转化为 inputstream   
        this.imageStream = new ByteArrayInputStream(output.toByteArray());   
        output.close();   
        return SUCCESS;   
    }   
  
    public InputStream getImageStream() {   
        return imageStream;   
    }   
  
    public void setSession(Map session) {   
        this.session = session;   
    }  

public class CheckCodeAction extends ActionSupport implements SessionAware {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    private InputStream imageStream;
    private Map session;

    public String getCheckCodeImage(String str, int show, ByteArrayOutputStream output) {
        Random random = new Random();
        BufferedImage image = new BufferedImage(80, 30, BufferedImage.TYPE_3BYTE_BGR);
        Font font = new Font("Arial", Font.PLAIN, 24);
        int distance = 18;
        Graphics d = image.getGraphics();
        d.setColor(Color.WHITE);
        d.fillRect(0, 0, image.getWidth(), image.getHeight());
        d.setColor(new Color(random.nextInt(100) + 100, random.nextInt(100) + 100, random.nextInt(100) + 100));
        for (int i = 0; i < 10; i++) {
            d.drawLine(random.nextInt(image.getWidth()), random.nextInt(image.getHeight()), random.nextInt(image.getWidth()),
                    random.nextInt(image.getHeight()));
        }
        d.setColor(Color.BLACK);
        d.setFont(font);
        String checkCode = "";
        char tmp;
        int x = -distance;
        for (int i = 0; i < show; i++) {
            tmp = str.charAt(random.nextInt(str.length() - 1));
            checkCode = checkCode + tmp;
            x = x + distance;
            d.setColor(new Color(random.nextInt(100) + 50, random.nextInt(100) + 50, random.nextInt(100) + 50));
            d.drawString(tmp + "", x, random.nextInt(image.getHeight() - (font.getSize())) + (font.getSize()));
        }
        d.dispose();
        try {
            ImageIO.write(image, "jpg", output);
        } catch (IOException e) {
            log.warn("生成验证码错误.", e);
        }
        return checkCode;
    }

    public String execute() throws Exception {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        String checkCode = getCheckCodeImage("ABCDEFGHJKLMNPQRSTUVWXYZ123456789", 4, output);
        this.session.put(Constants.CHECK_CODE_KEY, checkCode);
        //这里将output stream转化为 inputstream
        this.imageStream = new ByteArrayInputStream(output.toByteArray());
        output.close();
        return SUCCESS;
    }

    public InputStream getImageStream() {
        return imageStream;
    }

    public void setSession(Map session) {
        this.session = session;
    }
struts配置文件Java代码
<action name="checkCode" class="CheckCodeAction">   
      <result name="success" type="stream">   
            <param name="contentType">image/jpeg</param>   
            <!-- 指定提供InputStream的filed name -->   
            <param name="inputName">imageStream</param>   
            <param name="bufferSize">1024</param>   
        </result>   
        <interceptor-ref name="defaultStack"/>   
</action>  

<action name="checkCode" class="CheckCodeAction">
      <result name="success" type="stream">
            <param name="contentType">image/jpeg</param>
            <!-- 指定提供InputStream的filed name -->
            <param name="inputName">imageStream</param>
            <param name="bufferSize">1024</param>
        </result>
        <interceptor-ref name="defaultStack"/>
</action>
可以看出使用Stream result type非常简单。在该例子中使用了一个小技巧将OutputStream转化为InputStremJava代码
ByteArrayOutputStream output = new ByteArrayOutputStream();   
//省略填充output的代码   
...      
InputStrem   
in = new ByteArrayInputStream(output.toByteArray());


只看该作者    顶部
离线 juanpeng
中级会员



精华贴数 0
个人空间 0
技术积分 4608 (294)
社区积分 0 (1441085)
注册日期 2007-6-21
论坛徽章:27
开发板块每日发贴之星开发板块每日发贴之星开发板块每日发贴之星生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠
生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠

发表于 2008-7-3 19:18 
多谢楼主分享,是个好方法,方便了好多


只看该作者    顶部
离线 smartpig
老会员



精华贴数 1
个人空间 160
技术积分 6249 (212)
社区积分 0 (1065303)
注册日期 2006-8-9
论坛徽章:33
会员2007贡献徽章开发板块每日发贴之星开发板块每日发贴之星开发板块每日发贴之星生肖徽章2007版:鼠生肖徽章2007版:鼠
生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠

发表于 2008-7-3 19:18 
谢谢啦,正好用的着。


只看该作者    顶部
离线 hanfeishu
中级会员



精华贴数 0
个人空间 0
技术积分 4108 (347)
社区积分 0 (1285835)
注册日期 2007-2-28
论坛徽章:19
开发板块每日发贴之星生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠
生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠

发表于 2008-7-3 19:19 
大哥,写个完整点的例子嘛,帮人帮到底嘛。请问用于显示图片的JSP代码(最好用struts2标签)应该怎样写?


只看该作者    顶部
离线 cyntha80
中级会员



精华贴数 1
个人空间 0
技术积分 4577 (296)
社区积分 2 (30298)
注册日期 2007-2-8
论坛徽章:20
开发板块每日发贴之星生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠
生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠

发表于 2008-7-3 19:19 
我现在只能做到把图片从数据库中读出,然后写到磁盘上存成一个文件,比如“a.jpg",然后在JSP中用<img src="a.jpg"></img>显示出来,有没有直接用输出流输出到JSP网页中的?


只看该作者    顶部
离线 xieye
宝塔镇河妖


精华贴数 0
个人空间 0
技术积分 1853 (879)
社区积分 20767 (67)
注册日期 2004-11-29
论坛徽章:270
生肖徽章2007版:羊设计板块每日发贴之星设计板块每日发贴之星设计板块每日发贴之星行业板块每日发贴之星行业板块每日发贴之星
行业板块每日发贴之星开发板块每日发贴之星开发板块每日发贴之星开发板块每日发贴之星网络板块每日发贴之星网络板块每日发贴之星

发表于 2008-7-3 22:37 
好东西~~


__________________
菠菜就是看RP
只看该作者    顶部
 
    

相关内容


CopyRight 1999-2006 itpub.net All Right Reserved.
北京皓辰广域网络信息技术有限公司. 版权所有
E-mail:Webmaster@itpub.net
京ICP证:010037号 联系我们 法律顾问