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;
}
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>
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 至尊貝貝 閱讀(1241) 評論(0) 編輯 收藏 所屬分類: 代碼:JavaScript