龙空技术网

使用代码上传图片和视频到阿里云OSS

CameSawConquer 115

前言:

如今大家对“apache文件上传pom”可能比较着重,我们都需要了解一些“apache文件上传pom”的相关内容。那么小编同时在网上汇集了一些对于“apache文件上传pom””的相关内容,希望小伙伴们能喜欢,咱们一起来了解一下吧!

官网地址:

输入官网地址开通账号

创建bucket桶

创建Bucket桶

创建Bucket桶

测试上传

测试上传

上传后的结果

开通视频点播

在右上角的搜索框搜索视频点播并点击

添加OSS自有的Bucket使状态为正常

测试上传视频

上传结果

代码实现流程

1、创建maven工程

具体结构

2、编辑pom.xml文件

<project xmlns="; xmlns:xsi=";  xsi:schemaLocation=" ;>  <modelVersion>4.0.0</modelVersion>  <groupId>com.gec.oss</groupId>  <artifactId>springboot-oss-file</artifactId>  <version>1.0-SNAPSHOT</version>  <parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>2.2.6.RELEASE</version>    <relativePath /> <!-- lookup parent from repository -->  </parent>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>    <java.version>1.8</java.version>  </properties>  <dependencies>    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-test</artifactId>      <scope>test</scope>    </dependency>    <!--阿里云-->    <dependency>      <groupId>com.aliyun</groupId>      <artifactId>aliyun-java-sdk-core</artifactId>      <version>4.5.1</version>    </dependency>    <dependency>      <groupId>com.aliyun</groupId>      <artifactId>aliyun-java-sdk-dysmsapi</artifactId>      <version>1.1.0</version>    </dependency>    <dependency>      <groupId>com.aliyun</groupId>      <artifactId>aliyun-java-sdk-vod</artifactId>      <version>2.15.11</version>    </dependency>    <dependency>      <groupId>com.aliyun.oss</groupId>      <artifactId>aliyun-sdk-oss</artifactId>      <version>3.10.2</version>    </dependency>    <dependency>      <groupId>com.aliyun</groupId>      <artifactId>aliyun-sdk-vod-upload</artifactId>      <version>1.4.15</version>    </dependency>    <!--fastjson-->    <dependency>      <groupId>com.alibaba</groupId>      <artifactId>fastjson</artifactId>      <version>1.2.29</version>    </dependency>  </dependencies>  <build>    <plugins>      <plugin>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-maven-plugin</artifactId>      </plugin>    </plugins>  </build></project>

3、在resources下创建aplication.yml文件

accessKeyId、regionId、accessKeySecret使用自己的

查看自己的key和秘钥

找到AccessKey管理

点击继续使用AccessKey

查看Secret

查看的结果

spring:  servlet:    multipart:      max-file-size: -1      max-request-size: 500MB# 阿里配置# 阿里配置al:  oss: #存储    endpoint: oss-cn-guangzhou.aliyuncs.com   # 域    accessKeyId: LTAI5t8SxoHMePy6f5Zju5Jc  # 秘钥    accessKeySecret: zuIn8f8m9S8PwqrqOHUSKH9N7vLN6r # 秘钥    bucketName: camesawconquered  # 桶名    url:   # 访问地址  vod: # 视频    regionId: cn-shanghai    accessKeyId: LTAI5t8SxoHMePy6f5Zju5Jc    accessKeySecret: zuIn8f8m9S8PwqrqOHUSKH9N7vLN6r

4、在src/main/java下创建com.gec.oss.controller包、com.gec.oss.utils包

5、在com.gec.oss.utils包下创建VodConfig类和VodTemplate类:

VodConfig类

package com.gec.oss.utils;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Configuration;//oss存储配置@Configuration@ConfigurationProperties("al.vod")public class VodConfig {    //区域    private String regionId;    //访问id    private String accessKeyId;    //访问秘钥    private String accessKeySecret;    public String getRegionId() {        return regionId;    }    public void setRegionId(String regionId) {        this.regionId = regionId;    }    public String getAccessKeyId() {        return accessKeyId;    }    public void setAccessKeyId(String accessKeyId) {        this.accessKeyId = accessKeyId;    }    public String getAccessKeySecret() {        return accessKeySecret;    }    public void setAccessKeySecret(String accessKeySecret) {        this.accessKeySecret = accessKeySecret;    }}

