根据图片地址下载图片 获取长宽 大小

public File getFile(String url) throws Exception {
        //对本地文件命名
        //http://oyxe80s4l.bkt.clouddn.com/image/activity/20201130/896d997f49c141fbab9147478db082f2.jpg
//        String fileName = url.substring(url.lastIndexOf("."),url.lastIndexOf("?"));
        File file = null;

        URL urlfile;
        InputStream inStream = null;
        OutputStream os = null;
        try {
            file = File.createTempFile("net_url", ".jpg");
            //下载
            urlfile = new URL(url);
            inStream = urlfile.openStream();
            os = new FileOutputStream(file);

            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os) {
                    os.close();
                }
                if (null != inStream) {
                    inStream.close();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return file;
    }
    public Integer addPhoto(PhotoEntity photoEntity) {
        String orgUrl=null;
        if (photoEntity.getPhotoOrgImgurl().contains("oyxe80s4l.bkt.clouddn")) {
            orgUrl=photoEntity.getPhotoOrgImgurl().replace("oyxe80s4l.bkt.clouddn","res.ytaxx");
        }else{
            orgUrl=photoEntity.getPhotoOrgImgurl();
        }
        BufferedImage sourceImg = null;
        File picture=null;
        File orgImg = null;
        String thumbnailUrl=orgUrl+"?imageView2/2/w/500";

        try {
            picture = getFile(thumbnailUrl);
            orgImg = getFile(photoEntity.getPhotoOrgImgurl());
            sourceImg = ImageIO.read(new FileInputStream(picture));
        } catch (Exception e) {
            throw new RuntimeException("获取图片属性异常");
        }
        photoEntity.setCreateTime(new Date()).setPhotoDownloads(0).setDeleted(1).setPhotoWidth(sourceImg.getWidth()).setPhotoImgUrl(thumbnailUrl)
                .setPhotoHeight(sourceImg.getHeight()).setSize(Integer.valueOf((int) (orgImg.length())));
        Integer integer = photoMapper.addPhoto(photoEntity);
        materialMapper.updateMateriaCount(photoEntity.getParentId(),"add");
        picture.deleteOnExit();
        orgImg.deleteOnExit();
        return integer;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值