图片以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);

            }

        }

    }

}

测试结果:

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

 

你可能感兴趣的:(android)