(一):首先得在阿里云OSS上开通相关的服务,具体哪里开通及如何操作请参考此链接:
https://help.aliyun.com/document_detail/31883.html?spm=5176.10695662.1996646101.searchclickresult.5ceb3ee82NzMq7&aly_as=WhUj5kPK

或者视频地址请参考此链接:http://cloud.video.taobao.com/play/u/2955313663/p/1/e/6/t/1/218785851580.mp4

(二):阿里云OSS常见问题及介绍参考此链接:
https://yq.aliyun.com/articles/716527?spm=a2c4e.11155472.0.0.2fe35cbed5F3GK

(三):阿里云OSS错误查询及问题解决参考此链接:
https://error-center.aliyun.com/status/product/Oss

你也可以在此来链接地址上提问题:
https://selfservice.console.aliyun.com/ticket/createIndex

(四):基本功能介绍
初始化 创建存储空间 上传文件 跨域访问设置 设置读写权限

1.初始化:

private string accessKeyId = "xxxxxxxxx";
private string accessKeySecret = "xxxxxxxxxx";
private string endpoint = "oss-cn-beijing.aliyuncs.com";           //OSS对应的区域地址private static OssClient ossClient = new OssClient(endpoint, accessKeyId, accessKeySecret);

2.创建存储空间:

ossClient.CreateBucket("myBucket"); //新建一个Bucket

3.设置读写权限:

//CannedAccessControlList有三个属性:Private(私有),PublicRead(公共读),PublicReadWrite(公共读写)
ossClient.SetBucketAcl("myBucket", CannedAccessControlList.PublicRead);  //设置为公共读

4.跨域访问设置:

var req = new SetBucketCorsRequest("myBucket");
var rule = new CORSRule();
//指定允许跨域请求的来源
rule.AddAllowedOrigin("*");
//指定允许的跨域请求方法(GET/PUT/DELETE/POST/HEAD)
rule.AddAllowedMethod("POST");
//控制在OPTIONS预取指令中Access-Control-Request-Headers头中指定的header是否允许。
rule.AddAllowedHeader("*");req.AddCORSRule(rule);
ossClient.SetBucketCors(req);

(五):代码示例如下

1:上传

在写代码期间首先需要对如下代码有所认识:

OSS oSSClient = new OSSClientBuilder().build(ENDPOINTBEIJING,ACCESSKEYID,ACCESSKEYSECRET);
ossClient.shutdown();

理解:对比输入输出流来理解想当于启动流和最后关闭流,在swagger中try it out 每个方法时都要开流关流,如果开流是所有方法共用的(声明为成员变量)这样一个方法运行完之后流就关了,在执行下一个方法时就不能了(还得在重新开启),所有不要把
OSS oSSClient = new OSSClientBuilder().build(ENDPOINTBEIJING,ACCESSKEYID,ACCESSKEYSECRET); 声明为成员变量

SendFileController

package com.richfit.richfit.controller;import com.aliyun.oss.model.ObjectMetadata;
import com.richfit.richfit.service.DownFileService;
import com.richfit.richfit.service.SendFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.io.FileNotFoundException;
import java.net.URL;/*** @ClassName SendFileController* @Description: 上传文件* @Author BruthLi* @Date 2020/2/4* @Version V1.0**/
@Api(value = "上传文件或下载文件")
@RestController
@RequestMapping(value = "/sys/sendfile")
@Slf4j
public class SendFileController {@Autowiredprivate SendFileService sendFileService;@Autowiredprivate DownFileService downFileService;//本地或者服务器上文件的路径//private final String filePath="E:\\picture\\15C1D7875F17BE8808191E8A8B2EA1D6.jpg";@ApiOperation(value = "获取OSS图片,文档,网络流URL用于数据库保存",notes = "获取OSS图片,文档,网络流URL用于数据库保存")@RequestMapping(value = "/getOSSUrl",method = RequestMethod.GET)public URL getOSSUrl(@ApiParam(name = "key",value = "OSS上OSS图片,文档,网络流URL的名称",required = true) @RequestParam(value = "key",required = true) String key,@ApiParam(name = "bucketName",value = "OSS上Bucket名称",required = true) @RequestParam(value = "bucketName",required = true)String bucketName) throws FileNotFoundException {return sendFileService.getOSSUrl(key,bucketName);}@ApiOperation(value = "上传OSS图片或者视频,上传一张图片或者一个视频",notes = "上传OSS图片或者视频,上传一张图片或者一个视频例如 beautyleg/beautyleg3.jpg,会先创建一个beautylegde的文件夹")@RequestMapping(value = "/sendosspicture",method = RequestMethod.POST)public  boolean putObject(@ApiParam(name = "bucketName",value = "OSS上Bucket名称",required = true) @RequestParam(value = "bucketName",required = true) String bucketName,@ApiParam(name = "key",value = "OSS上图片或者视频的名称",required = true) @RequestParam(value = "key",required = true) String key,@ApiParam(name = "filePath",value = "本地或者服务器上文件的路径",required = true) @RequestParam(value = "filePath",required = true) String filePath) throws FileNotFoundException {return  sendFileService.putObject(bucketName,key,filePath);}@ApiOperation(value = "删除OSS上图片,删除一张图片",notes = "删除OSS上图片,删除一张图片")@RequestMapping(value = "/deleteosspicture",method = RequestMethod.DELETE)public boolean deleteOssObject(@ApiParam(name = "key",value = "OSS上图片的名称",required = true) @RequestParam(value = "key",required = true) String key,@ApiParam(name = "bucketName",value = "OSS上Bucket名称",required = true) @RequestParam(value = "bucketName",required = true)String bucketName){return sendFileService.deleteOssObject(key,bucketName);}/*** .zip  .hprof  .jpg  .txt .mp4等好多格式上传到OSS上去* @param bucketName* @param key* @param filePath* @return* @throws FileNotFoundException*/@ApiOperation(value = "上传OSS文档",notes = "上传OSS文档,注意filepath,key格式一样例如 .txt")@RequestMapping(value = "/sendossfile",method = RequestMethod.POST)public  boolean sendOssFile(@ApiParam(name = "bucketName",value = "OSS上Bucket名称",required = true) @RequestParam(value = "bucketName",required = true) String bucketName,@ApiParam(name = "key",value = "OSS上文档的名称",required = true) @RequestParam(value = "key",required = true) String key,@ApiParam(name = "filePath",value = "本地或者服务器上文件的路径",required = true) @RequestParam(value = "filePath",required = true) String filePath) throws FileNotFoundException {return  sendFileService.sendOssFile(bucketName,key,filePath);}@ApiOperation(value = "上传OSS网络流",notes = "上传OSS网络流")@RequestMapping(value = "/sendossstream",method = RequestMethod.POST)public  boolean sendossstream(@ApiParam(name = "bucketName",value = "OSS上Bucket名称",required = true) @RequestParam(value = "bucketName",required = true) String bucketName,@ApiParam(name = "key",value = "OSS上网络流的名称,这个随便写就行例如 lol jk",required = true) @RequestParam(value = "key",required = true) String key,@ApiParam(name = "streamPath",value = "链接地址",required = true) @RequestParam(value = "streamPath",required = true) String streamPath) throws FileNotFoundException {return  sendFileService.sendossstream(bucketName,key,streamPath);}/*** 可以下载到本地格式为  .zip  .hprof  .jpg  .txt .mp4等好多格式* 下载功能现在没问题但是前台报错:org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON:* @param objectName* @param bucketName* @param localFileAddressName* @return*/@ApiOperation(value = "下载OSS上已经存在的东西,存到本地文件,",notes = "下载OSS上已经存在的东西,存到idea项目指定文件")@RequestMapping(value = "/downossfile",method = RequestMethod.POST)public ObjectMetadata downOssFile(@ApiParam(name = "objectName",value = "OSS上已经存在东西名称,例如  tt.jpg",required = true) @RequestParam(value = "objectName",required = true) String objectName,@ApiParam(name = "bucketName",value = "OSS上Bucket名称",required = true) @RequestParam(value = "bucketName",required = true)String bucketName,@ApiParam(name = "localFileAddressName",value = "下载新生产文档名称,若是 .zip 结尾则可以下载为压缩包",required = true) @RequestParam(value = "localFileAddressName",required = true)String localFileAddressName){return downFileService.downOssFile(objectName ,bucketName,localFileAddressName);}
}

