在Java中下载时APK文件损坏,但在浏览器下载正常。

huangapple go评论64阅读模式
英文:

Corrupted APK when downloading in Java but browser download works fine

问题

我正在尝试从服务器下载一个APK文件。通过浏览器下载时,一切正常,文件大小为14MB。但是,当我使用Java下载时,下载的APK文件似乎损坏,因为其大小仅约为189KB。以下是我使用Apache Commons FileUtils进行下载的代码:

URL的格式类似于https://example.com/emm.apk。在浏览器中直接输入此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);

我还尝试了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();

我只在特定的URL上遇到了这个问题。我无法确定问题出在哪里,因为我无法访问主机服务器。显然,问题出在APK文件的主机方式上。主机APK文件的服务器是否需要特定的配置要求?我遇到了application/vnd.android.package-archive,主机服务器是否需要这个?为什么浏览器(Chrome)可以正常下载,而我的Java代码却不行?

英文:

I am trying to download an APK hosted in a server. When downloading via browser, it works fine. I get 14MB. But when downloading using Java, the APK seems corrupted since APK downloaded is just aroung 189kb. Below is my code when downloading using Apache Commons FileUtils:

URL format is similar to https://example.com/emm.apk. Entering this URL in a browser directly downloads the APK without problem.

    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:

    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. 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. Is there requirements on how servers hosting APK should be configured? I came across application/vnd.android.package-archive, is this required for host servers? Why does the browser (Chrome) downloads it without problem while my Java code does not?

答案1

得分: 1

似乎APK的主机服务器正在根据User-Agent请求头拦截请求,这就是为什么在浏览器下载时没问题,但在其他方式下载时会出问题。

我尝试手动设置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);

那个189kb的HTML文件显示请求被拒绝。

英文:

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.

I tried manually setting the User-Agent of the URLConnection, and it's downloading properly.

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.

huangapple
  • 本文由 发表于 2020年10月8日 01:27:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64249261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定