oss存储
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
导入依赖
package com.zax.appmanage.util;import com.aliyun.oss.ClientException;import com.aliyun.oss.OSS;import com.aliyun.oss.OSSClientBuilder;import com.aliyun.oss.OSSException;import com.aliyun.oss.model.CannedAccessControlList;import com.aliyun.oss.model.GetObjectRequest;import java.io.ByteArrayInputStream;import java.io.File;/*** @author HMB-XS* @date 2022年07月03日 12:01:26* 阿里云OSS工具类* 属性必须配置否则出错,第一次使用先初始化存储桶* 目前只提供了上传和下删除的方法* 如果要修改文件只需要重新上传同名文件即可,新文件覆盖旧文件* 使用该工具类需要在pom.xml中添加依赖:** <dependency>* <groupId>com.aliyun.oss</groupId>* <artifactId>aliyun-sdk-oss</artifactId>* <version>3.10.2</version>*</dependency>**/public class OssAliyunUtil {/**Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。*/private static String endpoint = "oss-cn-hangzhou.aliyuncs.com";/**阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。*/private static final String accessKeyId = "LTAI5tAxfhT9VuogVAq1JkTu";private static final String accessKeySecret = "OQxDr5xZXnjQlqHXC8MT8vKi8xaUfV";/**填写Bucket名称,例如examplebucket。*/private static final String bucketName = "zax2517";/**创建OSSClient实例。*/private static OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);//文件地址private static final String filePath="https://zax2517.oss-cn-hangzhou.aliyuncs.com/";/*** 创建存储桶初始化(只需要执行一次,请不要重复执行)*/public static void newBucket(){try {// 创建存储空间。ossClient.createBucket(bucketName);// 设置存储空间的读写权限。例如将examplebucket的读写权限ACL设置为公开读PublicReadossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);} catch (OSSException oe) {System.out.println("Caught an OSSException, which means your request made it to OSS, "+ "but was rejected with an error response for some reason.");System.out.println("Error Message:" + oe.getErrorMessage());System.out.println("Error Code:" + oe.getErrorCode());System.out.println("Request ID:" + oe.getRequestId());System.out.println("Host ID:" + oe.getHostId());} catch (ClientException ce) {System.out.println("Caught an ClientException, which means the client encountered "+ "a serious internal problem while trying to communicate with OSS, "+ "such as not being able to access the network.");System.out.println("Error Message:" + ce.getMessage());}}/*** 上传文件功能* @param objectName 文件全名称,包含文件后缀* @param file 文件对象Byte[]*/public static void uploadFile(String objectName,byte[] file){try {ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(file));} catch (OSSException oe) {System.out.println("Caught an OSSException, which means your request made it to OSS, "+ "but was rejected with an error response for some reason.");System.out.println("Error Message:" + oe.getErrorMessage());System.out.println("Error Code:" + oe.getErrorCode());System.out.println("Request ID:" + oe.getRequestId());System.out.println("Host ID:" + oe.getHostId());} catch (ClientException ce) {System.out.println("Caught an ClientException, which means the client encountered "+ "a serious internal problem while trying to communicate with OSS, "+ "such as not being able to access the network.");System.out.println("Error Message:" + ce.getMessage());}}/*** 文件下载* 参数一:objectName:文件在bucket中的位置* 参数二:pathName:下载到本地的保存路径* File file(pathName)*/public static void exportOssFile(String objectName){try {// 下载Object到本地文件,并保存到指定的本地路径中。如果指定的本地文件存在会覆盖,不存在则新建。// 如果未指定本地路径,则下载后的文件默认保存到示例程序所属项目对应本地路径中。ossClient.getObject(new GetObjectRequest(bucketName, objectName));} catch (OSSException oe) {System.out.println("Caught an OSSException, which means your request made it to OSS, "+ "but was rejected with an error response for some reason.");System.out.println("Error Message:" + oe.getErrorMessage());System.out.println("Error Code:" + oe.getErrorCode());System.out.println("Request ID:" + oe.getRequestId());System.out.println("Host ID:" + oe.getHostId());} catch (ClientException ce) {System.out.println("Caught an ClientException, which means the client encountered "+ "a serious internal problem while trying to communicate with OSS, "+ "such as not being able to access the network.");System.out.println("Error Message:" + ce.getMessage());}}/*** 文件删除功能* @param objectName 文件全名称,包含文件后缀*/public static void deleteFile(String objectName){try {// 删除文件。ossClient.deleteObject(bucketName, objectName);} catch (OSSException oe) {System.out.println("Caught an OSSException, which means your request made it to OSS, "+ "but was rejected with an error response for some reason.");System.out.println("Error Message:" + oe.getErrorMessage());System.out.println("Error Code:" + oe.getErrorCode());System.out.println("Request ID:" + oe.getRequestId());System.out.println("Host ID:" + oe.getHostId());} catch (ClientException ce) {System.out.println("Caught an ClientException, which means the client encountered "+ "a serious internal problem while trying to communicate with OSS, "+ "such as not being able to access the network.");System.out.println("Error Message:" + ce.getMessage());}}/*** 断开连接池,关机*/public static void shutdown(){if (ossClient != null) {ossClient.shutdown();}}/*** 获取地域信息* @return String*/public static String getEndpoint() {return endpoint;}/*** 获取存储桶信息* @return String*/public static String getBucketName() {return bucketName;}public static String getFilePath(){return filePath;}}
上传功能
@Transactional@RequestMapping("/Add_version")public String addVersion(AppVersion appVersion, MultipartFile photo){System.out.println(appVersion.getInfoId());String filename = photo.getOriginalFilename();appVersion.setApkFileName(filename);if (appVersion.getVersionNo()==null||appVersion.getVersionSize()==null){return "/";}if (!StringUtils.isBlank(filename)) {//获取上传的文件名//截取文件后缀名String hzm = filename.substring(filename.lastIndexOf("."));//根据时间戳生成的唯一文件名String finalName = System.currentTimeMillis() + hzm;try {OssAliyunUtil.uploadFile(finalName, photo.getBytes());appVersion.setDownloadLink(OssAliyunUtil.getFilePath()+finalName);} catch (Exception e) {e.printStackTrace();}}int i1 = appVersionService.insertVersion(appVersion);int i = iAppInfoService.updateVersion(appVersion.getInfoId());return "redirect:/index";}
分页导航栏
//新建一个数组,用来存放分页导航栏List<Integer> pages = new ArrayList<>();//将当前页放入数组pages.add(pageNum);//pageNum为当前页for (int i = 1; i <= 5; i++) {//当数据里的数只有1-3个//左边的元素if (pageNum - i > 0) {pages.add(0, pageNum - i);}//右边的元素if (pageNum + i <= page.getPages()) {// questions.getPages()从MybatisPlus分页插件中查出分页条数pages.add(pageNum + i);}}
