#
Junjie
2026-01-29 bc9cc84b5d074076692eedf4951584bb17f8985f
src/main/java/com/zy/asrs/controller/ApkBuildTaskController.java
@@ -1,5 +1,6 @@
package com.zy.asrs.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
@@ -13,6 +14,13 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@@ -162,6 +170,40 @@
        }
    }
    @RequestMapping(value = "/apkBuildTask/downloadFile/{id}/auth", method = RequestMethod.GET)
    @ManagerAuth
    public void downloadApkFile(@PathVariable("id") Long id, HttpServletResponse response) {
        try {
            String localPath = apkBuildTaskService.downloadApk(id);
            File file = new File(localPath);
            if (!file.exists()) {
                throw new RuntimeException("APK文件不存在");
            }
            String fileName = URLEncoder.encode(file.getName(), "UTF-8");
            response.setContentType("application/vnd.android.package-archive");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setHeader("Content-Length", String.valueOf(file.length()));
            try (InputStream inputStream = new FileInputStream(file);
                 OutputStream outputStream = response.getOutputStream()) {
                byte[] buffer = new byte[8192];
                int len;
                while ((len = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, len);
                }
                outputStream.flush();
            }
        } catch (Exception e) {
            try {
                response.reset();
                response.setCharacterEncoding("utf-8");
                response.setContentType("application/json; charset=utf-8");
                response.getWriter().print(JSON.toJSONString(R.error("下载APK失败: " + e.getMessage())));
            } catch (IOException ignore) {
            }
        }
    }
    /**
     * 通过ADB安装APK到指定设备
     */