Struts2+Spring+JDBC 实现图片上传与显示

数据库为oracle,图片存储类型为Blob。

要想往数据库里插入,需要两个步骤:

1、插入空值(empty_blob());

2、更新Blob字段。


 DAO如下:

public class BookDaoJdbcImpl extends JdbcDaoSupport implements BookDao {    
    //1、插入空值    
    public void addBook(Book book) {
        String sql = "INSERT INTO bookinfo(id, coverimg) " +
	    "VALUES(" + book.getId() + ",empty_blob())";		
	this.getJdbcTemplate().execute(sql);    
    }

    //2、更新图片
    public void upDateCoverImg(final int id, final File img) throws Exception {
        final FileInputStream fis = new FileInputStream(img);
        this.getJdbcTemplate().update(new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement(Connection connection){
                PreparedStatement ps = null;
                    try {
                    ps = connection.prepareStatement("UPDATE bookinfo SET coverimg=? WHERE id=?");
                    ps.setBinaryStream(1, fis, fis.available());
                    ps.setInt(2, id);
                    ps.executeUpdate();
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
                return ps;
            }
      });
   }

    //获取图片输入流,用于读取图片
    public InputStream getCoverImg(int id) {
        InputStream inputStream;
      
        String sql = "SELECT coverimg FROM bookinfo WHERE id=" + id;
  
        inputStream = (InputStream)this.getJdbcTemplate().execute(sql, new PreparedStatementCallback() {
            public Object doInPreparedStatement(PreparedStatement pstmt) throws SQLException, DataAccessException {
                ResultSet rs = pstmt.executeQuery();
                BufferedInputStream ins = null;
                if (rs.next()) {
                    Blob blob = rs.getBlob(1);
                    try {
                        ins = new BufferedInputStream(blob.getBinaryStream());
                    } catch (Exception e) {
                    }
                }
                return ins;
            }
        });
        return inputStream;
    }

}

AddBookAction如下:

public class AddBookAction {
	//inject
	private BookDao bookDao;
	
	//input
	private Book book;
	private File uploadFile;  //图片上传控件
	
	public String execute() throws Exception {
		//添加基本图书信息
		bookDao.addBook(book);
		
		//更新图片BLOB
		bookDao.upDateCoverImg(book.getId(), uploadFile);
		
		return "success";
	}

	//getter and setter
	
}

AddBookAction配置:

<action name="addBook" class="AddBookAction">
	<interceptor-ref name="fileUpload"></interceptor-ref>
	<interceptor-ref name="basicStack"></interceptor-ref>
	<result name="success">/book/bookInfo.jsp?id=${id}</result>
</action>

 

至此完成图片上传核心编码。


图片读取与显示

DAO见上面部分。

GetImageAction如下:

public class GetImageAction extends BaseAction {
	//inject
	private BookDao bookDao;
	
	//input
	private int id;
	
	//output
	private InputStream coverImg;

	public String execute() {
		coverImg = bookDao.getCoverImg(id);
		return "success";
	}
	
	//getter and setter
	
}

GetImageAction配置如下:

<action name="getImg" class="GetImageAction">
	<result name="success" type="stream">
		<param name="inputName">coverImg</param>
	</result>
</action>

JSP页面使用:

<img src="${pageContext.request.contextPath}/getImg?id=<s:property value='book.id'/>" width="100" height="120"/>

 


                         new FileInputStream(img)                                               setBinaryStream()方法
上传:File------------------------------------------>FileInputStream 流------------------------------------------>blob类型

                                 getBinaryStream()                                                             read()                         response
显示:blob类型----------------------------------->BufferedInputStream流 --------------->byte[]---------------------------->OutputStream().write()
 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值