16进制码转换成图片(可用)

本文介绍了一种将16进制字符串转换为图片的方法。通过接收16进制编码的图片数据,验证目录存在及写权限后,利用自定义工具类完成转换并保存为指定路径下的图片文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面很长的东西为一张图片的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;  
       }  
}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值