java 图片缓存工具,java缓存读取图片解决方案

java缓存读取图片

老师布置了任务,需要把数据库中的图片一缓存的形式读出,不要说什么数据库中路劲,图片整体较大,在给别人使用时不现实。

关键代码:for(int i=0;i<1;i++){

downloadDB(bi);

pm[i]=new paintimage(bi);

}

public void downloadDB(BufferedImage bi){

try{

/* String s="c:\\downloadDB\\"+i+".png";

File file=new File(s);

FileOutputStream fos=new FileOutputStream(file);*/

InputStream in=null;

String url="jdbc:mysql://192.168.**.***:3306/de";

Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection(url,"root","***");

String sql="select * from campusimage";

Statement st=con.createStatement();

ResultSet rs=st.executeQuery(sql);

for(int j=0;j<1;j++)

rs.next();

int m;

if(rs.next())

in=rs.getBinaryStream(1);

/*while((m=in.read())!=-1)

fos.write(m);*/

BufferedInputStream is=new BufferedInputStream(in);

bi=ImageIO.read(is);

System.out.println("00000000000");

rs.close();

st.close();

con.close();

}catch(Exception e){e.printStackTrace();}

}

class paintimage extends JPanel{

//ImageIcon icon=null;

Image ima=null;

paintimage(BufferedImage bi){

/*String s1="c:\\downloadDB\\"+i+".png";

icon=new ImageIcon(s1);

ima=icon.getImage();*/

ima=(Image)bi;

setBounds(0,0,700,600);

System.out.println("1111111111");

}

public void paint(Graphics g){

g.drawImage(ima,0,0,700,600,null);

System.out.println("2222222222");

}

}

求指教,我会出的是空白图。

java

缓存

图片

数据库

mysql

分享到:

------解决方案--------------------

如果读出来是16进制字符串的话,

16进制字符串-》byte数组-》流-》文件

public class ImgHelper {

public static void getImg(String hexString) {

byte[] b = ByteHelper.hexStringToBytes(hexString);

InputStream is = ByteHelper.byte2Input(b);

createFile(is, new File("D://pp.jpg"));

}

//生成图像文件

public static void createFile(InputStream is, File targetFile) {

BufferedInputStream inBuff = null;

BufferedOutputStream outBuff = null;

try {

// 新建文件输入流并对它进行缓冲

inBuff = new BufferedInputStream(is);

// 新建文件输出流并对它进行缓冲

try {

outBuff = new BufferedOutputStream(new FileOutputStream(

targetFile));

// 缓冲数组

byte[] b = new byte[1024 * 5];

int len;

while ((len = inBuff.read(b)) != -1) {

outBuff.write(b, 0, len);

}

// 刷新此缓冲的输出流

outBuff.flush();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} finally {

// 关闭流

try {

if (inBuff != null)

inBuff.close();

if (outBuff != null)

outBuff.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

public class ByteHelper {

public static byte[] hexStringToBytes(String hexString) {

if (hexString == null

------解决方案--------------------

hexString.equals("")) {

return null;

}

hexString = hexString.toUpperCase();

int length = hexString.length() / 2;

char[] hexChars = hexString.toCharArray();

byte[] d = new byte[length];

for (int i = 0; i 

int pos = i * 2;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值