oss存储

<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
导入依赖

  1. package com.zax.appmanage.util;
  2. import com.aliyun.oss.ClientException;
  3. import com.aliyun.oss.OSS;
  4. import com.aliyun.oss.OSSClientBuilder;
  5. import com.aliyun.oss.OSSException;
  6. import com.aliyun.oss.model.CannedAccessControlList;
  7. import com.aliyun.oss.model.GetObjectRequest;
  8. import java.io.ByteArrayInputStream;
  9. import java.io.File;
  10. /**
  11. * @author HMB-XS
  12. * @date 2022年07月03日 12:01:26
  13. * 阿里云OSS工具类
  14. * 属性必须配置否则出错,第一次使用先初始化存储桶
  15. * 目前只提供了上传和下删除的方法
  16. * 如果要修改文件只需要重新上传同名文件即可,新文件覆盖旧文件
  17. * 使用该工具类需要在pom.xml中添加依赖:
  18. *
  19. * <dependency>
  20. * <groupId>com.aliyun.oss</groupId>
  21. * <artifactId>aliyun-sdk-oss</artifactId>
  22. * <version>3.10.2</version>
  23. *</dependency>
  24. **/
  25. public class OssAliyunUtil {
  26. /**Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。*/
  27. private static String endpoint = "oss-cn-hangzhou.aliyuncs.com";
  28. /**阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,
  29. 请登录RAM控制台创建RAM用户。*/
  30. private static final String accessKeyId = "LTAI5tAxfhT9VuogVAq1JkTu";
  31. private static final String accessKeySecret = "OQxDr5xZXnjQlqHXC8MT8vKi8xaUfV";
  32. /**填写Bucket名称,例如examplebucket。*/
  33. private static final String bucketName = "zax2517";
  34. /**创建OSSClient实例。*/
  35. private static OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
  36. //文件地址
  37. private static final String filePath="https://zax2517.oss-cn-hangzhou.aliyuncs.com/";
  38. /**
  39. * 创建存储桶初始化(只需要执行一次,请不要重复执行)
  40. */
  41. public static void newBucket(){
  42. try {
  43. // 创建存储空间。
  44. ossClient.createBucket(bucketName);
  45. // 设置存储空间的读写权限。例如将examplebucket的读写权限ACL设置为公开读PublicRead
  46. ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
  47. } catch (OSSException oe) {
  48. System.out.println("Caught an OSSException, which means your request made it to OSS, "
  49. + "but was rejected with an error response for some reason.");
  50. System.out.println("Error Message:" + oe.getErrorMessage());
  51. System.out.println("Error Code:" + oe.getErrorCode());
  52. System.out.println("Request ID:" + oe.getRequestId());
  53. System.out.println("Host ID:" + oe.getHostId());
  54. } catch (ClientException ce) {
  55. System.out.println("Caught an ClientException, which means the client encountered "
  56. + "a serious internal problem while trying to communicate with OSS, "
  57. + "such as not being able to access the network.");
  58. System.out.println("Error Message:" + ce.getMessage());
  59. }
  60. }
  61. /**
  62. * 上传文件功能
  63. * @param objectName 文件全名称,包含文件后缀
  64. * @param file 文件对象Byte[]
  65. */
  66. public static void uploadFile(String objectName,byte[] file){
  67. try {
  68. ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(file));
  69. } catch (OSSException oe) {
  70. System.out.println("Caught an OSSException, which means your request made it to OSS, "
  71. + "but was rejected with an error response for some reason.");
  72. System.out.println("Error Message:" + oe.getErrorMessage());
  73. System.out.println("Error Code:" + oe.getErrorCode());
  74. System.out.println("Request ID:" + oe.getRequestId());
  75. System.out.println("Host ID:" + oe.getHostId());
  76. } catch (ClientException ce) {
  77. System.out.println("Caught an ClientException, which means the client encountered "
  78. + "a serious internal problem while trying to communicate with OSS, "
  79. + "such as not being able to access the network.");
  80. System.out.println("Error Message:" + ce.getMessage());
  81. }
  82. }
  83. /**
  84. * 文件下载
  85. * 参数一:objectName:文件在bucket中的位置
  86. * 参数二:pathName:下载到本地的保存路径
  87. * File file(pathName)
  88. */
  89. public static void exportOssFile(String objectName){
  90. try {
  91. // 下载Object到本地文件,并保存到指定的本地路径中。如果指定的本地文件存在会覆盖,不存在则新建。
  92. // 如果未指定本地路径,则下载后的文件默认保存到示例程序所属项目对应本地路径中。
  93. ossClient.getObject(new GetObjectRequest(bucketName, objectName));
  94. } catch (OSSException oe) {
  95. System.out.println("Caught an OSSException, which means your request made it to OSS, "
  96. + "but was rejected with an error response for some reason.");
  97. System.out.println("Error Message:" + oe.getErrorMessage());
  98. System.out.println("Error Code:" + oe.getErrorCode());
  99. System.out.println("Request ID:" + oe.getRequestId());
  100. System.out.println("Host ID:" + oe.getHostId());
  101. } catch (ClientException ce) {
  102. System.out.println("Caught an ClientException, which means the client encountered "
  103. + "a serious internal problem while trying to communicate with OSS, "
  104. + "such as not being able to access the network.");
  105. System.out.println("Error Message:" + ce.getMessage());
  106. }
  107. }
  108. /**
  109. * 文件删除功能
  110. * @param objectName 文件全名称,包含文件后缀
  111. */
  112. public static void deleteFile(String objectName){
  113. try {
  114. // 删除文件。
  115. ossClient.deleteObject(bucketName, objectName);
  116. } catch (OSSException oe) {
  117. System.out.println("Caught an OSSException, which means your request made it to OSS, "
  118. + "but was rejected with an error response for some reason.");
  119. System.out.println("Error Message:" + oe.getErrorMessage());
  120. System.out.println("Error Code:" + oe.getErrorCode());
  121. System.out.println("Request ID:" + oe.getRequestId());
  122. System.out.println("Host ID:" + oe.getHostId());
  123. } catch (ClientException ce) {
  124. System.out.println("Caught an ClientException, which means the client encountered "
  125. + "a serious internal problem while trying to communicate with OSS, "
  126. + "such as not being able to access the network.");
  127. System.out.println("Error Message:" + ce.getMessage());
  128. }
  129. }
  130. /**
  131. * 断开连接池,关机
  132. */
  133. public static void shutdown(){
  134. if (ossClient != null) {
  135. ossClient.shutdown();
  136. }
  137. }
  138. /**
  139. * 获取地域信息
  140. * @return String
  141. */
  142. public static String getEndpoint() {
  143. return endpoint;
  144. }
  145. /**
  146. * 获取存储桶信息
  147. * @return String
  148. */
  149. public static String getBucketName() {
  150. return bucketName;
  151. }
  152. public static String getFilePath(){
  153. return filePath;
  154. }
  155. }

