android远程更新下载apk

最近业务有涉及到,奈何是个app代码小白,遂记录一下

一:AndroidManifest.xml文件配置

application标签里面加上

复制代码
android:networkSecurityConfig="@xml/network_config"
XML 复制代码
<!--    app下载更新配置-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" tools:ignore="ProtectedPermissions" />
<!-- 存储权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- 安装APK权限 -->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />







<!-- 将以下 <provider> 标签移动到 <application> 标签内部 -->
<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="com.shuye.znsy.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>
复制代码

二:file_paths.xml

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <!--安装包文件存储路径-->
    <external-files-path
        name="my_download"
        path="Download" />
    <external-path
        name="."
        path="." />
</paths>

三:network_config.xml文件

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

如果没有这两个文件需要新建然后将以上美容复制进去

四:progress.xml滚动条布局

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
    <LinearLayout
        android:id="@+id/titleBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
 
        <TextView
            android:id="@+id/txtStatus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="状态"
            android:textSize="10sp"
            android:textStyle="normal" />
 
        <ProgressBar
            android:id="@+id/progress"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/txtStatus" />
    </LinearLayout>
</LinearLayout>

五:新建AutoUpdater.java 这里是核心

XML 复制代码
package com.shuye.znsy.update;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.content.FileProvider;

import com.shuye.znsy.BuildConfig;
import com.shuye.znsy.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class AutoUpdater {
    // 下载安装包的网络路径
    private String apkUrl = BuildConfig.host + "app/upload/download-apk";

    protected String checkUrl = apkUrl + "/output-metadata-json";

    // 保存APK的文件名
    private static final String saveFileName = "my.apk";
    private static File apkFile;

    // 下载线程
    private Thread downLoadThread;
    private int progress;// 当前进度
    // 应用程序Context
    private Context mContext;
    // 是否是最新的应用,默认为false
    private boolean isNew = false;
    private boolean intercept = false;
    // 进度条与通知UI刷新的handler和msg常量
    private ProgressBar mProgress;
    private TextView txtStatus;

    private static final int DOWN_UPDATE = 1;
    private static final int DOWN_OVER = 2;
    private static final int SHOWDOWN = 3;

    public AutoUpdater(Context context) {
        mContext = context;
        apkFile = new File(mContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), saveFileName);
    }

    public void ShowUpdateDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);

        builder.setTitle("软件版本更新");
        builder.setMessage("有最新的软件包,请下载并安装!");
        builder.setPositiveButton("立即下载", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ShowDownloadDialog();
            }
        });
        //按钮隐藏,只能下载更新
