现在的位置: 首页 > 综合 > 正文

get byte[] from DB & send it to client as download file

2013年10月11日 ⁄ 综合 ⁄ 共 708字 ⁄ 字号 评论关闭

String sql = "select id,content,mail_entity_id from mail_raw where mail_entity_id=? limit 1 ";
byte[] rawMail = (byte[]) jt.queryForObject(sql,
        new Object[]{downLoadRawMaiID}, new RowMapper() {
            @Override
            public Object mapRow(ResultSet rs, int rowid)
                    throws SQLException {
                return rs.getBytes("content");
            }
        });

if (rawMail == null) {
    response.setContentType("text/plain");
    response.getWriter().println(
            "no such mail. raw mai id is" + downLoadRawMaiID);
} else {
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition",
            "attachment; filename=" + downLoadRawMaiID + ".eml");
    response.getOutputStream().write(rawMail);

}

抱歉!评论已关闭.