用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
相关文章推荐
- angularjs中$http、$location、$watch及双向数据绑定学习实现简单登陆验证
- 猫猫学IOS(七)UI之UITextField代理事件_类似QQ登陆窗口的简单实现
- (素材源码)猫猫学IOS(七)UI之UITextField代理事件_类似QQ登陆窗口的简单实现
- 搭建SpringMVC框架,实现简单登陆功能
- 使用三层架构实现简单的MVC登陆操作
- 利用readwritelock简单模拟实现多线程下cache的系统
- 用批处理如何实现telnet登陆并进行一些简单命令
- 简单的登陆注册的实现+验证码和表单验证
- php实现用户登陆简单实例
- 第26篇:AngularJS+ui-router实现一个超简单的登陆和跳转的二级路由demo
- 记住密码实现登陆功能,简单,易懂。
- 一个简单登陆示例的MVC和MVP实现
- 简单的Struts2的Interceptors(拦截器)机制介绍以及利用拦截器实现用户登陆时执行耗时操作并自动跳转的实例
- Thread学习(七) 用读写锁ReadWriteMethod实现个简单的例子,cache缓存的原理
- SpringBoot + thymeleaf 实现简单的登陆验证
- Android笔记-4-实现登陆页面并跳转和简单的注册页面
- 简单三层实现登陆
- JavaWeb基础之Servlet简单实现用户登陆
- 简单LRU算法实现的Cache(C++)
- 只用UILabel实现简单登陆页面