JAVA生成将图片存入数据库(IMAGE类型)的sql语句

public class Image2Hex {
	public static void main(String[] args) {
		try{
			//存放图片的文件夹
			File list = new File("d:/qmx");
			File[] lists = list.listFiles();
			String name;
			//生成的语句存放文件
			PrintWriter pw = new PrintWriter(new FileWriter("d:/update.txt"),true);
			FileInputStream fis = null;
			byte[] b;
			for(File file : lists){
				//张三.jpg
				name=file.getName();
				fis = new FileInputStream(file);
				b = new byte[fis.available()];
				fis.read(b);
		        pw.println("update sys_userinfo set sign_image =0x" + byte2HexStr(b) + " where realName=\'" + name.substring(0,name.length() - 4) + "\'");      
			}
			pw.flush();
			pw.close();   
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * 
	 * 描述:byte转字符串
	 */
	public static String byte2HexStr(byte[] b) {   
		StringBuffer hs = new StringBuffer();   
		String stmp="";   
		for (int n=0;n< b.length;n++) {   
			stmp=(Integer.toHexString(b[n] & 0XFF));
			hs.append((stmp.length() == 1 ? "0" : "") + stmp);
		}   
		return hs.toString().toUpperCase();   
	}   
}

 现在的项目需要存放用户公司的员工的签名图片,美工做出来几百张图片需要存放到数据库里,于是就写了这么一个将图片读成十六进制并拼写update语句存放到文本文档中的方法,效果还是不错的。。。。。→_

PS:一般不建议将图片或文件存入数据库。+_+

猜你喜欢

转载自wangzemin.iteye.com/blog/2205802
今日推荐