詳解JAVA生成將圖片存入數(shù)據(jù)庫(kù)的sql語(yǔ)句實(shí)現(xiàn)方法
實(shí)現(xiàn)代碼:
注釋很清楚,不在重述~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
public class Image2Hex { public static void main(String[] args) { try { //存放圖片的文件夾 File list = new File( "d:/qmx" ); File[] lists = list.listFiles(); String name; //生成的語(yǔ)句存放文件 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轉(zhuǎn)字符串 */ 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(); } } |
現(xiàn)在的項(xiàng)目需要存放用戶公司的員工的簽名圖片,美工做出來幾百?gòu)垐D片需要存放到數(shù)據(jù)庫(kù)里,于是就寫了這么一個(gè)將圖片讀成十六進(jìn)制并拼寫update語(yǔ)句存放到文本文檔中的方法,效果還是不錯(cuò)的。
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,本站關(guān)于java開發(fā)的文章還有很多,歡迎大家搜索查閱,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
原文鏈接:http://wangzemin.iteye.com/blog/2205802