ZKFinger Live20R 版对接java - B/S(网络版)

查看此文章前请先查看ZKFinger Live20R 版对接java - B/S(ZKFinger SDK 5.0.0.32 )https://blog.csdn.net/qq_27185879/article/details/110387558

查看此文需要了解:

1.ZKFinger Live20R只支持单机!!!为了省事,建议换其它型号

2.本文涉及的上传服务依赖于java语言实现(也可以换成node服务或者其它语言)

3.查看本文的之前,已经查看了上文中的内容,并已实现!!!

4.和ZKFinger Live20R杠上了,没有条件就创造条件实现联机!!!

一、思路图

 二、实现

1.场景模拟:

               打印一个服务协议单,需要采集用户指纹并打印出来,且需要留电子档

②服务端

     1.提供上传和查询服务,用于上传图片和回显。(具体业务逻辑请自行实现)

      假设:上传接口:http://192.168.1.209/img/save   POST  文件参数名为 file

                 查询接口:http://192.168.209/img/get  

②客户端(客户机)

 1.生成有规则的图片文件名比如 user_id.jpg(为了在上传接口中通过文件名与业务做绑定)

 2.将本地本地文件通过上传接口:http://192.168.1.209/img/save  将文件上传至服务器

 3.回显,通过http://192.168.1.209/img/get将想要的指纹数据回显至客户机

 

 注:以上仅提送思路,  以下附一个java版的上传服务示例,如果采用base64上传的模式,请忽略下文

(当然也可以使用其它语言实现自动上传服务)  

package com.lvyq.controller;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.activation.MimetypesFileTypeMap;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 本地文件自动上传
 */
@Component
public class FileJob {
    /**
     * 本地图片上传-1s
     */
   @Scheduled(fixedRate = 1000)
    public void  uploadFile() throws Exception{
        File folder = new File("E:\\FileSpaces\\fmis\\document\\files\\finger\\");
        File[] listOfFiles = folder.listFiles();
        for (File file : listOfFiles) {
            if (file.isFile()) {
                System.out.println(file.getName());
                String strUrl = folder+"\\"+file.getName();
                sendPostUploadFile(strUrl);
                //TODO 删除本地文件
                file.delete();
            }
        }
    }
    /**
     * 上传文件
     * @param path
     * @return
     */
    public String sendPostUploadFile(String path) {
        DataOutputStream out = null;
        BufferedReader in = null;
        String result = "";
        try {
            //上传文件
            URL realUrl = new URL("http://192.168.1.209/img/save");
            HttpURLConnection conn = (HttpURLConnection)realUrl.openConnection();
            conn.setRequestMethod("POST");
            conn.setUseCaches(false);
            //发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            //BOUNDARY 分界线,随便设置
            String BOUNDARY = "----Boundary";
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36");
            conn.setRequestProperty("Charsert", "UTF-8");
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
            conn.connect();
            out = new DataOutputStream(conn.getOutputStream());
            byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
            //添加参数file
            File file = new File(path);
            StringBuffer sb = new StringBuffer();
            sb.append("--");
            sb.append(BOUNDARY);
            sb.append("\r\n");
            sb.append("Content-Disposition: form-data;name=\"file\";filename=\"" + file.getName() + "\"");
            sb.append("\r\n");
            sb.append("Content-Type: "+getContentType(path));
            sb.append("\r\n");
            sb.append("\r\n");
            out.write(sb.toString().getBytes());
            DataInputStream in1 = new DataInputStream(new FileInputStream(file));
            int bytes = 0;
            byte[] bufferOut = new byte[1024];
            while ((bytes = in1.read(bufferOut)) != -1) {
                out.write(bufferOut,0,bytes);
            }
            out.write("\r\n".getBytes());
            in1.close();
            out.write(end_data);
            //flush输出流的缓冲
            out.flush();
            //定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 获取文件类型
     * @param fileUrl
     * @return
     */
    public static String getContentType(String fileUrl) throws IOException {
        URLConnection connection = new File(fileUrl).toURL().openConnection();
        String mimeType = connection.getContentType();
        System.out.println("getContentType, File ContentType is : " + mimeType);
        return mimeType;
    }

}

每1s上传一次本地图片,并清空本地图片。

             注:以上思路不是最优解,意在提供基础思路!!!由此可扩散性优化

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不要喷香水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值