我需要直接使用java中的包名从google play store下载apk文件。
有多种在线工具可以做类似的事情。
https://apkpure.com/region-free-apk-download
https://apps.evozi.com/apk-downloader/
如果我们提供套餐名称或Google Play网址,则会返回apk下载链接。
我尝试使用java中的链接
String url = https://apkpure.com/google-play-store/com.aws.android/download?from=details
我的代码,
void downloadFromUrl(URL url, String localFilename) throws Exception {
InputStream is = null;
FileOutputStream fos = null;
try {
URLConnection urlConn = url.openConnection();//connect
urlConn.addRequestProperty("User-Agent", "Mozilla/4.0");
is = urlConn.getInputStream(); //get connection inputstream
fos = new FileOutputStream(localFilename); //open outputstream to local file
byte[] buffer = new byte[4096]; //declare 4KB buffer
int len;
//while we have availble data, continue downloading and storing to local file
while ((len = is.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
} finally {
try {
if (is != null) {
is.close();
}
} finally {
if (fos != null) {
fos.close();
}
}
}
}
但是上面的代码返回了html页面。但我需要存储下载的apk文件。如何搞定这个?
请提供您的意见。