SendFileServiceImpl

package com.richfit.richfit.service;import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import com.richfit.richfit.OSSClientUtil;
import org.springframework.stereotype.Service;import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;/*** @ClassName SendFileServiceImpl* @Description: 上传文件* @Author BruthLi* @Date 2020/2/4* @Version V1.0**/
@Service
public class SendFileServiceImpl implements SendFileService{/*** 获取OSS图片,文档,网络流URL* 类似于http://tylgd.oss-cn-beijing.aliyuncs.com/beautyleg1.jpg?Expires=1580794877&OSSAccessKeyId=LTAIzSTybwTVdqMn&Signature=YaHze%2Bvh4ZihN5%2Bbrz1lQ0hbtBo%3D* @return*/@Overridepublic URL getOSSUrl(String key, String bucketName) {Date date = new Date(new Date().getTime());OSS ossClientBeiJing = OSSClientUtil.getOssClientBeiJing();URL url = ossClientBeiJing.generatePresignedUrl(bucketName, key, date);return url;}/*** 上传OSS图片 上传一张图片 或者一个视频(例如 .mp4)* @return*/@Overridepublic boolean putObject(String bucketName,String key,String filePath) {// key指的是 保存在oss上后的路径+文件名// filePath 指的是上传的文件路径//参数设置//关于这个endPoint,可以参考:http://bbs.aliyun.com/read/149100.html?spm=5176.7189909.0.0.YiwiFw//可能报:The bucket you are attempting to access must be addressed using the specified endpoint. Please send//System.out.println("Error Message: " + oe.getErrorCode()); 提示你endpoint如何写//https://blog.csdn.net/torpidcat/article/details/82867093 endpoint错误提示//String bucketName = "tylgd";//保存在oss上的文件名//String key="beautyleg1.jpg";//本地或者服务器上文件的路径//String filePath="E:\\picture\\15C1D7875F17BE8808191E8A8B2EA1D6.jpg";//本地或者服务器上文件的路径boolean flag=false;OSS ossClientBeiJing = OSSClientUtil.getOssClientBeiJing();try{// 获取指定文件的输入流File file = new File(filePath);InputStream content = new FileInputStream(file);// 创建上传Object的MetadataObjectMetadata meta = new ObjectMetadata();// 必须设置ContentLengthmeta.setContentLength(file.length());// 上传Object.PutObjectResult result = ossClientBeiJing.putObject(bucketName, key, content, meta);if (file.isFile() && file.exists()) {flag = true;}}catch(OSSException oe){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());flag=false;}finally {ossClientBeiJing.shutdown();return flag;}}/*** 删除OSS上图片 删除一张图片* @param key 图片在OSS上的名称* @return*/@Overridepublic boolean deleteOssObject(String key,String bucketName) {OSS ossClientBeiJing = OSSClientUtil.getOssClientBeiJing();boolean b = ossClientBeiJing.doesObjectExist(bucketName, key);//如果存在返回的是true b为trueif (b){ObjectListing objectListing = ossClientBeiJing.listObjects(bucketName);ossClientBeiJing.deleteObject(bucketName,key);ossClientBeiJing.shutdown();return true;}return false;}/*** 上传OSS文档* @param filePath bucketName key* @return*/@Overridepublic boolean sendOssFile(String bucketName,String key,String filePath) {boolean flag=false;OSS ossClientBeiJing = OSSClientUtil.getOssClientBeiJing();try{// 获取指定文件的输入流File file = new File(filePath);InputStream content = new FileInputStream(file);// 创建上传Object的Metadata//ObjectMetadata meta = new ObjectMetadata();// 必须设置ContentLength//meta.setContentLength(file.length());// 上传Object.PutObjectResult result = ossClientBeiJing.putObject(bucketName, key, content);if (file.isFile() && file.exists()) {flag = true;}}catch(OSSException oe){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());flag=false;}finally {ossClientBeiJing.shutdown();return flag;}}/*** 上传OSS网络流* @param bucketName* @param key* @param streamPath* @return*/@Overridepublic boolean sendossstream(String bucketName, String key, String streamPath) {boolean flag=true;OSS ossClientBeiJing = OSSClientUtil.getOssClientBeiJing();try{// 获取指定文件的输入流InputStream content = new URL(streamPath).openStream();PutObjectResult result = ossClientBeiJing.putObject(bucketName, key, content);}catch(OSSException oe){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());flag=false;}finally {ossClientBeiJing.shutdown();return flag;}}}

OSSClientUtil

其实阿里云OSS自己就有一个OSSUtils 只是自己又写了一个方便自己用

package com.richfit.richfit;import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;/*** @ClassName OSSClient* @Description:  其实阿里云OSS自己就有一个OSSUtils  只是自己又写了一个方便自己用* @Author BruthLi* @Date 2020/2/4* @Version V1.0**/
public class OSSClientUtil {//http://oss-cn-hangzhou.aliyuncs.com 杭州的public static final String ENDPOINTBEIJING = "http://oss-cn-beijing.aliyuncs.com";//北京的接口public static final String ACCESSKEYID = "**********";public static final String ACCESSKEYSECRET = "*********";/*** 北京的OSSClient* @return*/public static OSS getOssClientBeiJing(){OSS oSSClient = new OSSClientBuilder().build(ENDPOINTBEIJING,ACCESSKEYID,ACCESSKEYSECRET);return oSSClient;}/*** 杭州的OSSClient* @return*//* public OSSClient getOssClientHangZhou(){OSSClient oSSClient = new OSSClient(endpointbeijing,accessKeyId,accessKeySecret);return oSSClient;}*/
}

2:下载

DownFileController

/*** 可以下载到本地格式为  .zip  .hprof  .jpg  .txt .mp4等好多格式* 下载功能现在没问题但是前台报错:org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON:* @param objectName* @param bucketName* @param localFileAddressName* @return*/@ApiOperation(value = "下载OSS上已经存在的东西,存到本地文件,",notes = "下载OSS上已经存在的东西,存到idea项目指定文件")@RequestMapping(value = "/downossfile",method = RequestMethod.POST)public ObjectMetadata downOssFile(@ApiParam(name = "objectName",value = "OSS上已经存在东西名称,例如  tt.jpg",required = true) @RequestParam(value = "objectName",required = true) String objectName,@ApiParam(name = "bucketName",value = "OSS上Bucket名称",required = true) @RequestParam(value = "bucketName",required = true)String bucketName,@ApiParam(name = "localFileAddressName",value = "下载新生产文档名称,若是 .zip 结尾则可以下载为压缩包",required = true) @RequestParam(value = "localFileAddressName",required = true)String localFileAddressName){return downFileService.downOssFile(objectName ,bucketName,localFileAddressName);}

DownFileService

ObjectMetadata downOssFile(String objectName, String bucketName, String localFileAddressName);

DownFileServiceImpl

package com.richfit.richfit.service;import com.aliyun.oss.OSS;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.ObjectMetadata;
import com.richfit.richfit.FileTypeUtil;
import com.richfit.richfit.OSSClientUtil;
import org.springframework.stereotype.Service;import java.io.File;/*** @ClassName downFileServiceImpl* @Description: 下载OSS上已经存在的东西* @Author BruthLi* @Date 2020/2/4* @Version V1.0**/
@Service
public class DownFileServiceImpl implements DownFileService {/*** 下载OSS上已经存在的东西 存到idea项目指定文件* @param objectName* @param bucketName* @return*/@Overridepublic ObjectMetadata downOssFile(String objectName, String bucketName,String localFileAddressName) {//String localFileAddressName="src/main/resources/files";OSS ossClient = null;ObjectMetadata object = null;try {//创建OSSClient实例,用于操作oss空间ossClient = OSSClientUtil.getOssClientBeiJing();ossClient.setBucketAcl("tylgd", CannedAccessControlList.PublicReadWrite);//指定文件保存路径//String filePath = localFileAddressName+"/"+System.currentTimeMillis()+".jpg";//判断文件目录是否存在,不存在则创建  .zip  .hprof  结尾都可以  System.currentTimeMillis()+ FileTypeUtil.fileType() 子文件File file = new File(localFileAddressName,System.currentTimeMillis()+ FileTypeUtil.fileType(objectName));boolean newFile = file.createNewFile();if (newFile){file.setWritable(true);object = ossClient.getObject(new GetObjectRequest(bucketName, objectName), file);return object;}} catch (Exception e) {e.printStackTrace();}finally {ossClient.shutdown();}//判断保存文件名是否加后缀/*if (objectName.contains(".")){//指定文件保存名称filePath = filePath+"/"+objectName.substring(objectName.lastIndexOf("/")+1);}*///获取OSS文件并保存到本地指定路径中,此文件路径一定要存在,若保存目录不存在则报错,若保存文件名已存在则覆盖本地文件return object;}
}

OSSClientUtil

package com.richfit.richfit;import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;/*** @ClassName OSSClient* @Description:* @Author BruthLi* @Date 2020/2/4* @Version V1.0**/
public class OSSClientUtil {//http://oss-cn-hangzhou.aliyuncs.com 杭州的public static final String ENDPOINTBEIJING = "http://oss-cn-beijing.aliyuncs.com";//北京的接口public static final String ACCESSKEYID = "********";public static final String ACCESSKEYSECRET = "************";/*** 北京的OSSClient* @return*/public static OSS getOssClientBeiJing(){OSS oSSClient = new OSSClientBuilder().build(ENDPOINTBEIJING,ACCESSKEYID,ACCESSKEYSECRET);return oSSClient;}/*** 杭州的OSSClient* @return*//* public OSSClient getOssClientHangZhou(){OSSClient oSSClient = new OSSClient(endpointbeijing,accessKeyId,accessKeySecret);return oSSClient;}*/
}

FileTypeUtil

package com.richfit.richfit;/*** @ClassName FileTypeUtil* @Description: OSS上类型和本地文件格式一致* @Author BruthLi* @Date 2020/2/5* @Version V1.0**/
public class FileTypeUtil {public static String fileType(String samefiletypesame){int start = samefiletypesame.lastIndexOf(".");String substring = samefiletypesame.substring(start, samefiletypesame.length());if (".jpg".equalsIgnoreCase(substring)){return  substring;}else if (".doc".equalsIgnoreCase(substring)){return  substring;}else if (".docx".equalsIgnoreCase(substring)){return  substring;}else if (".md".equalsIgnoreCase(substring)){return  substring;}else if (".hprof".equalsIgnoreCase(substring)){return  substring;}else if (".mp4".equalsIgnoreCase(substring)){return  substring;}else if (".txt".equalsIgnoreCase(substring)){return  substring;}else if (".java".equalsIgnoreCase(substring)){return  substring;}else if (".class".equalsIgnoreCase(substring)){return  substring;}else if (".zip".equalsIgnoreCase(substring)){return  substring;}else if (".rpm".equalsIgnoreCase(substring)){return  substring;}else if (".xlsx".equalsIgnoreCase(substring)){return  substring;}else if (".html".equalsIgnoreCase(substring)){return  substring;}else if (".xlsx".equalsIgnoreCase(substring)){return  substring;}else if (".properties".equalsIgnoreCase(substring)){return  substring;}else if (".xml".equalsIgnoreCase(substring)){return  substring;}else if (".http".equalsIgnoreCase(substring)){return  substring;}else if (".jar".equalsIgnoreCase(substring)){return  substring;}else if (".war".equalsIgnoreCase(substring)){return  substring;}else if (".dat".equalsIgnoreCase(substring)){return  substring;}else if (".md".equalsIgnoreCase(substring)){return  substring;}else if (".cmd".equalsIgnoreCase(substring)){return  substring;}else if (".iml".equalsIgnoreCase(substring)){return  substring;}return "java代码规定没有此种文本格式!";}
}

3:发短信

SendSmsController

package com.richfit.richfit.controller;/*** @ClassName SendSms* @Description: 阿里云发送短信 SendSms ,  阿里云发送验证码 SendSms* @Author BruthLi* @Date 2020/2/3* @Version V1.0**/import com.richfit.richfit.service.SendSmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import java.util.Map;/*
pom.xml
<dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>4.0.3</version>
</dependency>
*/
@Api(value = "发送短信或验证码")
@RestController
@RequestMapping(value = "/sys/sendmessagecode")
@Slf4j
public class SendSmsController {@Autowiredprivate SendSmsService sendSmsService;final String phoneNumber = "**************";@ApiOperation(value = "查询验证码是否正确",notes = "查询验证码是否正确")@RequestMapping(value = "/sendcodeyesorno",method = RequestMethod.GET)public Map<String,String> sendCodeYesOrNo(String inputcode){Map<String, String> mapReturn = sendSmsMessage(phoneNumber);String code = mapReturn.get("code");if (code.equalsIgnoreCase(inputcode)){return  mapReturn;}mapReturn.put("code", "验证码:["+inputcode+"]输入不正确,请重新输入!");return  mapReturn;}/*** 发送短信或验证码* @param phoneNumber 电话号码* @return HttpServletRequest*/@ApiOperation(value = "发送短信或验证码",notes = "发送短信或验证码")@RequestMapping(value = "/sendcode",method = RequestMethod.POST)public Map<String, String> sendSmsMessage(String phoneNumber) {return sendSmsService.sendSmsMessage(phoneNumber);}
}

SendSmsServiceImpl

package com.richfit.richfit.service;import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Service;import java.util.HashMap;
import java.util.Map;/*** @ClassName SendSmsServiceImpl* @Description:* @Author BruthLi* @Date 2020/2/4* @Version V1.0**/
@Service
public class SendSmsServiceImpl implements SendSmsService {/*** 发送短信或验证码* @param phoneNumber* @return*/@Overridepublic Map<String, String> sendSmsMessage(String phoneNumber) {String jsonCode = "{code:" + (int) ((Math.random() * 9 + 1) * 100000) + "}";CommonResponse response = null;Map<String, String> mapCode = new HashMap<>();DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "*************", "*****************");IAcsClient client = new DefaultAcsClient(profile);CommonRequest request = new CommonRequest();request.setMethod(MethodType.POST);request.setDomain("dysmsapi.aliyuncs.com");request.setVersion("2017-05-25");request.setAction("SendSms");request.putQueryParameter("RegionId", "cn-hangzhou");//电话PhoneNumbers和TemplateParam可以变,其他是在阿里云申请后都是固定值request.putQueryParameter("PhoneNumbers", phoneNumber);request.putQueryParameter("SignName", "BruthLi");request.putQueryParameter("TemplateCode", "SMS_183150435");//在这定义自己发送短信的内容 name不变:后面的值可以变//request.putQueryParameter("TemplateParam","{'name':'妈妈你吃饭了吗'}");//发送6位验证码request.putQueryParameter("TemplateParam", jsonCode);try {response = client.getCommonResponse(request);if (response.getHttpStatus() == 200) {mapCode.put("code", jsonCode);}} catch (ServerException e) {e.printStackTrace();} catch (ClientException e) {e.printStackTrace();}return mapCode;}
}

(六):报错
(1):在做下载的时候报一个:拒绝访问盘符拒绝访问

java.io.FileNotFoundException: E:\oilrichfit\richfit\src\main\resources\files\1580826234328.jpg (拒绝访问。)

这个错是因为对盘符没有访问权限或者对OSS bucket没有访问权限
解决方案在合适位置加如下代码:

//CannedAccessControlList有三个属性:Private(私有),PublicRead(公共读)
ossClient.setBucketAcl("tylgd", CannedAccessControlList.PublicReadWrite);File file = new File(localFileAddressName,System.currentTimeMillis()+".mp4");
file.setWritable(true);

(2):在做下载的时候功能能实现但是前台仍然报错报一个(至今没有解决):org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON:

阿里云OSS上传下载和短信验证码相关推荐

  1. 阿里云OSS上传下载跨域问题

    OSS文件传输 使用OSS进行上传/下载操作时,时常会遇到跨域问题. 其表现为,直接通过浏览器,可以直接访问已经存储在OSS中得文件地址并下载,但是通过代码却会报跨域问题. 解决办法:登录阿里云OSS ...

  2. 阿里云oss上传下载删除工具类

    工具类-阿里云oss private static String accessId;private static String accessKey;private static String endp ...

  3. java实现阿里云OSS上传下载

    首先得开通OSS服务 话不多说,直接上代码 先导入所需依赖 1,上传 2,下载 注:我这里使用的accessKeyId,accessKeySecret等仅作为展示,实际参数查看开通的oss服务 话不多 ...

  4. thinkPHP 阿里云OSS 上传文件、直接下载

    阿里云OSS 上传文件.直接下载 1. 安装OSS SDK composer require aliyuncs/oss-sdk-php 2. thinkPHP接口 public function up ...

  5. 阿里云oss上传svg等格式的文件,返回的路径打开后是下载而不是预览

    在做的一个项目,阿里云oss上传svg等格式的文件,返回的路径打开后是下载而不是预览.之后web同事跟产品说不好处理,换回了使用jpg格式的文件.但是最终这个问题都是要解决的,产品要求下期也要上传sv ...

