SpringBoot我想获取图片流,然后将图片流的数据传到服务器上

我想获取图片流,然后将图片流的数据传到服务器上

这行好像是获取图片流的,但是这个buffers我怎么传递给服务器

我想把buffers作为参数传给口,不知道怎么实现?

ByteBuffer buffers = strItsPlateResult.struPicInfo[i].pBuffer.getByteBuffer(offset, strItsPlateResult.struPicInfo[i].dwDataLen);

1、这段代码是跑在本地电脑上的

2、有个远程服务端,我希望跑这段代码时,可以将图片流传给远程的服务器

3、我希望将图片流传给远程服务器的接口

for(int i=0;i<strItsPlateResult.dwPicNum;i++)
                    {
                        if(strItsPlateResult.struPicInfo[i].dwDataLen>0)
                        {
                            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
                            String newName = sf.format(new Date());
                            FileOutputStream fout;
                            try {
//                                String filename = ".\\pic\\"+ new String(pAlarmer.sDeviceIP).trim() + "_"
//                                        + newName+"_type["+strItsPlateResult.struPicInfo[i].byType+"]_ItsPlate.jpg";
                                String filename = "D:\\ScenePics\\Plate20210416165034669.jpg";
                                fout = new FileOutputStream(filename);
                                //将字节写入文件
                                long offset = 0;
                                ByteBuffer buffers = strItsPlateResult.struPicInfo[i].pBuffer.getByteBuffer(offset, strItsPlateResult.struPicInfo[i].dwDataLen);
                                System.out.println("buffers == " + buffers);
                                byte [] bytes = new byte[strItsPlateResult.struPicInfo[i].dwDataLen];

                                System.out.println("bytes == " + bytes);
                                
                                buffers.rewind();
                                buffers.get(bytes);
                                fout.write(bytes);
                                fout.close();
                            } catch (FileNotFoundException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }

获取图片数据流接口

/**
     * 从输入流中获取数据
     *
     * 输入流
     * @return
     * @throws Exception
     */
    @ApiOperation("获取图片二进制流")
    @RequestMapping(value="/v1/bulldozer-info/inputStream", method={ RequestMethod.POST })
    public static byte[] readInputStream(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String fileName = request.getParameter("fileName");
        log.info("filename:"+fileName);
//        String fileName ="shafei.xls";
//        String fileFullPath = "C:/Users/Dell/Desktop/print/test/" + fileName;
        String fileFullPath = "/Users/shanguangqing/Desktop/company20210122/bulldozer-admin-java/abbott-cloud-core/target/" + fileName;

        InputStream input = null;
        FileOutputStream fos = null;

        try {
            input = request.getInputStream();
            File file = new File("/Users/shanguangqing/Desktop/company20210122/bulldozer-admin-java/abbott-cloud-core/target/");
            if(!file.exists()){
                file.mkdirs();
            }
            fos = new FileOutputStream(fileFullPath);
            int size = 0;
            byte[] buffer = new byte[1024];
            while ((size = input.read(buffer,0,1024)) != -1) {
                fos.write(buffer, 0, size);
            }

            //响应信息 json字符串格式
            Map<String,Object> responseMap = new HashMap<String,Object>();
            responseMap.put("flag", true);

            //生成响应的json字符串
            String jsonResponse = JSONObject.toJSONString(responseMap);
//                sendResponse(jsonResponse,response);
        } catch (IOException e) {
            //响应信息 json字符串格式
            Map<String,Object> responseMap = new HashMap<String,Object>();
            responseMap.put("flag", false);
            responseMap.put("errorMsg", e.getMessage());
            String jsonResponse = JSONObject.toJSONString(responseMap);
   //         sendResponse(jsonResponse,response);
        } finally{
            if(input != null){
                input.close();
            }
            if(fos != null){
                fos.close();
            }
        }
        return null;
    }
阅读 5.1k
3 个回答

你展示的代码里已经将图片buffer数据保存为图片文件了,不是太理解你这里的服务器是指第三方服务器还是指运行你自己springboot程序的服务器

为了方便起见,下面的工具类全部使用 hutool。

服务端

    @PostMapping("/v1/bulldozer-info/inputStream")
    public String readInputStream(@RequestBody String body) {
        // 接收 base64 并重新转化成二进制
        byte[] imageData = Base64.decode(body);

        try {
            //写入文件
            FileUtil.writeBytes(imageData, new File("xxx/xxx.jpeg"));
        } catch (IORuntimeException e) {
            e.printStackTrace();
            return "failure";
        }

        return "success";
    }

客户端

    public static void main(String[] args) {
        //读取图片的二进制数据,并转换成 base64
        byte[] imageData = FileUtil.readBytes("imagepath");
        String imageBase64 = Base64.encode(imageData);

        HttpResponse execute = HttpUtil.createPost("http://ip:port/v1/bulldozer-info/inputStream")
                .body(imageBase64)
                .execute();

        // 应该是 success
        System.out.println(execute.body());
    }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题