实现文件上传模块开发的流程

一、实现思路:
1、maven导包

<dependency>  
    <groupId>com.aliyun.oss</groupId>  
    <artifactId>aliyun-sdk-oss</artifactId>  
    <version>${aliyun.sdk.oss}</version>  
</dependency>  
  
<dependency>  
    <groupId>javax.xml.bind</groupId>  
    <artifactId>jaxb-api</artifactId>  
    <version>${jaxb-api}</version>  
</dependency>//具体依赖可在阿里云的SDK的java使用文档中获取


2、在yml文件中配置Oss的环境,然后直接复制粘贴 AliOssUtil和AliOssProperties
3、通过获取文件原始名,截取文件名,uuid防覆盖,构建新文件名称objectname
4、调用String filePath=AliOssUtil.update(file.getBytes(),objectname)
    return Result.success(filePath);

二、包含三个类(AliOssUtil,OssConfiguration, AliOssProperties)和一个yml配置:
(1)sky.commom.utiles(工具类,只有update方法用于上传文件)

```
package com.sky.utils;  
  
import com.aliyun.oss.ClientException;  
import com.aliyun.oss.OSS;  
import com.aliyun.oss.OSSClientBuilder;  
import com.aliyun.oss.OSSException;  
import lombok.AllArgsConstructor;  
import lombok.Data;  
import lombok.extern.slf4j.Slf4j;  
import java.io.ByteArrayInputStream;  
  
@Data  
@AllArgsConstructor //自动为类的所有属性生成有参构造
@Slf4j  
public class AliOssUtil {  
  
    private String endpoint;  
    private String accessKeyId;  
    private String accessKeySecret;  
    private String bucketName;  
  
    /**  
     * 文件上传  
     *  
     * @param bytes  
     * @param objectName  
     * @return  
     */    public String upload(byte[] bytes, String objectName) {  
  
        // 创建OSSClient实例。  
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);  
  
        try {  
            // 创建PutObject请求。  
            ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes));  
        } 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());  
        } finally {  
            if (ossClient != null) {  
                ossClient.shutdown();  
            }  
        }  
  
        //文件访问路径规则 https://BucketName.Endpoint/ObjectName        StringBuilder stringBuilder = new StringBuilder("https://");  
        stringBuilder  
                .append(bucketName)  
                .append(".")  
                .append(endpoint)  
                .append("/")  
                .append(objectName);  
  
        log.info("文件上传到:{}", stringBuilder.toString());  
  
        return stringBuilder.toString();  
    }  
}
```


(2)sky.setver.config.OssConfiguration(配置类,用于创建Oss对象并进行赋值)


package com.sky.config;  
  
import com.sky.entity.Employee;  
import com.sky.properties.AliOssProperties;  
import com.sky.utils.AliOssUtil;  
import lombok.extern.slf4j.Slf4j;  
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;  
import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.Configuration;  
/*配置类,用于创建0ss对象*/  
@Configuration //一个类中声明一个和多个 @Bean 标记的方法,并且这些方法被 Spring 容器管理用于生成 Bean 定义以及在运行时这些 Bean 的服务请求  
@Slf4j  
public class OssConfiguration {  
    @Bean  
    @ConditionalOnMissingBean    
    public AliOssUtil aliOssUtil(AliOssProperties aliOssProperties){  
         log.info("开始创建Oss文件存储对象{}",aliOssProperties);  
        AliOssUtil aliOssUtil = new AliOssUtil(aliOssProperties.getEndpoint(),  
                aliOssProperties.getAccessKeyId(),  
                aliOssProperties.getAccessKeySecret(),  
                aliOssProperties.getBucketName());  
        return aliOssUtil;  
    }  
}


(3)sky.commom.properties(Oss属性对象)
```

package com.sky.properties;  
  
import lombok.Data;  
import org.springframework.boot.context.properties.ConfigurationProperties;  
import org.springframework.stereotype.Component;  
  
@Component  
@ConfigurationProperties(prefix = "sky.alioss")//表示属性由对应的yml文件中的sky.alioss赋值  
@Data  
public class AliOssProperties {  
  
    private String endpoint;  
    private String accessKeyId;  
    private String accessKeySecret;  
    private String bucketName;  
  
}


```
(4)application.yml和application-dev.yml
```

//application.yml
spring:  
  profiles:  
    active: dev//开发环境  
  main:  
    allow-circular-references: true  //驼峰命名开关
  datasource:  
    druid:  
      driver-class-name: ${sky.datasource.driver-class-name}  
      url: jdbc:mysql://${sky.datasource.host}:${sky.datasource.port}/${sky.datasource.database}?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true  
      username: ${sky.datasource.username}  
      password: ${sky.datasource.password}
      
//application-dev.yml
sky:  

  alioss:  
    endpoint: "对应的endpoint"  例如:"oss-cn-hangzhou.aliyuncs.com"
    access-key-id: "对应的access-key-id"  
    access-key-secret: "对应的access-key-secret"  
    bucket-name: "对应的bucket-name"


```

上传文件实现模块代码:
```

package com.sky.controller.admin;  
  
import com.sky.result.Result;  
import com.sky.utils.AliOssUtil;  
import io.swagger.annotations.ApiOperation;  
import lombok.extern.slf4j.Slf4j;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.web.bind.annotation.PostMapping;  
import org.springframework.web.bind.annotation.RequestBody;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RestController;  
import org.springframework.web.multipart.MultipartFile;  
  
import java.io.IOException;  
import java.util.UUID;  
  
@Slf4j  
@RestController  
@RequestMapping("/admin/common")  
public class CommonController {  
    @Autowired  
    AliOssUtil aliOssUtil;  
    @PostMapping("/upload")  
    @ApiOperation("文件上传")  
    public Result<String> FileUpload(@RequestBody MultipartFile file){  
        log.info("文件上传,文件名:{}",file.getOriginalFilename());  
        /*调用Alioss工具类*/  
        try {  
            /*获取原始文件名*/  
            String originalFilename = file.getOriginalFilename();  
            /*获取原始文件名后缀*/  
            String extension = originalFilename.substring(originalFilename.lastIndexOf('.'));  
            /*构造新文件名称*/  
            String objectname =UUID.randomUUID().toString()+ extension;  
            String filePath=aliOssUtil.upload(file.getBytes(),objectname);  
            return Result.success(filePath);  
        } catch (IOException e) {  
            log.info("文件上传失败{}",e);  
        }  
        return null;  
    }  
}


```
 

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 非常感谢您的提问。开发音乐播放器上传录制歌曲模块流程大致如下: 1. 确定需求:确定上传录制歌曲模块的功能需求,包括支持的音频格式、上传限制等。 2. 设计架构:根据需求设计模块的架构,包括前端界面和后端逻辑。 3. 编写代码:根据架构设计编写代码,包括前端页面和后端接口。 4. 调试测试:对代码进行调试和测试,确保模块的功能正常。 5. 部署上线:将模块部署到服务器上线,供用户使用。 希望这个回答能够帮助您。如果您有更多问题,请随时提出。 ### 回答2: 音乐播放器上传录制歌曲模块开发流程主要包括需求分析、设计、开发、测试和上线五个阶段。 首先,需求分析阶段是整个开发流程的起点。开发团队需要与客户或产品经理沟通,明确上传录制歌曲模块的功能需求和用户需求。确定上传文件类型、录制音频格式、上传方式、存储方式等方面的需求。 接下来,设计阶段根据需求分析的结果进行系统设计。确定模块的功能结构和交互设计,包括用户界面设计和系统架构设计。确定好后端存储数据库的表结构和数据字段,以及前端用户界面的布局和交互逻辑。 然后,进入开发阶段,开发团队按照设计阶段确定的需求和设计方案进行编码。前端开发人员负责开发用户界面,实现上传录制歌曲的功能和用户交互逻辑。后端开发人员负责编写后端接口和业务逻辑,完成文件上传和录制音频的功能。 开发完成后,进行测试阶段。测试人员根据设计需求编写测试用例,对上传录制歌曲模块进行功能测试、性能测试和稳定性测试。发现问题后及时反馈给开发团队进行修改和优化,直至通过测试。 最后,开发完成并通过测试的上传录制歌曲模块可以进入上线阶段。将模块部署到音乐播放器的服务器上,并进行上线前的最后一次验证和测试。确保模块在正式上线后能够正常运行。 总之,音乐播放器上传录制歌曲模块开发流程包括需求分析、设计、开发、测试和上线五个阶段,每个阶段都有相应的工作和任务,需要团队成员的协作和配合,以最终实现符合用户需求的上传录制歌曲功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值