起步软件技术论坛
搜索
 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1463|回复: 2

[处理中3] UI页面如何获取数据库中的图片?

[复制链接]

135

主题

387

帖子

775

积分

高级会员

Rank: 4

积分
775
QQ
发表于 2018-3-21 09:10:39 | 显示全部楼层 |阅读模式
做的APP,想通过点击按钮跳转到图片页面,图片是以blob格式存在数据库中的,我想知道图片该如何获取?是通过自定义action的方式还是别的?

91

主题

13万

帖子

3万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
36046
发表于 2018-3-21 10:36:22 | 显示全部楼层
远程的联系方法QQ1392416607,添加好友时,需在备注里注明其论坛名字及ID,公司等信息
发远程时同时也发一下帖子地址,方便了解要解决的问题  WeX5教程  WeX5下载



如按照该方法解决,请及时跟帖,便于版主结贴
回复 支持 反对

使用道具 举报

135

主题

387

帖子

775

积分

高级会员

Rank: 4

积分
775
QQ
 楼主| 发表于 2018-4-13 14:26:25 | 显示全部楼层
jishuang 发表于 2018-3-21 10:36
http://bbs.wex5.com/forum.php?mod=redirect&goto=findpost&ptid=112304&pid=165345516

我按照baas自定义action使用sql语句查询出二进制数据!然后再以文件流的形式写出!您看看我这段代码该如何修改才能成功获取数据库图片,自己写的代码也有点懵
// 获取图纸
        public static JSONObject getImgInfo(JSONObject params, ActionContext context) throws JsonGenerationException, JsonMappingException, IOException {
                String id = params.getString("Strid"); // 获取图纸ID

                HttpServletRequest request = (HttpServletRequest) context.get(ActionContext.REQUEST);
                HttpServletResponse response = (HttpServletResponse) context.get(ActionContext.RESPONSE);

                PreparedStatement pstmt = null;
                ResultSet rs = null;
                Connection conn = null;
                String sql = "";
                JSONObject result = new JSONObject();
                try {
                        conn = context.getConnection("txxt");
                        if (id != null) {
                                sql = "SELECT * FROM ENTITY_000 WHERE ID = '" + id + "' AND USED = 1";
                                System.out.println(sql);
                                pstmt = conn.prepareStatement(sql);
                                rs = pstmt.executeQuery();
                                if (rs != null) {
                                        if (rs.next()) {
                                                String ENTITY = rs.getString("ENTITY");
                                                result.put("ENTITY", ENTITY);
                                                System.out.println(ENTITY);
                                                Blob blob = rs.getBlob("ENTITY_000");
                                                InputStream ins = blob.getBinaryStream();

                                                 // 输出到文件
                                                 OutputStream fout = new FileOutputStream(ENTITY);
                                                 // 下面将BLOB数据写入文件
                                                 byte[] b = new byte[1024];
                                                 int len = 0;
                                                 while ((len = ins.read(b)) != -1) {
                                                 fout.write(b, 0, len);
                                                 }

                                                response.setContentType("image/WMF");
                                                response.setHeader("Cache-control", "no-cache");
//                                                ServletOutputStream fout1 = response.getOutputStream();

                                                int num;
                                                byte buf[] = new byte[1024];
                                                while ((num = ins.read(buf)) != -1) {
                                                        fout.write(buf, 0, num);
                                                }

                                                // 依次关闭
                                                fout.close();
                                                ins.close();
                                        }
                                }
                        }
                        return null;
                } catch (Exception e) {
                        e.printStackTrace();
                } finally {
                        try {
                                conn.close();
                        } catch (Exception e2) {
                                e2.printStackTrace();
                        }
                }
                return result;

        }
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|X3技术论坛|Justep Inc.    

GMT+8, 2024-9-27 17:30 , Processed in 0.063501 second(s), 23 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表