diff --git a/dl_admin/ruoyi-admin/src/main/java/com/ruoyi/base/controller/WebController.java b/dl_admin/ruoyi-admin/src/main/java/com/ruoyi/base/controller/WebController.java index 6f81c91..7a22d2e 100644 --- a/dl_admin/ruoyi-admin/src/main/java/com/ruoyi/base/controller/WebController.java +++ b/dl_admin/ruoyi-admin/src/main/java/com/ruoyi/base/controller/WebController.java @@ -21,10 +21,12 @@ import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.R; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.system.service.ISysDictDataService; +import com.ruoyi.utils.OssUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; +import lombok.SneakyThrows; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import springfox.documentation.annotations.ApiIgnore; @@ -75,6 +77,8 @@ public class WebController extends BaseController { private IBaseAppService appService; @Autowired private GoogleConfig googleConfig; + @Autowired + private OssUtil ossUtil; /** * 导航栏接口--所有分类 @@ -474,46 +478,17 @@ public class WebController extends BaseController { return R.ok(appService.selectNewApp()); } + @SneakyThrows @ApiOperation("下载APK文件") @GetMapping("/downloadApk") - public void downloadApk(HttpServletResponse response) { - try { - // APK文件路径 - File file = new File(googleConfig.getAppDownload()); - - // 检查文件是否存在 - if (!file.exists()) { - response.setStatus(HttpServletResponse.SC_NOT_FOUND); - response.getWriter().write("文件不存在"); - return; - } - - // 设置响应头 - response.setContentType("application/vnd.android.package-archive"); - response.setHeader("Content-Disposition", "attachment; filename=truck.apk"); - response.setHeader("Content-Length", String.valueOf(file.length())); - - // 写入响应流 - FileInputStream fis = new FileInputStream(file); - OutputStream os = response.getOutputStream(); - - byte[] buffer = new byte[1024]; - int bytesRead; - while ((bytesRead = fis.read(buffer)) != -1) { - os.write(buffer, 0, bytesRead); - } - - fis.close(); - os.flush(); - os.close(); - } catch (Exception e) { - try { - response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - response.getWriter().write("文件下载失败: " + e.getMessage()); - } catch (Exception ex) { - logger.error("下载APK文件时发生错误", ex); - } + public void downloadApk(HttpServletResponse response,@RequestParam(required = false) String id) { + BaseApp appVersion; + if(StringUtils.isNotEmpty(id)){ + appVersion = appService.getById(id); + }else{ + appVersion = appService.selectNewApp(); } + ossUtil.downloadFile(response, appVersion.getApkUrl(), appVersion.getVersion()); } /** diff --git a/dl_admin/ruoyi-admin/src/main/java/com/ruoyi/base/domain/BaseApp.java b/dl_admin/ruoyi-admin/src/main/java/com/ruoyi/base/domain/BaseApp.java index 4483bc7..f630b26 100644 --- a/dl_admin/ruoyi-admin/src/main/java/com/ruoyi/base/domain/BaseApp.java +++ b/dl_admin/ruoyi-admin/src/main/java/com/ruoyi/base/domain/BaseApp.java @@ -33,6 +33,10 @@ public class BaseApp extends DlBaseEntity @Excel(name = "版本") private String version; + /** app包下载地址 */ + @Excel(name = "app包下载地址") + private String apkUrl; + /** 本次升级描述 */ @Excel(name = "本次升级描述") private String content; diff --git a/dl_admin/ruoyi-admin/src/main/java/com/ruoyi/utils/OssUtil.java b/dl_admin/ruoyi-admin/src/main/java/com/ruoyi/utils/OssUtil.java new file mode 100644 index 0000000..c27b156 --- /dev/null +++ b/dl_admin/ruoyi-admin/src/main/java/com/ruoyi/utils/OssUtil.java @@ -0,0 +1,82 @@ +package com.ruoyi.utils; + +import com.aliyun.oss.ClientBuilderConfiguration; +import com.aliyun.oss.OSS; +import com.aliyun.oss.OSSClientBuilder; +import com.aliyuncs.utils.IOUtils; +import com.ruoyi.common.config.OssConfig; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import javax.servlet.http.HttpServletResponse; +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URLEncoder; + +@Slf4j +@Component +public class OssUtil { + @Autowired + private OssConfig ossConfig; + + /** + * 上传文件(fileKey-InputStream) + */ + public void uploadFile(String fileKey, InputStream stream) throws IOException { + ClientBuilderConfiguration conf = new ClientBuilderConfiguration(); + OSS client = new OSSClientBuilder().build(ossConfig.getEndPoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret(), conf); + + try { + client.putObject(ossConfig.getBucketName(), fileKey, stream); + } finally { + stream.close(); + if (client != null) { + client.shutdown(); + } + IOUtils.closeQuietly(stream); + } + } + + + /** + * 文件下载 + */ + public void downloadFile(HttpServletResponse response, String fileKey, String fileName) throws IOException { + fileKey = fileKey.replace("https://"+ossConfig.getBucketName()+"."+ossConfig.getEndPoint()+"/",""); + InputStream stream = null; + BufferedInputStream bufferedInputStream = null; + ClientBuilderConfiguration conf = new ClientBuilderConfiguration(); + OSS client = new OSSClientBuilder().build(ossConfig.getEndPoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret(), conf); + try { + stream = client.getObject(ossConfig.getBucketName(), fileKey).getObjectContent(); + response.reset(); + response.setContentType("application/octet-stream"); + response.addHeader("Content-Disposition", "attachment;filename=CDbay-" + URLEncoder.encode(fileName, "UTF-8") + .replace("+", "%20")+".apk"); + OutputStream outputStream = response.getOutputStream(); + bufferedInputStream = new BufferedInputStream(stream); + byte[] buf = new byte[16384]; + int bytesRead; + while ((bytesRead = stream.read(buf, 0, buf.length)) >= 0) { + outputStream.write(buf, 0, bytesRead); + outputStream.flush(); + } + } finally { + if (client != null) { + client.shutdown(); + } + + if (bufferedInputStream != null || stream != null) { + try { + bufferedInputStream.close(); + stream.close(); + } catch (IOException e) { + log.error("Oss Download IOException,fileKey:" + fileKey, e); + } + } + } + } +} diff --git a/dl_admin/ruoyi-admin/src/main/resources/mapper/base/BaseAppMapper.xml b/dl_admin/ruoyi-admin/src/main/resources/mapper/base/BaseAppMapper.xml index 560aa22..b53d06f 100644 --- a/dl_admin/ruoyi-admin/src/main/resources/mapper/base/BaseAppMapper.xml +++ b/dl_admin/ruoyi-admin/src/main/resources/mapper/base/BaseAppMapper.xml @@ -7,6 +7,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + @@ -16,7 +17,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - select id, version, content, creator, create_time, updater, update_time, del_flag from dl_base_app + select id, version, apk_url, content, creator, create_time, updater, update_time, del_flag from dl_base_app \ No newline at end of file diff --git a/dl_vue/src/components/Editor/index.vue b/dl_vue/src/components/Editor/index.vue index 78be4bd..e4dad58 100644 --- a/dl_vue/src/components/Editor/index.vue +++ b/dl_vue/src/components/Editor/index.vue @@ -1,46 +1,184 @@ - + diff --git a/dl_vue/src/components/ImageUpload/index.vue b/dl_vue/src/components/ImageUpload/index.vue index 836ca29..eb80f96 100644 --- a/dl_vue/src/components/ImageUpload/index.vue +++ b/dl_vue/src/components/ImageUpload/index.vue @@ -1,5 +1,25 @@