目录

chen 的个人博客

VX:ZzzChChen
Phone:13403656751
Email:zxydczzs@gmail.com

解决Java文件下载后缀为.apk,下载完成变.zip

在手机 Chrome 浏览器中访问接口下载使用 Java IO 流写的文件下载,下载的内容后缀为.apk 的 app 安装包,下载完成后后缀变为.zip。

电脑端的浏览器一直正常下载,没有去在意手机的浏览器,而且手机的浏览器我是用了 oppo 自带的浏览器和 QQ 浏览器都是正常下载的,直到后来用了 Chrome 浏览器时,发现下载的文件安装不了,点击后会跳转解压,而一些没有解压软件的手机会提示文件打不开等提示。

解决方案:

 1public void downloadApk(HttpServletResponse httpServletResponse) throws UnsupportedEncodingException {
 2        String filePath = "你自己的文件路径";
 3        File file = new File(filePath);
 4        httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
 5        httpServletResponse.setHeader("content-type", "application/vnd.android.package-archive");//增加了这行代码就可以在手机Chrome浏览器中访问接口下载APK了
 6        OutputStream out;
 7        try (InputStream in = new FileInputStream(filePath)) {
 8            int len;
 9            byte buffer[] = new byte[1024];
10            out = httpServletResponse.getOutputStream();
11            while ((len = in.read(buffer)) != -1) {
12                out.write(buffer, 0, len);
13            }
14        } catch (Exception e) {
15            logger.error("查看图片失败" + e.getMessage());
16        }

转载至:https://blog.csdn.net/u013067891/article/details/82967961


标题:解决Java文件下载后缀为.apk,下载完成变.zip
作者:zzzzchen
地址:https://dczzs.com/articles/2021/04/28/1619605399576.html