简体   繁体   English

用 Java 下载时 APK 损坏,但浏览器下载工作正常

[英]Corrupted APK when downloading in Java but browser download works fine

I am trying to download an APK hosted in a server.我正在尝试下载托管在服务器中的 APK。 When downloading via browser, it works fine.通过浏览器下载时,它工作正常。 I get 14MB.我得到 14MB。 But when downloading using Java, the APK seems corrupted since APK downloaded is just aroung 189kb.但是在使用 Java 下载时,APK 似乎已损坏,因为下载的 APK 大约为 189kb。 Below is my code when downloading using Apache Commons FileUtils:下面是我使用 Apache Commons FileUtils 下载时的代码:

URL format is similar to https://example.com/emm.apk . URL 格式类似于https://example.com/emm.apk Entering this URL in a browser directly downloads the APK without problem.在浏览器中输入此 URL 可以毫无问题地直接下载 APK。

    URL url = new URL("https://example.com/emm.apk");
    File file = new File(UUID.randomUUID().toString() +"-"+ FilenameUtils.getName(fileUrl.getPath()));
    FileUtils.copyURLToFile(url, file, 50_000, 50_000);

I also tried Java's built in File IO:我还尝试了 Java 的内置文件 IO:

    URLConnection conn = url.openConnection();
    int contentLength = conn.getContentLength();

    DataInputStream stream = new DataInputStream(url.openStream());

    byte[] buffer = new byte[contentLength];
    stream.readFully(buffer);
    stream.close();

    DataOutputStream fos = new DataOutputStream(new FileOutputStream(file));
    fos.write(buffer);
    fos.flush();
    fos.close();

I only get this problem for that particular URL.我只遇到那个特定 URL 的问题。 I cannot pinpoint what's wrong since I don't have access with the host server.由于我无法访问主机服务器,因此我无法确定出了什么问题。 Clearly the problem is with how the APK is hosted.显然,问题在于 APK 的托管方式。 Is there requirements on how servers hosting APK should be configured?托管 APK 的服务器的配置方式有要求吗? I came across application/vnd.android.package-archive , is this required for host servers?我遇到了application/vnd.android.package-archive ,这是主机服务器所必需的吗? Why does the browser (Chrome) downloads it without problem while my Java code does not?为什么浏览器(Chrome)可以毫无问题地下载它,而我的 Java 代码却没有?

It seems that host server of the APK is blocking requests based on the User-Agent request header that's why browser download is okay but when downloading in different medium, it's not.似乎 APK 的主机服务器基于User-Agent请求标头阻止请求,这就是为什么浏览器下载没问题,但在不同介质中下载时,事实并非如此。

I tried manually setting the User-Agent of the URLConnection , and it's downloading properly.我尝试手动设置URLConnectionUser-Agent ,并且它正在正确下载。

URLConnection connection = fileUrl.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
InputStream input = connection.getInputStream();
FileUtils.copyInputStreamToFile(input, file);

The 189kb is an HTML file saying the request was rejected. 189kb是一个 HTML 文件,表示请求被拒绝。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ProGuard-应用程序可在模拟器中正常运行,但在尝试构建APK时失败 - ProGuard - Application works fine in emulator, but fails when trying to build apk java下载的zip文件已损坏 - java downloading zip file is corrupted 一个简单的 Java HTTP 服务器在 ApacheBench 上失败,但在浏览器上工作正常 - A simple Java HTTP server fails with ApacheBench but works fine on a browser Java 中 JSON 的漂亮打印适用于控制台,但在浏览器中不起作用 - Pretty print for JSON in Java works fine for the console, but in browser it does not work 使用Java下载文件-文件损坏 - File download with java - Files corrupted 下载 APK 在 Chrome 浏览器中不起作用 - Download APK not working in Chrome browser 为什么在Android中下载https时,http URL为什么不以编程方式下载APK? - Why http URL does not download APK with programmatically when https is downloading in Android? 使用Java下载Zip文件已损坏 - Downloading a Zip File with Java gets it corrupted 多线程Java下载器正在下载损坏的文件? - Multi Thread java downloader downloading corrupted file? 从Android Studio运行时,应用正常运行,但在安装签名的APK后崩溃 - App works fine when running from Android Studio but crashes when signed apk installed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM