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信息。。。


上一篇:Java已知图片路径下载图片到本地


下一篇:opencv-python轮廓检测:findContours