日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/9 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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;

總結

以上是生活随笔為你收集整理的java 图片缓存工具,java缓存读取图片解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。