  6. Springboot集成 阿里云OSS上传及下载

    文章目录 使用手册 maven依赖及环境配置 定义配置bean及OSS工具类 定义UploadController和DownloadController 下载接口优化为返回重定向oss路径 遇到的问题 ...

  7. springboot整合阿里云oss上传的方法示例

    这篇文章主要介绍了springboot整合阿里云oss上传的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 OSS申请和 ...

  8. spring boot 整合 阿里云oss上传

    Spring Boot 整合 阿里云OSS上传 OSS申请和配置 1. 注册登录 2.开通以及配置 springboot整合使用 1. 进入我们springboot的项目中,导入oss相关依赖 2. ...

  9. 阿里云OSS上传文件设置请求头

    之前写过一篇发送文件消息的,原生input上传文件(聊天发送文件消息),这次的需求就是更改上传地址,使用阿里云OSS上传. 如果想了解阿里云对象存储OSS是如何使用的,请看阿里云存储对象OSS使用讲解 ...

最新文章

  1. 互联网人必读的30本书
  2. 异构计算架构师眼中的AI算法(object detection)
  3. 可实现自动设置尺寸的图片上传类
  4. vant自定义二级菜单
  5. Datawhale MySQL 训练营 Task2 查询语句
  6. Android之数据库操作
  7. NDPIC极简昼夜瀑布流图片WordPress主题
  8. 在线生成横幅BANNER广告图网站源码
  9. ERROR java.lang.NoClassDefFoundError
  10. PAT1015 德才论(C++)
  11. VC6.0代码移植到VS2008运行时乱码问题解决
  12. apache .htaccess 转化nginx工具
  13. [NOIP2017 普及组] 图书管理员
  14. c语言如何写出高清的录屏软件,电脑可以实现高清录屏的软件有哪些?看完你就明白了...
  15. DaHua工业相机开发中调试遇到的相机断开问题
  16. [高级]pdf生成(可水印)、pdf预览(可分页)、pdf打印:全栈一条龙方案
  17. FPGA基础之cyclone_iv资源概述
  18. noip2017广东提高组复赛成绩
  19. word打开文档很久很慢_打开Office文档很慢的解决办法
  20. Fastlane(一):用法

热门文章

  1. java 爬取评论,Java基于WebMagic爬取某豆瓣电影评论的实现
  2. ubuntu IOS文件下载
  3. STM32 CAN笔记(一)
  4. 从零开始实现一个MQTT客户端 开篇漫谈
  5. 搞清这几个答案在恋爱吧
  6. 【推荐系统】评估指标总结
  7. Java实现子序列问题
  8. 数据库系统——2.1、概念结构设计之概念模型(1)
  9. H.T. Kung对博士生研究的建议
  10. Win95下的注册表文件(User.dat,System.dat)文件格式说明 (转)