shicf
2025-04-24 23d0d2bd37cc8789b35f60f2c6d6e0727c284c7b
移动端app自动升级,安装包下载 ,版本检测
已修改2个文件
173 ■■■■■ 文件已修改
src/main/java/com/product/file/controller/FileManagerController.java 35 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/product/file/service/FileManagerService.java 138 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/product/file/controller/FileManagerController.java
@@ -274,7 +274,40 @@
            return error(FileCode.UPLOAD_FILE_FAIL.getValue(), FileCode.UPLOAD_FILE_FAIL.getText());
        }
    }
    /**
     * 移动端升级下载安装文件
     * 此接口需要加入白名单
     *
     * @return 结果
     */
    @RequestMapping(value = "/upgrade-download/{version}", method = RequestMethod.GET)
    @ApiVersion(1)
    public String getAppFileContents(@RequestParam("uuid") String uuid,HttpServletRequest request, HttpServletResponse response) {
        try {
            FieldSetEntity fse = null;
            Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
            RequestParameterEntity rpe = (RequestParameterEntity) bean;
            if (null != rpe && null != rpe.getFormData()) {
                fse = rpe.getFormData();
            }
            if (fse == null) {
                fse=new FieldSetEntity();
                fse.setTableName("product_sys_attachments");
                fse.setValue("uuid",uuid);
                fse.setValue("needOnlineView", 0);
                SpringMVCContextHolder.getSystemLogger().error(FileCode.FORM_NODATA.getValue(), FileCode.FORM_NODATA.getText());
//                return this.error(FileCode.FORM_NODATA.getValue(), FileCode.FORM_NODATA.getText());
            }
            SpringMVCContextHolder.getSystemLogger().info("通过GET请求获取附件内容");
            // 正式代码
            fileManagerService.getFileContent(fse, response ,true);
            return OK();
        } catch (Exception e) {
            SpringMVCContextHolder.getSystemLogger().error(e);
            e.printStackTrace();
            return error(FileCode.GET_FILE_CONTENT_FAIL.getValue(), FileCode.GET_FILE_CONTENT_FAIL.getText());
        }
    }
    /**
     * 下载文件或者在线预览
     *
src/main/java/com/product/file/service/FileManagerService.java
@@ -445,7 +445,145 @@
            }
        }
    }
    /**
     * 移动端升级,安装包下载
     *
     * @param fse
     * @param response
     */
    public void getFileContent(FieldSetEntity fse, HttpServletResponse response,boolean isUpgrade) throws IOException {
        if(isUpgrade) {
            FieldSetEntity attachmentFse = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, fse.getUUID(), false);
            if (attachmentFse == null || !"product_sys_app_version".equals(attachmentFse.getString("attachment_data_table")) ) {
                return;
            }
            logger.info("正在获取文件流...");
            boolean needDownloadFromServerFlag = "1".equals(attachmentFse.getString(CmnConst.UPLOAD_SIGN));
            String dir = attachmentFse.getString(CmnConst.ATTACHMENT_URL);
            String fileName = attachmentFse.getString(CmnConst.ATTACHMENT_TITLE);
            //真实文件名
            String realFileName = attachmentFse.getString(CmnConst.FILE_NAME);
            String viewOnlineSign = attachmentFse.getString(CmnConst.VIEW_ONLINE_SIGN);
            boolean encrptSignFlag = "1".equals(attachmentFse.getString(CmnConst.ENCRPT_SIGN));
            boolean needOnlineViewFlag = "1".equals(fse.getString(CmnConst.NEED_ONLINE_VIEW)) && "1".equals(viewOnlineSign);
            String clientType =CoreConst.CLIENT_TYPE_APP;
            String contentType = "multipart/form-data";
            boolean isExcel = realFileName.endsWith(".xlsx") || realFileName.endsWith(".xls");
            if (needOnlineViewFlag) {
                //特殊处理: 如果客户端不是App 但预览的文件是Excel 直接返回excel源文件 content头标识是excel cheng 2025年2月11日10:41:41
                if (isExcel && !CoreConst.CLIENT_TYPE_APP.equals(clientType)) {
                    contentType = "application/vnd.ms-excel";
                } else {
                    dir += File.separator + CmnConst.TRANSFER_DIR_NAME;
                }
            }
            String path = dir + File.separator + fileName;
            if (needDownloadFromServerFlag) {
                // 需要从附件服务器上取文件
                FTPService ftpService = new FTPService();
                logger.info("需要从附件服务器上取文件...");
                response.setHeader("Access-Control-Expose-Headers", "*");
                response.setContentType(contentType);
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(realFileName, "UTF-8"));
                //设置头中的文件类型
                if (!needOnlineViewFlag && !encrptSignFlag) {
                    logger.info("不需要解密且获取原文件...");
                    try (ServletOutputStream os = response.getOutputStream()) {
                        ftpService.downloadFile(path, os);
                    }
                } else {
                    logger.info("需要解密或者获取转换后的文件...");
                    String localTempPath = Global.getSystemConfig("temp.dir", "") + File.separator + fileName;
                    File localTempFile = new File(localTempPath);
                    OutputStream tempOs = new FileOutputStream(localTempFile);
                    ftpService.downloadFile(path, tempOs);
                    tempOs.flush();
                    tempOs.close();
                    if (needOnlineViewFlag && localTempFile.length() == 0L) {
                        // 若是预览,但是转换后的文件不存在,那么重新执行转换操作
                        File sourceFile = fileUtils.getFile(true, attachmentFse.getString(CmnConst.ATTACHMENT_URL), fileName, encrptSignFlag);
                        File tempSourceFile = new File(sourceFile.getParentFile().getAbsolutePath() + File.separator + attachmentFse.getString(CmnConst.FILE_NAME));
                        sourceFile.renameTo(tempSourceFile);
                        FileUtils.convertPdf(false, true, encrptSignFlag, true, tempSourceFile, dir, fileName, attachmentFse.getString(CmnConst.FILE_NAME));
                        tempSourceFile.delete();
                        // 重新拉文件
                        tempOs = new FileOutputStream(localTempFile);
                        ftpService = new FTPService();
                        ftpService.downloadFile(path, tempOs);
                        tempOs.flush();
                        tempOs.close();
                    }
                    response.setContentLengthLong(localTempFile.length());
                    InputStream is = new FileInputStream(localTempFile);
                    int len;
                    byte[] b = new byte[1024];
                    try (ServletOutputStream os = response.getOutputStream()) {
                        while ((len = is.read(b)) > 0) {
                            os.write(encrptSignFlag ? FileUtil.decryption(b) : b, 0, len);
                        }
                        os.flush();
                        is.close();
                    }
                    // 删除临时文件
                    localTempFile.delete();
                }
            } else {
                // 直接在本地的目录中找文件
                logger.info("直接在本地的目录中找文件...");
                String localBasePath = Global.getSystemConfig("local.dir", "");
                path = localBasePath + File.separator + path;
                File file = new File(path);
                if (needOnlineViewFlag && file.exists() && CoreConst.CLIENT_TYPE_APP.equals(clientType)) {
                    //特殊处理: 是App 但预览的文件是Excel 需要将之前已转换为的文件删除掉重新转换(因转换后的格式不是pdf)
                    String changeTime = "2025-02-11 23:59:59";
                    Date changeDate = DateUtil.parse(changeTime, "yyyy-MM-dd HH:mm:ss");
                    long fileTime = file.lastModified();
                    //如果时间是 2025年2月10日23:59:59,那么就重新转换
                    //删除文件
                    if (fileTime <= changeDate.getTime()) {
                        file.delete();
                    }
                }
                if (needOnlineViewFlag && !file.exists()) {
                    // 若是预览,但是转换后的文件不存在,那么重新执行转换操作
                    File sourceFile = fileUtils.getFile(false, attachmentFse.getString(CmnConst.ATTACHMENT_URL), fileName, encrptSignFlag);
                    File tempSourceFile = new File(sourceFile.getParentFile().getAbsolutePath() + File.separator + attachmentFse.getString(CmnConst.FILE_NAME));
                    sourceFile.renameTo(tempSourceFile);
                    FileUtils.convertPdf(false, false, encrptSignFlag, true, tempSourceFile, dir, fileName, attachmentFse.getString(CmnConst.FILE_NAME));
                    tempSourceFile.delete();
                }
                int len;
                byte[] b = new byte[1024];
                InputStream is = new FileInputStream(file);
                response.setHeader("Access-Control-Expose-Headers", "*");
                response.setContentType(contentType);
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(realFileName, "UTF-8"));
                response.setContentLengthLong(file.length());
                try (ServletOutputStream os = response.getOutputStream()) {
                    while ((len = is.read(b)) > 0) {
                        if (encrptSignFlag) {
                            // 需要解密
                            logger.info("需要解密...");
                            os.write(FileUtil.decryption(b), 0, len);
                        } else {
                            // 无需解密
                            os.write(b, 0, len);
                        }
                    }
                    os.flush();
                }
                is.close();
            }
            logger.info("文件流获取成功");
        }
    }
    /**
     * 提取文件信息-下载文件或者在线预览文件
     *