VodTemplate类

package com.gec.oss.utils;import com.aliyun.vod.upload.impl.UploadVideoImpl;import com.aliyun.vod.upload.req.UploadStreamRequest;import com.aliyun.vod.upload.resp.UploadStreamResponse;import com.aliyuncs.DefaultAcsClient;import com.aliyuncs.profile.DefaultProfile;import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import java.io.InputStream;//视频操作@Componentpublic class VodTemplate {    @Autowired    private VodConfig vodConfig;    //初始化    public DefaultAcsClient initVodClient() {        DefaultProfile profile = DefaultProfile.getProfile(                vodConfig.getRegionId(),                vodConfig.getAccessKeyId(),                vodConfig.getAccessKeySecret());        return new DefaultAcsClient(profile);    }    //上传视频,返回视频id    public String uploadVideo(String fileName, InputStream inputStream) {        String title = fileName.substring(0, fileName.lastIndexOf("."));        UploadStreamRequest request = new UploadStreamRequest(                vodConfig.getAccessKeyId(),                vodConfig.getAccessKeySecret(),                title,                fileName,                inputStream);        UploadVideoImpl uploader = new UploadVideoImpl();        UploadStreamResponse response = uploader.uploadStream(request);        String videoId = response.getVideoId();        return videoId;    }    //获取播放凭证函数    public GetVideoPlayAuthResponse getVideoPlayAuth(String videoId) throws Exception {        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();        request.setVideoId(videoId);        return initVodClient().getAcsResponse(request);    }}

6、com.gec.oss.controller包下创建FileController类:

FileController类

package com.gec.oss.controller;import com.aliyun.oss.OSS;import com.aliyun.oss.OSSClientBuilder;import com.gec.oss.utils.VodTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.*;import org.springframework.web.multipart.MultipartFile;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;@RestController@CrossOrigin("*")   //后端跨域public class FileController {    @Value("${al.oss.bucketName}")    private String bucketName;    @Value("${al.oss.endpoint}")    private String endpoint;    @Value("${al.oss.accessKeyId}")    private String accessKeyId;    @Value("${al.oss.accessKeySecret}")    private String accessKeySecret;    @Value("${al.oss.url}")    private String url;    @Autowired    private VodTemplate vodTemplate;    //返回图片的访问地址    @PostMapping("/uploadPicFile")    public String uploadPicFile(MultipartFile file){        //创建ossclient实例        OSS ossClient=new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);        if (!ossClient.doesBucketExist(bucketName)){            System.out.println("你的Bucket不存在");            ossClient.createBucket(bucketName);        }else {            System.out.println("已经创建了bucket");        }        //设计文件存储对象(路径+文件名)        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");        String filename=file.getOriginalFilename();        String objectName=sdf.format(new Date())+"/"+filename;        try{            //存储对象            ossClient.putObject(bucketName,objectName,file.getInputStream());        }catch (IOException e){            e.printStackTrace();        }        //        String httpUrl=url+"/"+objectName;        System.out.println(httpUrl);        return httpUrl;    }    @RequestMapping("/uploadVideo")    @CrossOrigin    public String uploadVideo(MultipartFile uploadVideo) throws IOException{        String videoId=this.vodTemplate.uploadVideo(uploadVideo.getOriginalFilename(),uploadVideo.getInputStream());        System.out.println(videoId);        return videoId;    }}

7、在com.gec.oss下创建FileApp启动类

FileApp

package com.gec.oss;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class FileApp {    public static void main(String[] args) {        SpringApplication.run(FileApp.class,args);    }}

8、运行FileApp类

FileApp运行结果

9、打开Postman并进行操作

上传文件流程

点击Send人,然后去OSS管理控制台看有没有添加文件

OSS管理控制台上传的结果

上传视频流程

然后去视频点播控制台看一下刚上传的视频

视频点播控制台上传的结果

标签: #apache文件上传pom