/**
* 最新apk
*/
@ApiOperation("最新apk")
@GetMapping("/getApk")
public R getApk(HttpServletResponse response) {
try {
// 下载的文件的路径
File file = new File(downloadPath); // downloadPath为文件所在文件夹路径,如 D:\destop\apk
// 获取该路径下所有文件
File[] files = file.listFiles();
long createTime = 0;
// 获取最新的文件
for (File file1 : Arrays.asList(files)) {
if (file1.lastModified() > createTime) {
file = file1;
createTime = file1.lastModified();
}
}
if(file.exists()){
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + file.getName());
response.addHeader("Content-Length", "" + file.length());
response.addHeader("Content-Type", "application/vnd.android.package-archive apk");
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
}
log.info("/getApk 获取最新apk成功!");
return R.ok();
} catch (Exception e) {
log.error("/getApk 获取最新apk失败:", e);
return R.error(e.toString());
}
}
从服务器上下载文件(app远程升级)
最新推荐文章于 2024-09-18 23:06:28 发布