@RequestMapping(value = "/uploadp_w_picpath.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
public List
方法二、
控制器:
List 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 ShowImg(final String id){ String sql = "select name,img from img where id = "+id+""; final List list=new ArrayList(); 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 ResultSetExtractorAbstract 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 p_w_picpathdb WHERE p_w_picpath_name=?", new Object[] {name}, new AbstractLobStreamingResultSetExtractor() { public void streamData(ResultSet rs) throws SQLException, IOException { FileCopyUtils.copy(lobHandler.getBlobAsBinaryStream(rs, 1), contentStream); } } );