1.UBUtil类,处理Blob值
package com.taxsearch.controller;
import java.io.IOException;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
/**
*
* @Title:DBUtil
* @Description: 连接数据库工具类
* @author 高敏
* @version V1.0
* @date 2018年8月16日
*/
public class DBUtil {
private static Connection connection = null;
private static PreparedStatement smt = null;
private static ResultSet rs = null;
//链接数据库
public static Connection getConnection() {
String USERNAMR = "hotelbs";
String PASSWORD = "hotelbs";
String DRVIER = "oracle.jdbc.OracleDriver";
String URL = "jdbc:oracle:thin:@192.168.88.160:1521:CDFGSDB";
try {
Class.forName(DRVIER);
connection = DriverManager.getConnection(URL, USERNAMR, PASSWORD);
System.out.println("成功连接数据库");
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFound");
} catch (SQLException e) {
System.out.println("连接分发库失败");
}
return connection;
}
//关闭连接
public static void close(){
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (smt != null) {
try {
smt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//获得Blob类型的二进制图片
public static byte[] getByteBlob(String RECORDSNO) throws IOException {
try {
connection=getConnection();
String sql=
"select DJ_UPZHAOPIAN from CGUESTERSP "
+"where RECORDSNO like '"+RECORDSNO+"'";
smt=connection.prepareStatement(sql);
rs=smt.executeQuery();
HashMap hm=new HashMap();
byte[] b = null;
if(rs.next()){
Blob bb = rs.getBlob("DJ_UPZHAOPIAN");
if(bb!=null) {
b = bb.getBytes(1, (int)bb.length());
}
}
close();
return b;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
2.登记入住控制器,base64和blob二进制互相转换
package com.taxsearch.controller;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import com.taxsearch.service.CheckInService;
import com.taxsearch.util.Json;
/**
*
* @Title:CheckInController
* @Description: 登记入住控制器
* @author 高敏
* @version V1.0
* @date 2018年8月14日
*/
@Controller
public class CheckInController {
private static Logger logger = Logger.getLogger(CheckInController.class);
@Resource
private CheckInService checkInService;
/**
* 查询民族列表
*
* @return
*/
@RequestMapping("/selectNation.do")
@ResponseBody
public Json selectNation() {
Json json = new Json();
List<HashMap<String, String>> list = checkInService.selectNation();
logger.info(list);
json.setObj(list);
json.setMsg("成功查询民族列表!");
json.setSuccess(true);
return json;
}
/**
* 处理无证审批 插入无证审批内容,等待三分钟查询无证审批结果返回给前台 当三分钟后审核成功则入住,不成功返回信息
*
* @return
*/
@RequestMapping("/insertExamine.do")
@ResponseBody
public Json insertExamine(HttpServletRequest request) {
Json json = new Json();
try {
// 获取前端的值
/*String name = "linsa"; String sex = "2";
String nation = "01";String IDType = "11";
String IDNumber = "421182199405013329";String address = "四川成都";
String time = "2018-06-18 00:24:38";String roomNumber = "101";
String photo = "111"; String hotelCode = "5101220047";*/
String name = request.getParameter("name").toString();
String sex = request.getParameter("sex").toString();
String nation = request.getParameter("nation").toString();
String IDType = request.getParameter("IDType").toString();
String IDNumber = request.getParameter("IDNumber").toString();
String address = request.getParameter("address").toString();
String time = request.getParameter("time").toString();
String roomNumber = request.getParameter("roomNumber").toString();
String photo = request.getParameter("photo").toString();
// base64格式转成Blob二进制
byte[] photoBytes = null;
try {
photoBytes = getStringImage(photo);
} catch (IOException e1) {
e1.printStackTrace();
}
String hotelCode = request.getParameter("hotelCode").toString();
// 查询RECORDSNO后三位最大值
String Old_RECORDSNO = hotelCode + time.replace("-", "").substring(0, 8) + "%";
HashMap<String, String> hashMap = checkInService.selectRECORDSNO_MAX(Old_RECORDSNO);
String RECORDSNO_MAX = hashMap == null ? "00" : hashMap.get("RECORDSNO_MAX");
// RECORDSNO后三位最大值加1为新的后三位,保留前面的零值
String prefix = "";
for (int i = 0; i < RECORDSNO_MAX.length() - 1; i++) {
char a = RECORDSNO_MAX.charAt(i);
String b = String.valueOf(a);
if (b.equals("0")) {
prefix = prefix + 0;
} else {
break;
}
}
int max = Integer.parseInt(RECORDSNO_MAX);
String num;
if (max + 1 > 999) {
num = (int) (Math.random() * 10) + "" + (int) (Math.random() * 10) + "" + (int) (Math.random() * 10);
} else if (RECORDSNO_MAX.equals("00")) {
num = "00";
} else {
num = String.valueOf(max + 1);
}
logger.info("RECORDSNO后三位:" + prefix + num);
// 插入无证审批内容
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("DJ_LVKEDAIMA", hotelCode + time.replace("-", "").substring(0, 8) + "0" + prefix + num);
map.put("DJ_XINGMING", name);
map.put("DJ_XINGBIE", sex);
map.put("DJ_MINZU", nation);
map.put("DJ_CHUSHENGRIQI", IDNumber.substring(6, 14));
map.put("DJ_ZHENGJIANLEIXING", IDType);
map.put("DJ_ZHENGJIANHAOMA", IDNumber);
map.put("DJ_SHENGSHIXIAN", IDNumber.substring(0, 6));
map.put("DJ_XIANGZHI", address);
map.put("DJ_RUZHUSHIJIAN", time);
map.put("DJ_RUZHUFANGHAO", roomNumber);
map.put("DJ_TUIFANGSHIJIAN", time);
map.put("DJ_ZHAOPIAN", photoBytes);
map.put("DJ_LVGUANDAIMA", hotelCode);
map.put("RECORDSNO", hotelCode + time.replace("-", "").replace(":", "").replace(" ", "") + prefix + num);
map.put("STATIONID", hotelCode);
logger.info("插入无证审批信息" + map);
int res = checkInService.insertExamine(map);
logger.info("成功插入无证审批信息" + res);
// 查询无证审批返回内容
String RECORDSNO = hotelCode + time.replace("-", "").replace(":", "").replace(" ", "") + prefix + num;
Thread.currentThread();
// 等待三分钟执行
// Thread.sleep(1000*60*3);
List<HashMap<String, Object>> list = checkInService.selectExamineBack(RECORDSNO);
logger.info("查询无证审批返回内容" + list);
Object SP_RESULT = list.get(0).get("SP_RESULT");
logger.info("审批结果" + SP_RESULT);
if (String.valueOf(SP_RESULT).equals("1")) {
// 审批成功,插入无证入住信息
HashMap<String, Object> sub_map = new HashMap<String, Object>();
sub_map.put("DJ_LVKEDAIMA", hotelCode + String.valueOf(generate(12)));
sub_map.put("DJ_XINGMING", name);
sub_map.put("DJ_XINGBIE", sex);
sub_map.put("DJ_MINZU", nation);
sub_map.put("DJ_CHUSHENGRIQI", IDNumber.substring(6, 14));
sub_map.put("DJ_ZHENGJIANLEIXING", IDType);
sub_map.put("DJ_ZHENGJIANHAOMA", IDNumber);
sub_map.put("DJ_SHENGSHIXIAN", IDNumber.substring(0, 6));
sub_map.put("DJ_XIANGZHI", address);
sub_map.put("DJ_RUZHUSHIJIAN", time);
sub_map.put("DJ_RUZHUFANGHAO", roomNumber);
sub_map.put("DJ_ZHAOPIAN", photoBytes);
sub_map.put("DJ_UPZHAOPIAN", list.get(0).get("DJ_UPZHAOPIAN"));
sub_map.put("DJ_LVGUANDAIMA", hotelCode);
sub_map.put("RECORDSNO", hotelCode + time.replace("-", "").replace(":", "").replace(" ", "") + prefix + num);
sub_map.put("STATIONID", hotelCode);
sub_map.put("PACKNAME", hotelCode + time.replace("-", "").replace(":", "").replace(" ", "") + prefix + num);
logger.info("插入无证入住信息" + sub_map);
int sub_res = checkInService.insertCheckIn(sub_map);
logger.info("成功插入无证入住信息" + sub_res);
// Blob二进制转成base64格式
byte[] blob = DBUtil.getByteBlob(RECORDSNO);
if(blob!=null){
String DJ_UPZHAOPIAN = "data:image/jpeg;base64," + getImageString(blob);
list.get(0).put("DJ_UPZHAOPIAN", DJ_UPZHAOPIAN);
}
json.setObj(list);
json.setMsg("审批成功,已入住!");
logger.info("审批成功,已入住!");
json.setSuccess(true);
} else {
// Blob二进制转成base64格式
byte[] blob = DBUtil.getByteBlob(RECORDSNO);
if(blob!=null){
String DJ_UPZHAOPIAN = "data:image/jpeg;base64," + getImageString(blob);
list.get(0).put("DJ_UPZHAOPIAN", DJ_UPZHAOPIAN);
}
json.setObj(list);
json.setMsg("审批失败!");
json.setSuccess(false);
logger.info("审批失败!");
}
} catch (Exception e) {
json.setMsg("审批失败,请联系管理员!");
json.setSuccess(false);
logger.info("审批失败,请联系管理员!");
e.printStackTrace();
}
return json;
}
/**
* 出入有证入住信息
*
* @return
*/
@RequestMapping("/insertCheckIn.do")
@ResponseBody
public Json insertCheckIn(HttpServletRequest request) {
Json json = new Json();
try {
// 获取前端的值
/*String name = "linsa"; String sex = "2";
String nation = "01";String IDType = "11";
String IDNumber = "421182199405013329";String address = "四川成都";
String time = "2018-06-18 00:24:38";String roomNumber = "101";
String photo = "111"; String IDPhoto = "111";
String hotelCode = "5101220047";*/
String name = request.getParameter("name").toString();
String sex = request.getParameter("sex").toString();
String nation = request.getParameter("nation").toString();
String IDType = request.getParameter("IDType").toString();
String IDNumber = request.getParameter("IDNumber").toString();
String address = request.getParameter("address").toString();
String time = request.getParameter("time").toString();
String roomNumber = request.getParameter("roomNumber").toString();
String photo = request.getParameter("photo").toString();
// base64格式转成Blob二进制
byte[] photoBytes = null;
photoBytes = getStringImage(photo);
String IDPhoto = request.getParameter("IDPhoto").toString();
// base64格式转成Blob二进制
byte[] IDPhotoBytes = null;
IDPhotoBytes = getStringImage(IDPhoto);
String hotelCode = request.getParameter("hotelCode").toString();
String num = (int) (Math.random() * 10) + "" + (int) (Math.random() * 10) + "" + (int) (Math.random() * 10);
logger.info("RECORDSNO后三位:" + num);
// 插入有证入住信息
HashMap<String, Object> sub_map = new HashMap<String, Object>();
sub_map.put("DJ_LVKEDAIMA", hotelCode + String.valueOf(generate(12)));
sub_map.put("DJ_XINGMING", name);
sub_map.put("DJ_XINGBIE", sex);
sub_map.put("DJ_MINZU", nation);
sub_map.put("DJ_CHUSHENGRIQI", IDNumber.substring(6, 14));
sub_map.put("DJ_ZHENGJIANLEIXING", IDType);
sub_map.put("DJ_ZHENGJIANHAOMA", IDNumber);
sub_map.put("DJ_SHENGSHIXIAN", IDNumber.substring(0, 6));
sub_map.put("DJ_XIANGZHI", address);
sub_map.put("DJ_RUZHUSHIJIAN", time);
sub_map.put("DJ_RUZHUFANGHAO", roomNumber);
sub_map.put("DJ_ZHAOPIAN", photoBytes);
sub_map.put("DJ_UPZHAOPIAN", IDPhotoBytes);
sub_map.put("DJ_LVGUANDAIMA", hotelCode);
sub_map.put("RECORDSNO", hotelCode + time.replace("-", "").replace(":", "").replace(" ", "") + num);
sub_map.put("STATIONID", hotelCode);
sub_map.put("PACKNAME", hotelCode + time.replace("-", "").replace(":", "").replace(" ", "") + num);
logger.info("插入有证入住信息" + sub_map);
int sub_res = checkInService.insertCheckIn(sub_map);
logger.info("成功插入有证入住信息" + sub_res);
json.setObj(sub_res);
json.setMsg("成功入住!");
logger.info("成功入住!");
json.setSuccess(true);
} catch (Exception e) {
json.setMsg("入住失败,请联系管理员!");
json.setSuccess(false);
logger.info("入住失败,请联系管理员!");
e.printStackTrace();
}
return json;
}
/**
* 产生随机数
*
* @return
*/
public static char[] generate(int grade) {
char[] chs = new char[grade];
// 随机字符范围数组
char[] letters = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9' };
// boolean[] flag=new boolean[letters.length];//默认都存为false
for (int i = 0; i < chs.length; i++) {
int index;
index = (int) (Math.random() * letters.length);
chs[i] = letters[index];// 基于下标index到letters中选字符
/*
* do { index=(int)(Math.random()*letters.length);
* }while(flag[index]);// 判断生成的字符是否重复
* chs[i]=letters[index];//基于下标index到letters中选字符 flag[index]=true;
*/
}
return chs;
}
/**
*
* 二进制流转Base64字符串
*
* @param data
* 二进制流
* @return data
* @throws IOException
* 异常
*/
public static String getImageString(byte[] data) throws IOException {
BASE64Encoder encoder = new BASE64Encoder();
return data != null ? encoder.encode(data) : "";
}
/**
* Base64字符串转 二进制流
*
* @param base64String
* Base64
* @return base64String
* @throws IOException
* 异常
*/
public static byte[] getStringImage(String base64String) throws IOException {
BASE64Decoder decoder = new sun.misc.BASE64Decoder();
return base64String != null ? decoder.decodeBuffer(base64String) : null;
}
}