随笔 - 25, 文章 - 1, 评论 - 0, 引用 - 0
数据加载中……

Ajax从服务端读取图片数据

服务端代码:
@Action(value = "/imageload")
    
public String imageLoad ()throws Exception {
        
        File f 
= new File ("d:/down.jpg");
        FileInputStream in 
= new FileInputStream (f);
        
byte[] b = new byte[in.available()];
        in.read(b);
        HttpServletResponse response 
= ServletActionContext.getResponse();
//        response.setCharacterEncoding("UTF-8");
        response.setContentType("image");
        OutputStream pwt 
= response.getOutputStream();
        pwt.write(b);
        pwt.flush();
        pwt.close();
        
return null;
    }
客户端代码:
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding
="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
</head>
<body>
<img id="img1" src=""></img>
<script type="text/javascript">
    
function load() {
        $('#img1').attr('src', 'subject
/imageload.action');
    }
</script>

<button onclick=load();>点击获取</button>
</body>
</html>


posted on 2010-05-19 17:20 至尊贝贝 阅读(1231) 评论(0)  编辑  收藏 所属分类: 代码:JavaScript


只有注册用户登录后才能发表评论。


网站导航: