日期:2014-05-20 浏览次数:20809 次
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;