通过url获取文件流,保存到其他服务器

 public  byte[] getData(String filePath)throws IOException{
            URL url = new URL(filePath);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            //设置超时间为3秒
            conn.setConnectTimeout(3*1000);
            //防止屏蔽程序抓取而返回403错误
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

            //得到输入流
            InputStream inputStream = conn.getInputStream();

            byte[] buffer = new byte[1024];
            int len = 0;
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            while((len = inputStream.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
            bos.close();
        return bos.toByteArray();
    }
  public PortraitView getPortraitView(TMDevice device, TRecordVisitor record, TMVisitor tmVisitor){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        PortraitView portraitKafkaView=new PortraitView();
        portraitKafkaView.setDeviceIP(device.getIpAddress());
        portraitKafkaView.setIdentity(device.getMacAddress().replace(":",""));
        portraitKafkaView.setCaptureTime(sdf.format(record.getVisitTime()));

        try {
            portraitKafkaView.setScenePath(client.uploadImageWithTag(getData("http://"+record.getVisitPicUrl()), StoreType.LOC, ImageType.JPG));
            String[] faceUrl={client.uploadImageWithTag(getData("http://"+record.getVisitFaceUrl()), StoreType.LOC, ImageType.JPG, ImageCategory.CaptureImage)};
            portraitKafkaView.setFacePaths(faceUrl);
        } catch (IOException e) {
            e.printStackTrace();
        }
        portraitKafkaView.setCardId(tmVisitor.getIdCard());
        portraitKafkaView.setBodyPaths(new String[]{});
        Map<String,Object> json=new HashMap<String,Object>();
        json.put("name",tmVisitor.getName());
        json.put("sex",tmVisitor.getSex());
        json.put("nation",tmVisitor.getNation());
        json.put("cardId",tmVisitor.getIdCard());
        json.put("birthday",tmVisitor.getBirthday());
        json.put("address",tmVisitor.getAddress());
        json.put("depart",tmVisitor.getDepart());
        json.put("validStart",tmVisitor.getValidStart());
        json.put("validEnd",tmVisitor.getValidEnd());
        portraitKafkaView.setExt(JSON.toJSONString(json));
        return portraitKafkaView;
    }

参考文档:https://blog.csdn.net/wu8439512/article/details/78481479

 

posted on 2018-07-10 15:26  让代码飞  阅读(1681)  评论(0)    收藏  举报

导航

一款免费在线思维导图工具推荐:https://www.processon.com/i/593e9a29e4b0898669edaf7f?full_name=python