解密加密的URL图片数据

这篇博客详细介绍了如何解密加密的URL图片数据。通过建立URL连接,设置请求属性,获取输入流,实现图片数据的解密加载。过程中涉及了HttpURLConnection的使用以及处理403错误的方法。

public void decryptData(String alarmId,String picUrlPath, String password, @NonNull DecryptPictureCallback callBack) {
try {
URL picUrl = new URL(picUrlPath);
HttpURLConnection connection = (HttpURLConnection) picUrl.openConnection();
connection.setReadTimeout(10000);
connection.setDoInput(true);
//防止屏蔽程序抓取而返回403错误
connection.setRequestProperty(“User-Agent”, “Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)”);
connection.connect();
int contentLength = connection.getContentLength();
InputStream inputStream = connection.getInputStream();

        //1,下载URL数据
        byte data[] = ByteUtil.InputStreamToByte(inputStream, contentLength);
        inputStream.close();
        //2、解密数据
        byte[] decryptData = decryptData(data,password);
        if(decryptData == null){
            callBack.onError(new ErrorPair(ErrorType.APP,-1));
            return;
        }

        // 3, 将解密后的数据写到文件中,看是否解密成功
        File fileDir = new File(PIC_ROOT);
        if (!fileDir.exists())
        {
            fileDir.mkdir();
        }
        String filePath = fileDir + File.separator + alarmId + ".jpg";
        File file = new File(filePath);
        FileOutputStream fos = null;
        try
        {
            fos = new FileOutputStream(file);
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
            callBack.onError(new ErrorPair(ErrorType.APP,-1));
            return;
        }

        Bitmap bitmap = null;

        bitmap = BitmapFactory.decodeByteArray(decryptData, 0, decryptData.length);

        if (bitmap != null)
        {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

// callBack.onPictureLoaded(filePath);
}else{
fos.close();
file.delete();
}
fos.close();

    } catch (IOException e) {
        e.printStackTrace();
        callBack.onError(new ErrorPair(ErrorType.APP,-1));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值