详解JAVA生成将图片存入数据库的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语句存放到文本文档中的方法,效果还是不错的。

如有疑问请留言或者到本站社区交流讨论,本站关于java开发的文章还有很多,欢迎大家搜索查阅,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。