您的位置:首页 > 其它

用Cache简单的实现单点登陆

2007-01-30 16:09 211 查看
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'showImg.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<table>
<tr>
<td>111</td>
<td><img alt="" src=\'#\'" /showimg.do?id=1" width="200px" height="180px" ></td>
</tr>
</table>

</body>
</html>
@RequestMapping(value = "/uploadimage.do")
public void uploadPhoto(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request,HttpServletResponse response) throws Exception {
try{
byte[] data = file.getBytes();
int id = uploadDao.FileUpload(data, file.getOriginalFilename());
}
catch(Exception e){
e.printStackTrace();
}

}

@RequestMapping(value = "/showimg.do")
public void showimg(HttpServletRequest request,HttpServletResponse response) throws Exception {
int id = Integer.parseInt(request.getParameter("id"));
System.out.println(id);
List<Object>  list =  uploadDao.ShowImg(id);
Blob blob=(Blob) list.get(0);
int length = (int) blob.length();
byte[] bImage = new byte[length];
InputStream is = new BufferedInputStream(blob.getBinaryStream());
is.read(bImage, 0, length);
OutputStream out = response.getOutputStream();
out.write(bImage);
out.flush();
out.close();
is.close();
}
public List<Object> ShowImg(int id) {
String sql = "select name,img from img where id = "+id+"";
List<Object> list=new ArrayList<Object>();
Connection conn=null;
Statement state=null;
ResultSet rs = null;
try {
conn = jdbcTemplate.getDataSource().getConnection();
state=conn.createStatement();
rs=state.executeQuery(sql.toString());
if (rs.next()) {
Blob blob = rs.getBlob("img");
list.add(0, blob);

}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
方法二、
控制器:
List<Object>  list =  uploadDao.ShowImg(mainid);
if(!list.isEmpty() && list != null){
Blob blob=(Blob) list.get(0);
int length = (int) blob.length();
byte[] bImage = new byte[length];
InputStream is = new BufferedInputStream(blob.getBinaryStream());
is.read(bImage, 0, length);
OutputStream out = response.getOutputStream();
out.write(bImage);
out.flush();
out.close();
is.close();
接口实现:
@SuppressWarnings("unchecked")
public List<Object> ShowImg(final String id){
String sql = "select name,img from img where id = "+id+"";
final List<Object> list=new ArrayList<Object>();
jdbcTemplate.query(sql, new org.springframework.jdbc.core.support.AbstractLobStreamingResultSetExtractor() {

@Override
protected void streamData(ResultSet rs) throws SQLException, IOException,
DataAccessException {
Blob blob = rs.getBlob("img ");
list.add(0, blob);

}
});
return list;
}


api:
public abstract class AbstractLobStreamingResultSetExtractorextends Objectimplements ResultSetExtractor
Abstract ResultSetExtractor implementation that assumes streaming of LOB data. Typically used as inner class, with access to surrounding method arguments.
Delegates to the streamData template method for streaming LOB content to some OutputStream, typically using a LobHandler. Converts an IOException thrown during streaming to a LobRetrievalFailureException.
A usage example with JdbcTemplate:
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);  // reusable object
final LobHandler lobHandler = new DefaultLobHandler();  // reusable object

jdbcTemplate.query(
"SELECT content FROM imagedb WHERE image_name=?", new Object[] {name},
new AbstractLobStreamingResultSetExtractor() {
public void streamData(ResultSet rs) throws SQLException, IOException {
FileCopyUtils.copy(lobHandler.getBlobAsBinaryStream(rs, 1), contentStream);
}
}
);
本文出自 “骑猴上树” 博客,请务必保留此出处http://qihoushangshu.blog.51cto.com/7872138/1543647
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: