2021-05-08

java实现apk下载

今天上重构后的系统,因为项目重构后使用的是服务器本地资源,app不能提示升级重新下载了。so而老板要求要用扫码方式下载app,百度许久找到


代码实现

	public void downloadApk( HttpServletResponse response) {
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		try {
			//根据用户的图片路径,创建文件
			File file = new File("D:\\uploadFile\\apk\\yunqing.apk");
			// 配置文件下载
			response.setHeader("content-type", "application/octet-stream");
			response.setContentType("application/octet-stream");
			// 下载文件能正常显示中文
			response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("yunqing.apk", "UTF-8"));
			response.setHeader("Content-Length",""+file.length());
			// 实现文件下载
			byte[] buffer = new byte[1024];


			fis = new FileInputStream(file);
			bis = new BufferedInputStream(fis);
			OutputStream os = response.getOutputStream();
			int i = bis.read(buffer);
			while (i != -1) {
				os.write(buffer, 0, i);
				i = bis.read(buffer);
			}
			log.info("Download the song successfully!");

		} catch (Exception e) {
			log.error("Download the song successfully!");
			e.printStackTrace();
		}finally {
			if (bis != null) {
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

因为资源来源是服务器本地,而不oss等服务,所以直接用的file方式获取apk信息。。。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值