Spring中使用JdbcTemplate插入和读取图片

来源:互联网 发布:gta5视角软件 编辑:程序博客网 时间:2024/05/16 07:44

1.往数据库插入图片

数据库操作:

1.往数据库插入图片

数据库操作:

    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;                    }                });    }

前台代码:

我使用的是struts2的上传,使用struts2的上传需要commons-fileupload.1.x.jar,commons-io-1.x.jar包

这两个包下载地址:http://commons.apache.org/

UploadPage.jsp

<%@ 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>

对应的Action类UploadPageAction.java

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;    }

2.从数据库取图片

数据库操作:

    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";    }    }

返回的image类型需要在struts配置文件里配置一下:

            <result name="image" type="stream">                <param name="contentType">                    application/octet-stream                </param>                <param name="inputName">inputStream</param>                <param name="bufferSize">1024</param>            </result>
0 0