public void downloadAppApk(HttpServletResponse response, String path, String fileName) {
File file = new File(path);
if (!file.exists()) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
logger.warn("Requested file not found: " + path);
return ;
}
try (InputStream fis = new BufferedInputStream(Files.newInputStream(file.toPath()));
OutputStream toClient = new BufferedOutputStream(response.getOutputStream())) {
response.reset();
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "attachment;filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\"");
response.setContentType("application/vnd.android.package-archive");
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
toClient.write(buffer, 0, bytesRead);
}
toClient.flush();
} catch (Exception e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
logger.error("Error processing file download: " + e.getMessage(), e);
}
}
09-30
1169
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)