• 图片以BLOB存储在后台数据库中,Android客户端要进行读取显示


    解决方法:

    1:在后台以InputStream的方式将图片从数据库中读出:

    public static InputStream getPicInputStream(){
            String id = "f304733361e243779b2340afe20e62bf";
            
             Connection conn = JdbcUtil.getConnection();
             
             PreparedStatement ps = null;
             
             ResultSet rs = null;
             
             InputStream is = null;
             
             String sql = "select zp from pic_test where id= ?";
             
            try {
                
                ps = conn.prepareStatement(sql);
                
                ps.setString(1, id);
                
                rs = ps.executeQuery();
                
                if(rs.next()){
                    is = rs.getBlob("zp").getBinaryStream();
                }
                
            } catch (SQLException ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            }finally{
                try {
                    if(null!=ps){
                        ps.close();
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    if(null!=rs){
                        rs.close();
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                }
                JdbcUtil.release(conn);
            }
             
            
            return is;
        }

    2:在Servlet中直接生成图片:

    response.setContentType("image/jpeg");
            
            InputStream is = Test.getPicInputStream();
            
            if(null!=is){
               
                OutputStream os = response.getOutputStream();
                
                int len;
                
                byte buf[] = new byte[1024];
                
                while((len=is.read(buf))!=-1){
                    os.write(buf, 0, len);
                }
                
                is.close();
                os.close();
            }

    3:Android客户端的处理

    public class MainActivity extends Activity {
    
        ImageView imgView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            imgView = (ImageView)findViewById(R.id.imgView);
            
            new ImgViewAsyncTask().execute("");
        }
    
        class ImgViewAsyncTask extends AsyncTask<String,String,String>{
            Bitmap bitmap = null;
    
            @Override
            protected void onPreExecute() {
    
            }
    
            @Override
            protected String doInBackground(String... strings) {
                InputStream is = null;
                String result = "";
    
                try {
                    URL picURL = new URL("http://192.168.0.165:8084/wisdompm/PicServlet");
                    HttpURLConnection conn = (HttpURLConnection)picURL.openConnection();
                    conn.setConnectTimeout(10000);
                    conn.connect();
    
                    is = conn.getInputStream();
                    if(null!=is){
                        bitmap = BitmapFactory.decodeStream(is);
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    try {
                        if(null!=is){
                            is.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    
                return result;
            }
    
            @Override
            protected void onPostExecute(String s) {
                if(null!=bitmap && null!=imgView){
                    imgView.setImageBitmap(bitmap);
                }
            }
        }
    }

    测试结果:

  • 相关阅读:
    DOM获取分辨率
    DataSnap被动断开客户端及主动断开客户端
    DOM的常用操作
    Apache Shiro官方的教程和文档
    maven jar 导入本地仓库
    Media Queries 媒体查询详解
    flash遮挡DIV元素的问题总结
    CSS样式字体 自动换行(强制换行)与强制不换行
    【推荐】万能清除浮动样式
    响应式网页设计
  • 原文地址:https://www.cnblogs.com/yshyee/p/4179464.html
Copyright © 2020-2023  润新知