和规划网站如何,wordpress 左右风格主题,宝安品牌网站制作,石家庄网页设计java 转换图片为字符串#xff0c;将字符串转换成图片显示#xff0c;该方法只适用于比较小的图片传输#xff0c;50K以内#xff1a;try{// 将图片转换成字符串File imgFile new File(f:\\Vista.png);FileInputStream fis new FileInputStream( imgFile );b…java 转换图片为字符串将字符串转换成图片显示该方法只适用于比较小的图片传输50K以内try{// 将图片转换成字符串File imgFile new File(f:\\Vista.png);FileInputStream fis new FileInputStream( imgFile );byte[] bytes new byte[fis.available()];fis.read(bytes);fis.close();String imgStr byte2hex( bytes );//System.out.println( imgStr);// 将字符串转换成二进制用于显示图片byte[] imgByte hex2byte( imgStr );InputStream in new ByteArrayInputStream( imgByte );byte[] b new byte[1024];int nRead 0;OutputStream o response.getOutputStream();while( ( nRead in.read(b) ) ! -1 ){o.write( b, 0, nRead );}o.flush();o.close();in.close();}catch(Exception e){e.printStackTrace();}下载次数: 701分享到 2009-10-09 20:13浏览 14991评论4 楼狂盗一枝梅2014-04-11hex2byte函数功能是转换成十六进制吧上面写的是转换成二进制3 楼狂盗一枝梅2014-04-11包里的文件是jsp文件代码倒是挺全乎的~给改了一改才能在netbeans中运行代码没问题就是不知道怎么弄的stmp Integer.toHexString(b[n] 0XFF);这段代码是什么意思b[i / 2] (byte) Integer.decode(0X str.substring(i, i 2)).intValue();这段代码又是什么意思2 楼dongjun6122013-07-30jdk里javax.xml.bind.annotation.adapters.HexBinaryAdapter中有两个方法是byte[]和String互转public String marshal(byte[] bytes)public byte[] unmarshal(String s)1 楼dongjun6122013-07-30兄弟, 虽然有文件下载, 但你贴的代码真没什么价值, 有价值的是byte2hex()这个方法, 你却没贴, 帮你贴下:public static String byte2hex(byte[] b) // 二进制转字符串{StringBuffer sb new StringBuffer();String stmp ;for (int n 0; n b.length; n) {stmp Integer.toHexString(b[n] 0XFF);if (stmp.length() 1){sb.append(0 stmp);}else{sb.append(stmp);}}return sb.toString();}public static byte[] hex2byte(String str) { // 字符串转二进制if (str null)return null;str str.trim();int len str.length();if (len 0 || len % 2 1)return null;byte[] b new byte[len / 2];try {for (int i 0; i str.length(); i 2) {b[i / 2] (byte) Integer.decode(0X str.substring(i, i 2)).intValue();}return b;} catch (Exception e) {return null;}}