阅读背景:

图片和字符串相互转换

来源:互联网 
try{
		OutputStream o = response.getOutputStream();
		
		// 将图片转换成字符串
		File f = new File("f:\Vista.png");
		FileInputStream fis = new FileInputStream( f );
		byte[] bytes = new byte[fis.available()];
		fis.read(bytes);
		fis.close();
		
		// 生成字符串
		String imgStr = byte2hex( bytes );
		System.out.println( imgStr);
		
		// 将字符串转换成二进制,用于显示图片
		// 将上面生成的图片格式字符串 imgStr,还原成图片显示
		byte[] imgByte = hex2byte( imgStr );
		InputStream in = new ByteArrayInputStream( imgByte );
		
		byte[] b = new byte[1024];
		int nRead = 0;
		while( ( nRead = in.read(b) ) != -1 ){
			o.write( b, 0, nRead );
		}
		o.flush();
		o.close();
		in.close();
		
		
	}catch(Exception e){
		e.printStackTrace();
	}finally{
	}try{
		OutputStream o = response.getOutputStrea



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: