java demo
package com.lofxve.utils;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
public class OssUtils {
private static String endpoint = "";
private static String accessKeyId = "";
private static String accessKeySecret = "";
private static String bucketName = "";
private static String url = "";
public static String uploadByFile(String fileName, InputStream inputStream) {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
ObjectMetadata meta = new ObjectMetadata();
meta.setContentType("image/jpg");
String objectName = getObjectName();
ossClient.putObject(bucketName, objectName, inputStream, meta);
ossClient.shutdown();
return url + "/" + objectName;
}
public static String uploadByByte(byte[] imageByte) {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
ObjectMetadata meta = new ObjectMetadata();
meta.setContentType("image/jpg");
String objectName = getObjectName();
ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(imageByte), meta);
ossClient.shutdown();
return url + "/" + objectName;
}
public static String getObjectName() {
String objectName = "images/" + new SimpleDateFormat("yyyy/MM/dd").format(new Date())
+ "/" + System.currentTimeMillis() + UUID.randomUUID().toString().replace("-","") + ".jpg";
return objectName;
}
public static boolean deleteOss(String objectName) {
try {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
URL url = new URL(objectName);
ossClient.deleteObject(bucketName, url.getPath().replaceFirst("/", ""));
ossClient.shutdown();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return true;
}
public static void main(String[] args) throws FileNotFoundException, MalformedURLException {
String filePath = "C:\\B3AABC31A96839494504F68450DC1BBC.png";
File file = new File(filePath);
}
}
spring 集成
package com.newgrand.saas.platform.core;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
@Component
public class OssUtils {
@Value("${aliyun.oss.file.accessKeyId}")
private String accessKeyId;
@Value("${aliyun.oss.file.accessKeySecret}")
private String accessKeySecret;
@Value("${aliyun.oss.file.bucketName}")
private String bucketName;
@Value("${aliyun.oss.file.url}")
private String url;
@Value("${aliyun.oss.file.endpoint}")
private String endpoint;
@Value("${aliyun.oss.file.expiration}")
private Integer expiration;
public String uploadByFile(String fileName, InputStream inputStream) {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
ObjectMetadata meta = new ObjectMetadata();
meta.setContentType("image/jpg");
String objectName = getObjectName();
ossClient.putObject(bucketName, objectName, inputStream, meta);
ossClient.shutdown();
return url + "/" + objectName;
}
public String uploadByByte(byte[] imageByte) {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
ObjectMetadata meta = new ObjectMetadata();
meta.setContentType("image/jpg");
String objectName = getObjectName();
ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(imageByte), meta);
ossClient.shutdown();
return url + "/" + objectName;
}
public String getObjectName() {
String objectName = "images/" + new SimpleDateFormat("yyyy/MM/dd").format(new Date())
+ "/" + System.currentTimeMillis() + UUID.randomUUID().toString().replace("-", "") + ".jpg";
return objectName;
}
public boolean deleteOss(String objectName) {
try {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
URL url = new URL(objectName);
ossClient.deleteObject(bucketName, url.getPath().replaceFirst("/", ""));
ossClient.shutdown();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return true;
}
public String getUrl(String objectName) {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
Date expirationDate = new Date(System.currentTimeMillis() + expiration * 60 * 1000);
URL url = ossClient.generatePresignedUrl(bucketName, objectName, expirationDate);
return url.toString();
}
}
yaml
aliyun:
oss:
file:
accessKeyId: 6a2P5oyv5Lic6a2P5oyv5Lic6a2P5oyv5Lic
accessKeySecret: 6a2P5oyv5Lic6a2P5oyv5Lic6a2P5oyv5Lic6a2P5oyv5Lic6a2P5oyv5Lic
endpoint: oss-cn-hangzhou.aliyuncs.com
bucketName: kkkk
url: https://kkkk.oss-cn-hangzhou.aliyuncs.com
expiration: 60 #单位分钟
图片与base64字符串互转,base64与byte[]互转
package com.lofxve;
import org.apache.commons.codec.binary.Base64;
import java.io.*;
public class FileBase64ConvertUitl {
public static String imageToBase64Str(String imgFile) {
InputStream inputStream = null;
byte[] data = null;
try {
inputStream = new FileInputStream(imgFile);
data = new byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return Base64.encodeBase64String(data);
}
public static boolean base64StrToImage(String imgStr, String path) {
if (imgStr == null) {
return false;
}
Base64 decoder = new Base64();
try {
byte[] b = decoder.decode(imgStr);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
File tempFile = new File(path);
if (!tempFile.getParentFile().exists()) {
tempFile.getParentFile().mkdirs();
}
OutputStream out = new FileOutputStream(tempFile);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
public static byte[] base64String2ByteFun(String base64Str) {
return Base64.decodeBase64(base64Str);
}
public static String byte2Base64StringFun(byte[] b) {
return Base64.encodeBase64String(b);
}
public static boolean byteToImage(byte[] b, String path) {
try {
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
File tempFile = new File(path);
if (!tempFile.getParentFile().exists()) {
tempFile.getParentFile().mkdirs();
}
OutputStream out = new FileOutputStream(tempFile);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
}