public void setImgByte(final byte[] bytes, final int id) { this.getJdbcTemplate().execute( "update tbcaseadjust set content=? where id=?", new PreparedStatementCallback() { public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { ps.setBytes(1, bytes); ps.setInt(2, id); ps.execute(); return null; } }); }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></title> </head> <body><s:form enctype="multipart/form-data" theme="simple" method="post"><s:file name="upload"></s:file><s:submit value="submit"></s:submit></s:form> </body></html>
public class UploadAction extends ActionSupport { private File upload; private String uploadContentType; private String uploadFileName; @Override public String execute() throws Exception { if(uploadContentType!=null) { setImgByte(FileUtils.readFileToByteArray(upload),1); } return SUCCESS; } public File getUpload() { return upload; } public void setUpload(File upload) { this.upload = upload; } public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } public String getUploadFileName() { return uploadFileName; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; }
public Object getImgByte(final int id) { return this.getJdbcTemplate().execute( "select content from tbcaseadjust where id=?", new PreparedStatementCallback() { public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { ps.setInt(1, id); ResultSet rs = ps.executeQuery(); if (rs.next()) { InputStream inputStream = rs .getBinaryStream("content"); try { return IOUtils.toByteArray(inputStream); } catch (IOException e) { } } return null; } }); }
public class UpLoadImgAction extends ActionSupport{ private InputStream inputStream; public InputStream getInputStream() { return inputStream; } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } @Override public String execute() throws Exception { Object[] args =new Object[]{1}; byte[] bytes =(byte[])getImgByte(args); inputStream = new ByteArrayInputStream(bytes); return "image"; } }
<result name="image" type="stream"> <param name="contentType"> application/octet-stream </param> <param name="inputName">inputStream</param> <param name="bufferSize">1024</param> </result>