下面很长的东西为一张图片的16进制转码
/**
*
*/
@RequestMapping(value = LIST + "/testImg",method=RequestMethod.POST)
@ResponseBody
public String testImg(String imgByte){
String path = "D://imgSave";
File dir = new File(path);
imgByte = "图片16进制代码";
//检查目录
if(!dir.isDirectory()){
throw new BusinessException("上传目录不存在");
}
//检查目录写权限
if(!dir.canWrite()){
throw new BusinessException("上传目录没有写权限");
}
path = "D://imgSave//test.jpg"; //path到下面方法调用的时候需要有带记录下来的文件名
try {
com.dql.lms.system.util.HexToImage.toImage(imgByte, path);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("十六进制转换图片报错");
e.printStackTrace();
}
return "1";
}
上面方法调用的方法
package com.dql.lms.system.util;
import java.io.File;
import java.io.FileOutputStream;
public class HexToImage {
public static void toImage(String image, String path) throws Exception {
HexToImage to=new HexToImage();
/*InputStream is=new FileInputStream("f://cc.txt");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String str = null;
StringBuilder sb = new StringBuilder();
while ((str = br.readLine()) != null) {
//System.out.println(str);
sb.append(str);
}
//str.replaceAll(" ", "");
//String aaa = sb.toString();
//aaa.replaceAll(" ", "");
System.out.println(sb.toString().replaceAll(" ", ""));
//System.out.println(str);
to.saveToImgFile(sb.toString().replaceAll(" ", "").replace("<", "").replace(">", "").toUpperCase(),path);*/
if (image != null) {
to.saveToImgFile(image.replaceAll(" ", "").replace("<", "").replace(">", "").toUpperCase(), path);
}
}
public void saveToImgFile(String src,String output){
if(src==null||src.length()==0){
return;
}
try{
FileOutputStream out = new FileOutputStream(new File(output));
byte[] bytes = src.getBytes();
for(int i=0;i<bytes.length;i+=2){
out.write(charToInt(bytes[i])*16+charToInt(bytes[i+1]));
}
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
private int charToInt(byte ch){
int val = 0;
if(ch>=0x30&&ch<=0x39){
val=ch-0x30;
}else if(ch>=0x41&&ch<=0x46){
val=ch-0x41+10;
}
return val;
}
}