java缓存读取图片

u010181717 2013-10-31 09:27:57
老师布置了任务,需要把数据库中的图片一缓存的形式读出,不要说什么数据库中路劲,图片整体较大,在给别人使用时不现实。
关键代码: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");
}
}
求指教,我会出的是空白图。
...全文
409 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
a17317070 2015-10-30
  • 打赏
  • 举报
回复
你用流来读图片才坑爹,浏览器自带的304缓存都被你吃了,你这样,一个页面读大量大图片,能快速读出来才有鬼了
Steven_kiss 2015-03-25
  • 打赏
  • 举报
回复
无意冒犯。忽略上一条消息~
Steven_kiss 2015-03-25
  • 打赏
  • 举报
回复
事的减肥了敬爱可适当
u010181717 2013-11-01
  • 打赏
  • 举报
回复
额,想出来了,很简单
public class pictest
{
	public pictest(){
		JFrame f=new JFrame();
	    Container p=f.getContentPane();
		ppic pic=null;
		InputStream in=null;
		try{
			int i=3;
			String url=".................";
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=DriverManager.getConnection(*******);
			//连接到数据库
			if(!con.isClosed())
				System.out.println("数据库连接成功");
			String sql="select * from campusimage";
			Statement st=con.createStatement();
			ResultSet rs=st.executeQuery(sql);

			for(int j=0;j<i;j++)
				rs.next();
			if(rs.next())
				in=rs.getBinaryStream(1);
            //pic=new ppic(in);
			new dragimage();
			rs.close();
			st.close();
			con.close();
		}catch(Exception e){e.printStackTrace();}
		pic=new ppic(in);
		p.setLayout(null);
		p.add(pic);
		f.setBounds(500,400,800,900);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}

	class ppic extends JPanel
	{   BufferedImage bi;
		ppic(InputStream in){
			try{
			  bi=ImageIO.read(in);
			  setSize(700,600);
			}catch(Exception e){e.printStackTrace();}
		}
		public void paint(Graphics g){
			g.drawImage(bi,0,0,700,600,null);
		}
	}
	public static void main(String[] args){
		new pictest();
	}

	}
安特矮油 2013-10-31
  • 打赏
  • 举报
回复
那就直接把文件读取出来,然后以流的方式缓存的内存中。用的时候直接去文件流
失落夏天 2013-10-31
  • 打赏
  • 举报
回复
如果读出来是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 < length; i++) {  
	        int pos = i * 2;  
	        d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));  
	    }  
	    return d;  
	} 
	public static String bytesToHexString(byte[] src){  
	    StringBuilder stringBuilder = new StringBuilder("");  
	    if (src == null || src.length <= 0) {  
	        return null;  
	    }  
	    for (int i = 0; i < src.length; i++) {  
	        int v = src[i] & 0xFF;  
	        String hv = Integer.toHexString(v);  
	        if (hv.length() < 2) {  
	            stringBuilder.append(0);  
	        }  
	        stringBuilder.append(hv);  
	    }  
	    return stringBuilder.toString();  
	}  
	
	private static byte charToByte(char c) {  
	    return (byte) "0123456789ABCDEF".indexOf(c);  
	}  
	
	 public static final InputStream byte2Input(byte[] buf) {  
	        return new ByteArrayInputStream(buf);  
	    }  
	  
	    public static final byte[] input2byte(InputStream inStream)  
	            throws IOException {  
	        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();  
	        byte[] buff = new byte[100];  
	        int rc = 0;  
	        while ((rc = inStream.read(buff, 0, 100)) > 0) {  
	            swapStream.write(buff, 0, rc);  
	        }  
	        byte[] in2b = swapStream.toByteArray();  
	        return in2b;  
	    }  
}

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