最新示例全
js端口
//黑名单验证(jsonp跨域)
$.ajax({
url: "http://localhost:6060/shjlPub/blackListValidateJsonp",
type: "post",
data: {idCard: CARDNO},
async: true,//true为异步,false为同步
dataType: "jsonp",
//jsonp: "callback",//(默认为:callback)传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名
jsonpCallback:"success_jsonpCallback",//使用自定义的固定的jsonp回调函数名称,(默认为:jQuery自动生成随机函数名)
success: function(data,status,xhr){
if(data.status == 0){//成功
alert("校验通过");
}else if(data.status == -1){//失败
layer.alert(data.msg, {icon: 0});
//设置提示
$("#Name").val(data.msg);
$("#Name").attr("style","color:red");
//禁用页面
$("#Name").attr("disabled",true);
}else{//异常
layer.alert("异常:" + xhr.responseText, {icon: 2});
}
},
error:function(xhr,status,error){
layer.alert("失败:" + xhr.responseText, {icon: 2});
}
});
Java端
/**
* 黑名单验证(Jsonp)
* @param idCard
* @return
*/
@RequestMapping("/blackListValidateJsonp")
@ResponseBody
public String blackListValidateJsonp(String idCard) {
System.out.println("---黑名单验证 idCard=" + idCard);
ResultUtil result = svc.blackListValidate(idCard);
//使用自定义的固定的jsonp回调函数名称,(默认为:jQuery自动生成随机函数名),(jsonp跨域)callback方法名包裹真正信息
System.out.println("success_jsonpCallback(" + result.toStringJsonp() + ")");
return "success_jsonpCallback(" + result.toStringJsonp() + ")";
}
ResultUtil的toString改造
//like12 add,20220526,兼容Jsonp跨域(默认的toString()与JSONObject格式不一致,不能用)
public String toStringJsonp() {
return "{" +
"\"status\":" + status +
",\"result\":\"" + result + '\"' +
",\"msg\":\"" + msg + '\"' +
",\"data\":" + data +
",\"token\":\"" + token + '\"' +
'}';
}
js端
//like12 modified,20220104,优化 改为IDEA模式(jsonp跨域)
$.ajax({
url: "http://127.0.0.1:9090/cameraPhoto/saveCameraPhoto",
type: "post",
data: {
"photo": imgBase64Data,
"idCard": examIdGlobal + "_" + cameraIndexGlobal.toString()
},
dataType: "jsonp",
//jsonp: "callback",//(默认为:callback)传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名
//jsonpCallback:"success_jsonpCallback",//使用自定义的固定的jsonp回调函数名称,(默认为:jQuery自动生成随机函数名)
success: function(data){
cameraIndexGlobal++;
//dorado.MessageBox.alert(data.tailPath);
},
error: function(){
cameraIndexGlobal++;
dorado.MessageBox.alert("jsonp跨域调用失败");
}
});
java端
//(jsonp跨域)获得callback方法名字
String callbackFuncation = request.getParameter("callback");
//注意:必须返回json格式 否则进不到success回调函数
JSONObject json = new JSONObject();
json.put("tailPath", tailPath);
//(jsonp跨域)callback方法名包裹真正信息
return callbackFuncation + "(" + json + ")";
java完整
/**
* 保存图片
* @param request
* @return
*/
@RequestMapping("/saveCameraPhoto")
@ResponseBody
public String saveCameraPhoto(HttpServletRequest request) {
String photo = request.getParameter("photo");
String idCard = request.getParameter("idCard");
//(jsonp跨域)获得callback方法名字
String callbackFuncation = request.getParameter("callback");
System.out.println("---抓拍图片保存 idCard=" + idCard);
//Base64解码
byte[] photoBuff = null;
try {
photoBuff = Base64.decode(photo);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//获取保存文件路径及名称
String uploadDir = "F:/Java/MySQL/image_test";
String fileName = idCard + ".jpg";
String finalPath = uploadDir + "/camera_photo/" + fileName;
String tailPath = "camera_photo/" + fileName;
//##############################################################//
//like12 add,20201214,判断路径是否存在(自动创建文件夹)
String tempPath = uploadDir + "/camera_photo/";
File tempFile = new File(tempPath);
if (!tempFile.exists()) {
tempFile.mkdirs();
}
//##############################################################//
//png转jpg
try {
//bug,ImageIO.read会让图片蒙上一层红色
//Image src=Toolkit.getDefaultToolkit().getImage(file.getPath());
Image imageTookit = Toolkit.getDefaultToolkit().createImage(photoBuff);
BufferedImage img = toBufferedImage(imageTookit);
//InputStream buffIn = new ByteArrayInputStream(photoBuff);
//BufferedImage img = ImageIO.read(buffIn);
ImageIO.write(img, "jpg", new File(finalPath));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//注意:必须返回json格式 否则进不到success回调函数
JSONObject json = new JSONObject();
json.put("tailPath", tailPath);
//(jsonp跨域)callback方法名包裹真正信息
return callbackFuncation + "(" + json + ")";
}
//BufferedImageBuilder
public BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
// This code ensures that all the pixels in the image are loaded
image = new ImageIcon(image).getImage();
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
try {
int transparency = Transparency.OPAQUE;
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(image.getWidth(null),
image.getHeight(null), transparency);
} catch (HeadlessException e) {
// The system does not have a screen
}
if (bimage == null) {
// Create a buffered image using the default color model
int type = BufferedImage.TYPE_INT_RGB;
bimage = new BufferedImage(image.getWidth(null),
image.getHeight(null), type);
}
// Copy image to buffered image
Graphics g = bimage.createGraphics();
// Paint the image onto the buffered image
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}