上传功能

  1. @Transactional
  2. @RequestMapping("/Add_version")
  3. public String addVersion(AppVersion appVersion, MultipartFile photo){
  4. System.out.println(appVersion.getInfoId());
  5. String filename = photo.getOriginalFilename();
  6. appVersion.setApkFileName(filename);
  7. if (appVersion.getVersionNo()==null||appVersion.getVersionSize()==null){
  8. return "/";
  9. }
  10. if (!StringUtils.isBlank(filename)) {
  11. //获取上传的文件名
  12. //截取文件后缀名
  13. String hzm = filename.substring(filename.lastIndexOf("."));
  14. //根据时间戳生成的唯一文件名
  15. String finalName = System.currentTimeMillis() + hzm;
  16. try {
  17. OssAliyunUtil.uploadFile(finalName, photo.getBytes());
  18. appVersion.setDownloadLink(OssAliyunUtil.getFilePath()+finalName);
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. }
  23. int i1 = appVersionService.insertVersion(appVersion);
  24. int i = iAppInfoService.updateVersion(appVersion.getInfoId());
  25. return "redirect:/index";
  26. }

分页导航栏

  1. //新建一个数组,用来存放分页导航栏
  2. List<Integer> pages = new ArrayList<>();
  3. //将当前页放入数组
  4. pages.add(pageNum);//pageNum为当前页
  5. for (int i = 1; i <= 5; i++) {//当数据里的数只有1-3个
  6. //左边的元素
  7. if (pageNum - i > 0) {
  8. pages.add(0, pageNum - i);
  9. }
  10. //右边的元素
  11. if (pageNum + i <= page.getPages()) {// questions.getPages()从MybatisPlus分页插件中查出分页条数
  12. pages.add(pageNum + i);
  13. }
  14. }