@PostMapping("/save")
public R save(@RequestBody ShareEntity share){
OSSClient oss = new OSSClient(GlobalConfig.domain, GlobalConfig.AccessKeyID,GlobalConfig.AccessKeySecret);
InputStream inputStream1;
String[] shartimg = new String[2];
if(!share.getShareImg().isEmpty()){ //base64所在字段
shartimg = share.getShareImg().split(",");
int index = shartimg[0].indexOf("/");
int endindex = shartimg[0].indexOf(";");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String date = sdf.format(new Date());
String endName = shartimg[0].substring(index+1,endindex);//獲取文件後綴
String newFileName = date +"."+ endName;// 新文件名
try {
byte[] bytes = new BASE64Decoder().decodeBuffer(shartimg[1]); //將字符串轉換爲byte數組
inputStream1 = new ByteArrayInputStream(bytes);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(inputStream1.available());
oss.putObject(GlobalConfig.bucket,GlobalConfig.packagename+"/"+newFileName, inputStream1, metadata);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String shareImg ="https://"+GlobalConfig.bucket+".oss-cn-beijing.aliyuncs.com/"+GlobalConfig.packagename+"/"+newFileName;
share.setShareImg(shareImg);
shareService.save(share);//service添加方法
return R.ok();
}else{
return R.ok().put("data","數據有誤");
}
}
public class GlobalConfig {
public final static String domain = "http://oss-cn-beijing.aliyuncs.com";//endpoint
public final static String bucket = "bucketName";//上傳的bucket名
public final static String packagename = "manager";//圖片所在文件夾名
public final static String AccessKeyID = "";//id
public final static String AccessKeySecret = "";//密碼
}