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

被折叠的 条评论
为什么被折叠?



