图片以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客户端要进行读取显示的更多相关文章
- SharePoint 2013 使用 RBS 功能将二进制大型对象 BLOB 存储在内容数据库外部。
为每个内容数据库设置 BLOB 存储 启用并配置 FILESTREAM 之后,请按照以下过程在文件系统中设置 BLOB 存储.必须为要对其使用 RBS 的每个内容数据库设置 BLOB 存储. 设置 ...
- Python爬取招聘信息,并且存储到MySQL数据库中
前面一篇文章主要讲述,如何通过Python爬取招聘信息,且爬取的日期为前一天的,同时将爬取的内容保存到数据库中:这篇文章主要讲述如何将python文件压缩成exe可执行文件,供后面的操作. 这系列文章 ...
- 将 flask 中的 session 存储到 SQLite 数据库中
将 flask 中的 session 存储到 SQLite 数据库中 使用 flask 构建服务器后端时,常需要在浏览器端存储 cookie 用于识别不同用户,根据不同的 cookie 判断出当前请求 ...
- 利用POI工具读取word文档并将数据存储到sqlserver数据库中
今天实现了利用POI工具读取word文档,并将数据存储到sql数据库中,代码如下: package word; import java.io.File; import java.io.FileInpu ...
- EasyUI动态显示后台数据库中的数据
最近在完成一个项目,采用SSM框架搭建完成,前端使用EasyUI搭建页面: 其中涉及到一个查询显示功能:查询数据库中的数据,动态显示在页面之中,刚开始这部分十分有疑问,所以虚心向同学学习,现总结至博客 ...
- 通过JSP网页连接MySQL数据库,从MySQL数据库中读出一张表并显示在JSP网页中
1.安装所需软件 ①安装java和tomcat,建立JSP网页最基础的软件②安装MySQL数据库(下载地址:https://www.mysql.com/)③安装Navicat Premium来查看数据 ...
- scrapy 自定义图片路径保存,并存到数据库中
scrapy中有个自带的pipeline工具,ImagesPipeline,可以专门用来储存图片到本地. 但默认储存地址无法配置,所以我们需要写一个自己的pipeline用于储存图片. 先分析一下我们 ...
- .net的session详解 存储模式 存到数据库中 使用范围与大小限制 生命周期
Session又称为会话状态,是Web系统中最常用的状态,用于维护和当前浏览器实例相关的一些信息.举个例子来说,我们可以把已登录用户的用户名放在Session中,这样就能通过判断Session中的某个 ...
- Java爬网页数据,并存储到本地数据库中
由于开发一个人工智能项目,需要强大的后台数据库加持,所以,没有办法,又是需要医疗数据,只能自己爬某医疗网站数据,进行分析,但是由于不同网站的结构不一样,所以这个程序只能爬该网站的,第一次爬网页数据,自 ...
随机推荐
- write a macro to judge big endian or little endian
Big endian means the most significant byte stores first in memory. int a=0x01020304, if the cpu is b ...
- UNIX网络编程--IPV4 IPV6 ICMPV4 ICMPV6
一.IPV4首部 IP层提东无连接不可靠的数据报递送服务.它会尽力把IP数据报递送到指定的目的地,然而并不保证他们一定到达,也不保证他们的到达顺序与发送顺序一致,还不保证每个IP数据报只到达一次.任何 ...
- startkde出现$DISPLAY is not set or cannot connect to the X server
#startkde $DISPLAY is not set or cannot connect to the X server 解决: xinit /usr/bin/startkde --displa ...
- Linux企业级项目实践之网络爬虫(1)——项目概述及准备工作
我们在学习了Linux系统编程之后,需要一些实战项目来提高自己的水平,本系列我们通过编写一个爬虫程序,将我们学习的知识进行综合应用,同时在实现项目的过程中逐渐养成一些有用的思维方式,并具有初步的软件开 ...
- bzoj1003[ZJOI2006]物流运输trans
1003: [ZJOI2006]物流运输trans Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常 ...
- POJ1159 Palindrome(数位DP)
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 58277 Accepted: 20221 Desc ...
- 升级 pip版本
安装第三方库:pip install Pillow 出现 You are using pip version 7.1.2, however version 9.0.1 is available. Yo ...
- (转)iOS Wow体验 - 第三章 - 用户体验的差异化策略
本文是<iOS Wow Factor:Apps and UX Design Techniques for iPhone and iPad>第三章译文精选,其余章节将陆续放出.上一篇:Wow ...
- 分享一个option样式传递给select当前选中样式
今天遇到一个很是纠结的问题,需求又改了!原生的select给option加样式,结果发现select选中仍是默认样式,如下图:
- windows下体验Redis
Redis 是一个高性能的key-value数据库, 使用内存作为主存储,数据访问速度非常快,当然它也提供了两种机制支持数据持久化存储.比较遗憾的是,Redis项目不直接支持Windows,Windo ...