//        builder.setNegativeButton("以后再说", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                dialog.dismiss();
//            }
//        });
        AlertDialog dialog = builder.create();
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
                Button btnPos = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
                Button btnNeg = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
                if (btnPos != null) {
                    btnPos.setTextColor(Color.WHITE);
                }
                if (btnNeg != null) {
                    btnNeg.setTextColor(Color.WHITE);
                }
            }
        });
        // 设置点击空白区域不关闭对话框
        dialog.setCanceledOnTouchOutside(false);
        //这里设置点击返回按钮对话框不消失
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent keyEvent) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    // 拦截返回按钮事件,不做任何操作
                    return true;
                }
                return false;
            }
        });
        dialog.show();
    }

    private void ShowDownloadDialog() {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
        dialogBuilder.setTitle("软件版本更新");
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View v = inflater.inflate(R.layout.progress, null);
        mProgress = (ProgressBar) v.findViewById(R.id.progress);
        txtStatus = v.findViewById(R.id.txtStatus);
        dialogBuilder.setView(v);
        dialogBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                intercept = true;
            }
        });

        AlertDialog dialog = dialogBuilder.create();
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
                Button btnNeg = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
                if (btnNeg != null) {
                    btnNeg.setTextColor(Color.WHITE);
                }
            }
        });
        // 设置点击空白区域不关闭对话框
        dialog.setCanceledOnTouchOutside(false);
        //这里设置点击返回按钮对话框不消失
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent keyEvent) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    // 拦截返回按钮事件,不做任何操作
                    return true;
                }
                return false;
            }
        });

        dialog.show();
        DownloadApk();
    }
    /**
     * 检查是否更新的内容
     */
    public void CheckUpdate() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                String localVersion = "1";
                try {
                    localVersion = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0).versionName;
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
                String versionName = "1";
                String outputFile = "";
                String config = doGet(checkUrl);
                if (config != null && config.length() > 0) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        Matcher m = Pattern.compile("\"outputFile\":\\s*\"(?<m>[^\"]*?)\"").matcher(config);

                        if (m.find()) {
                            outputFile = m.group("m");
                        }
                        m = Pattern.compile("\"versionName\":\\s*\"(?<m>[^\"]*?)\"").matcher(config);
                        if (m.find()) {
                            String v = m.group("m");
                            versionName = m.group("m").replace("v1.0.", "");
                        }
                    }
                }

                if (localVersion.contains(".")) {
                    localVersion = localVersion.substring(0, localVersion.indexOf("."));
                }
                if (versionName.contains(".")) {
                    versionName = versionName.substring(0, versionName.indexOf("."));
                }

                //测试环境 大于号 正式上线要改成小于号,<<<<<<<<<<<<<<<<<<<<<<
                if (Long.parseLong(localVersion) > Long.parseLong(versionName)) {
//                    apkUrl = apkUrl + outputFile;
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            System.out.println("版本升级开始自动更新--------------------" + apkUrl);
                            mHandler.sendEmptyMessage(SHOWDOWN);
                        }
                    });
                } else {
                    // 当前版本已是最新版本
                    return;
                }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 从服务器下载APK安装包
     */
    public void DownloadApk() {
        downLoadThread = new Thread(DownApkWork);
        downLoadThread.start();
    }

    private Runnable DownApkWork = new Runnable() {
        @Override
        public void run() {
            URL url;
            try {
                url = new URL(apkUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.connect();
                int length = conn.getContentLength();
                InputStream ins = conn.getInputStream();
                FileOutputStream fos = new FileOutputStream(apkFile);
                int count = 0;
                byte[] buf = new byte[1024];
                while (!intercept) {
                    int numread = ins.read(buf);
                    count += numread;
                    progress = (int) (((float) count / length) * 100);
                    // 下载进度
                    mHandler.sendEmptyMessage(DOWN_UPDATE);
                    if (numread <= 0) {
                        // 下载完成通知安装
                        mHandler.sendEmptyMessage(DOWN_OVER);
                        break;
                    }
                    fos.write(buf, 0, numread);
                }
                fos.close();
                ins.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    /**
     * 安装APK内容
     */
    public void installAPK() {
        try {
            if (!apkFile.exists()) {
                return;
            }

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//安装完成后打开新版本
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // 给目标应用一个临时授权
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//判断版本大于等于7.0
                //如果SDK版本>=24,即:Build.VERSION.SDK_INT >= 24,使用FileProvider兼容安装apk
                String packageName = mContext.getApplicationContext().getPackageName();
                String authority = new StringBuilder(packageName).append(".fileprovider").toString();
                Uri apkUri = FileProvider.getUriForFile(mContext, authority, apkFile);
                intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
            } else {
              intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
            }

            // 添加权限请求
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            mContext.startActivity(intent);
            android.os.Process.killProcess(android.os.Process.myPid());//安装完之后会提示"完成" "打开"。


        } catch (Exception e) {
            e.printStackTrace();
            // 添加异常处理,打印异常信息或者显示Toast提示
             Toast.makeText(mContext, "安装APK出错", Toast.LENGTH_SHORT).show();
        }
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case SHOWDOWN:
                    ShowUpdateDialog();
                    break;
                case DOWN_UPDATE:
                    txtStatus.setText(progress + "%");
                    mProgress.setProgress(progress);
                    break;
                case DOWN_OVER:
                    Toast.makeText(mContext, "下载完毕", Toast.LENGTH_SHORT).show();
                    installAPK();
                    break;
                default:
                    break;
            }
        }

    };

    public static String doGet(String httpurl) {
        HttpURLConnection connection = null;
        InputStream is = null;
        BufferedReader br = null;
        String result = null;
        try {
            URL url = new URL(httpurl);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(15000);
            connection.setReadTimeout(60000);
            connection.connect();
            if (connection.getResponseCode() == 200) {
                is = connection.getInputStream();
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                StringBuffer sbf = new StringBuffer();
                String temp = null;
                while ((temp = br.readLine()) != null) {
                    sbf.append(temp);
                    sbf.append("\r\n");
                }
                result = sbf.toString();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            connection.disconnect();
        }
        return result;
    }
}
复制代码
六:MainActivity.java
XML 复制代码
 /**
     * app检查更新
     */
    private void appUpdata(){
        //检查更新
        try {
            //6.0才用动态权限
            if (Build.VERSION.SDK_INT >= 23) {
                String[] permissions = {
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.ACCESS_WIFI_STATE,
                        Manifest.permission.INTERNET};
                List<String> permissionList = new ArrayList<>();
                for (int i = 0; i < permissions.length; i++) {
                    if (ActivityCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
                        permissionList.add(permissions[i]);
                    }
                }
                if (permissionList.size() <= 0) {
                    //说明权限都已经通过,可以做你想做的事情去
                    //自动更新
                    AutoUpdater manager = new AutoUpdater(MainActivity.this);
                    manager.CheckUpdate();
                } else {
                    //存在未允许的权限
                    ActivityCompat.requestPermissions(this, permissions, 100);
                }
            }
        } catch (Exception ex) {
            Toast.makeText(MainActivity.this, "自动更新异常:" + ex.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        boolean haspermission = false;
        if (100 == requestCode) {
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[i] == -1) {
                    haspermission = true;
                }
            }
            if (haspermission) {
                //跳转到系统设置权限页面,或者直接关闭页面,不让他继续访问
                permissionDialog();
            } else {
                //全部权限通过,可以进行下一步操作
                AutoUpdater manager = new AutoUpdater(MainActivity.this);
                manager.CheckUpdate();
            }
        }
    }

    AlertDialog alertDialog;
    //打开手动设置应用权限
    private void permissionDialog() {
        if (alertDialog == null) {
            alertDialog = new AlertDialog.Builder(this)
                    .setTitle("提示信息")
                    .setMessage("当前应用缺少必要权限,该功能暂时无法使用。如若需要,请单击【确定】按钮前往设置中心进行权限授权。")
                    .setPositiveButton("设置", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            cancelPermissionDialog();
                            Uri packageURI = Uri.parse("package:" + getPackageName());
                            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                            startActivity(intent);
                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            cancelPermissionDialog();
                        }
                    })
                    .create();
        }
        alertDialog.show();
    }

    private void cancelPermissionDialog() {
        alertDialog.cancel();
    }

    private static final long HEARTBEAT_INTERVAL = 60 * 60 * 1000; // 1小时的毫秒数
