文件(视频)上传到阿里云 java实现
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                文件(视频)上传到阿里云 java实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                前臺使用postman進行測試,因此我們只寫后臺代碼,
postman請求發送格式如下:
接下來是后臺接收前臺請求的代碼,注意我為后臺請求的所有返回結果做了統一,相關鏈接在這https://blog.csdn.net/qq_42331202/article/details/115823134
package com.example.learn.controller;import com.example.learn.common.Result; import com.example.learn.utils.OSSUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.UUID;/*** @author : wangbo* @version : 1.0* @date :Create in 2021/4/18* @description :*/ @RestController @RequestMapping("video") @Api(value = "視頻控制器") public class VideoController {SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy/MM/dd");@ApiOperation(value = "添加視頻")@PostMapping("/add")public Result add(MultipartFile multipartFile,HttpServletRequest request) {Map<String,Object> map=new HashMap<>();String originalFilename = multipartFile.getOriginalFilename();if(!originalFilename.endsWith(".mp4")){return Result.error().message("文件類型不對");}String format = simpleDateFormat.format(new Date());System.out.println("format是:" + format);String newName = UUID.randomUUID().toString() + ".mp4";String objectName=format+newName;Result result = OSSUtils.createOSSClient(objectName, multipartFile);return result;}}接下來是阿里云工具類的相關代碼:
package com.example.learn.utils;import com.aliyun.oss.ClientBuilderConfiguration; import com.aliyun.oss.HttpMethod; import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.example.learn.common.Result; import org.springframework.web.multipart.MultipartFile;import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.net.URL; import java.util.Date; import java.util.HashMap; import java.util.Map;/*** @author : wangbo* @version : 1.0* @date :Create in 2021/4/18* @description :*/ public class OSSUtils {public static void main(String[] args) {}public static Result createOSSClient(String object_Name, MultipartFile multipartFile){Map<String,Object> map=new HashMap<>();String endpoint = "你的endpoint"; // 阿里云主賬號AccessKey擁有所有API的訪問權限,風險很高。強烈建議您創建并使用RAM賬號進行API訪問或日常運維,請登錄RAM控制臺創建RAM賬號。String accessKeyId = "你的id";String accessKeySecret = "你的secret";String bucketName = "你的bucketName";// 創建OSSClient實例。OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);String objectName=object_Name;try {ossClient.putObject(bucketName, objectName, multipartFile.getInputStream());} catch (Exception e) {e.printStackTrace();}finally {Date expiredTime = new Date(System.currentTimeMillis() + 3600L * 1000L);URL url = ossClient.generatePresignedUrl(bucketName,objectName,expiredTime );map.put("url",url.toString());// 關閉OSSClient。ossClient.shutdown();return Result.ok().data(map);}} }如上便可完成對mp4格式的視頻進行上傳~
總結
以上是生活随笔為你收集整理的文件(视频)上传到阿里云 java实现的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: html如何实现切换效果,纯CSS实现页
- 下一篇: c html联调,JS与native 交
