java从字节流中读取图片或视频

       // 1. 创建一个服务端套接字
     
            ServerSocket serverSocket = new ServerSocket(8088);


                log.info("服务端开启");

                while (true) {
                    log.info("循环进行查找jpeg的头");
                    Socket socket = serverSocket.accept();
                    InputStream inputStream = socket.getInputStream();
                    
                    BufferedOutputStream bos = null;
                    FileOutputStream fos = null;
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
                    String milliDate = sdf.format(new Date());

                    int receiveBufferSize = socket.getReceiveBufferSize();
                    byte[] buf = new byte[receiveBufferSize];
                    int lens = -1;

                    boolean flag = false;

                    while ((lens = inputStream.read(buf)) != -1)

                    {


                        String strHex = "";
                        for (int n = 0; n < buf.length; n++) {
                            strHex = Integer.toHexString(buf[n] & 0xFF);
                            // 每个字节由两个字符表示,位数不够,高位补0
                            if (strHex.equals("ff") && n != buf.length - 1) {
                                String startHex2 = Integer.toHexString(buf[n + 1] & 0xFF);
                                if (startHex2.equals("d8")) {
                                    flag = true;

                                    log.info("找到图片的头了");
                                    String path = "D:" + File.separator + "ANPRPicture";

                                    File file = new File(path);
                                    if (!file.isDirectory()) {
                                        //创建文件上传目录
                                        file.mkdirs();
                                    }
                                    String realPath = path + "\\" + milliDate + ".jpg";
                                    log.info("图片存在服务器中的地址为:" + path);
                                    fos = new FileOutputStream(realPath);
                                    bos = new BufferedOutputStream(fos);

                                }

                                if (startHex2.equals("d9")) {
                                    bos.write(buf[n]);
                                    bos.write(buf[n + 1]);
                                    flag = false;
                                    log.info("找到图片的尾了");
                                    if (null != bos) {
                                        bos.flush();
                                        bos.close();
                                    }
                                    if (null != fos) {
                                        fos.close();
                                    }
                                    log.info("关闭文件输出流");
                                }
                            }

                            if (flag) {
                                bos.write(buf[n]);
                            }
                    };
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值