//    private static final long HEARTBEAT_INTERVAL = 1 * 60 * 1000; // 每隔一分钟的毫秒数

    private Handler heartbeatHandler = new Handler();
    private Runnable heartbeatRunnable = new Runnable() {
        @Override
        public void run() {
            appUpdata(); // 调用appUpdata()方法
            heartbeatHandler.postDelayed(this, HEARTBEAT_INTERVAL); // 每隔1小时再次执行
        }
    };
    private void startHeartbeat() {
        heartbeatHandler.postDelayed(heartbeatRunnable, HEARTBEAT_INTERVAL);
    }

    private void stopHeartbeat() {
        heartbeatHandler.removeCallbacks(heartbeatRunnable);
    }



}

然后在creat里面调用心跳

 @Override
    protected void onCreate(Bundle savedInstanceState) {
       //其他代码省略 
        startHeartbeat();

    }

这里要说一下,打包的时候可以根据当前时间设置,修改build.gradle文件

plugins {
    id 'com.android.application'
    //添加如下配置
    id 'com.huawei.agconnect'
}
apply plugin: 'com.huawei.agconnect'

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.shuye.znsy"
        minSdk 21
        targetSdk 31
        versionCode 1
//        versionName "1.0"
        versionName "${releaseTime()}"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }


    

    buildTypes {
        debug {
            signingConfig signingConfigs.debug
            debuggable true
            minifyEnabled false

           
            buildConfigField("String", "host", "\"http://192.168.100.111:8001/\"")

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            android.applicationVariants.all { variant ->
                variant.outputs.all {
                    outputFileName = "my_${releaseTime()}.apk"
                }
            }

        }
        release {
            minifyEnabled false
            shrinkResources false
            zipAlignEnabled true
            jniDebuggable false
            debuggable false

         
            buildConfigField("String", "host", "\"http://192.168.100.111:8001/\"")


            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            android.applicationVariants.all { variant ->
                variant.outputs.all {
                    outputFileName = "my_${releaseTime()}.apk"
                }
            }
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
//    implementation 'androidx.media3:media3-common:+'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    implementation 'com.github.Jasonchenlijian:FastBle:2.4.0'


    implementation 'com.huawei.hms:scanplus:2.12.0.301'

    //数据解析相关
    implementation 'com.alibaba:fastjson:1.2.37'
    implementation 'io.reactivex:rxjava:1.3.0'
    implementation 'io.reactivex:rxandroid:1.2.1'
    implementation 'com.google.code.gson:gson:2.8.6'
    implementation 'com.orhanobut:logger:2.1.1'
    //网络框架
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
    implementation 'io.reactivex.rxjava2:rxjava:2.1.16'

    implementation 'com.squareup.okhttp3:okhttp:3.11.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.8.1'
    //eventbus
    implementation 'org.greenrobot:eventbus:3.0.0'


}
def releaseTime() {
    return new Date().format("yyyyMMddHHmmss", TimeZone.getTimeZone("UTC"))
}

打包后得到的文件

以上是app内所要修改或添加全部代码,注意这里需要配合后端下载文件联合使用,考虑到这里遂上一下后端代码,每个人情况不一样,仅供参考

我这里是springboot框架,java语言

一个为下载文件apk的方法,另外一个读取output-metadata.json反馈到app进行版本解析比较

注意这里我后端已经有了文件上传的方法,我是将apk和output-metadata.json一起上传到了服务器上面

package com.cz.hospital.base.controller.pad;

import com.cz.hospital.base.service.IPackageUploadService;
import com.cz.hospital.base.service.IUserService;
import com.cz.hospital.entity.attachment.AttachmentEntity;
import com.cz.hospital.entity.packageupload.vo.PackageUploadVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/app/upload")
@Api(tags = "app安装包下载管理接口")
public class AppUpdateController {
    @Resource
    private IPackageUploadService packageUploadService;
    @Resource
    private IUserService userService;
    @RequestMapping("/download-apk")
    public ResponseEntity<org.springframework.core.io.Resource> downloadNewestFile() throws IOException {
        PackageUploadVo vo = packageUploadService.findNewestList();
        List<AttachmentEntity> attachmentList = vo.getAttachmentList();
        AttachmentEntity file = new AttachmentEntity();
        if( attachmentList.size()>0 ){
            if( attachmentList.size()>0 ){
                for( AttachmentEntity aeF : attachmentList ){
                    if( ".apk".equals( aeF.getFileExtension() ) ){
                        file = aeF;
                    }
                }
            }
        }
        String filePath = file.getFileStoragePath();

        String fileName = file.getFileName();
        // 创建文件资源对象
        Path path = Paths.get(filePath);
        org.springframework.core.io.Resource resource = new UrlResource(path.toUri());
        // 设置响应头
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" + URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString()));
        headers.set(HttpHeaders.CONTENT_ENCODING, "UTF-8");
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" + URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString()));
        // 返回文件内容
        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(resource.contentLength())
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(resource);
    }
    @RequestMapping("/download-apk/output-metadata-json")
    public String readJson() throws IOException {
        PackageUploadVo vo = packageUploadService.findNewestList();
        List<AttachmentEntity> attachmentList = vo.getAttachmentList();
        AttachmentEntity file = new AttachmentEntity();
        if( attachmentList.size()>0 ){
            if( attachmentList.size()>0 ){
                for( AttachmentEntity aeF : attachmentList ){
                    if( ".json".equals( aeF.getFileExtension() ) ){
                        file = aeF;
                    }
                }
            }
        }

        // 读取output-metadata.json文件
        String filePath = file.getFileStoragePath();
        String fileName = file.getFileName();
        // 创建文件资源对象
        Path path = Paths.get(filePath);
        org.springframework.core.io.Resource resource = new UrlResource(path.toUri());
        InputStream inputStream = resource.getInputStream();
        byte[] jsonBytes = FileCopyUtils.copyToByteArray(inputStream);
        String jsonContent = new String(jsonBytes, StandardCharsets.UTF_8);
        // 返回文件内容
        return jsonContent;
    }


}

至此结束,有问题欢迎大家随时交流

相关推荐
江上清风山间明月3 分钟前
flutter bottomSheet 控件详解
android·flutter·底部导航·bottomsheet
Crossoads2 小时前
【汇编语言】外中断(一)—— 外中断的魔法:PC机键盘如何触发计算机响应
android·开发语言·数据库·深度学习·机器学习·计算机外设·汇编语言
sunphp开发者3 小时前
黑客攻击网站,篡改首页问题排查修复
android·js
我又来搬代码了3 小时前
【Android Studio】创建新项目遇到的一些问题
android·ide·android studio
ggs_and_ddu7 小时前
Android--java实现手机亮度控制
android·java·智能手机
zhangphil13 小时前
Android绘图Path基于LinearGradient线性动画渐变,Kotlin(2)
android·kotlin
watl013 小时前
【Android】unzip aar删除冲突classes再zip
android·linux·运维
键盘上的蚂蚁-13 小时前
PHP爬虫类的并发与多线程处理技巧
android
喜欢猪猪14 小时前
Java技术专家视角解读:SQL优化与批处理在大数据处理中的应用及原理
android·python·adb
JasonYin~16 小时前
HarmonyOS NEXT 实战之元服务:静态案例效果---手机查看电量
android·华为·harmonyos