/***
* 上传
* @param OSSStorePath 前缀不要加/
* @param input
* @return
*/
public static String upload(String OSSStorePath, InputStream input, String contentDispositio) {
// 生成OSSClient
OSS ossClient = null;
try {
ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 如果需要上传时设置存储类型与访问权限,请参考以下示例代码。
ObjectMetadata metadata = new ObjectMetadata();
//文件存储入OSS
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, OSSStorePath, input);
//inline:直接在应用(例如浏览器)中打开Object。
//attachment:将Object下载到本地。附加filename可预设Object保存在
if (Strings.isNullOrEmpty(contentDispositio)) {
metadata.setHeader(OSSHeaders.CONTENT_DISPOSITION, "attachment");
}else{
metadata.setHeader(OSSHeaders.CONTENT_DISPOSITION, contentDispositio);
}
putObjectRequest.setMetadata(metadata);
// 上传文件。
ossClient.putObject(putObjectRequest);
return bucketUrl + OSSStorePath;
} catch (Exception ex) {
log.error("文件上传OSS失败:" + ex.getMessage());
return "";
} finally {
// 关闭OSSClient。
if (ossClient != null) {
ossClient.shutdown();
}
}
}
/***
* 上传
* @param OSSStorePath 前缀不要加/
* @param fileBytes
* @return
*/
public static String upload(String OSSStorePath, byte[] fileBytes) {
// 生成OSSClient
OSS ossClient = null;
try {
ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 如果需要上传时设置存储类型与访问权限,请参考以下示例代码。
ObjectMetadata metadata = new ObjectMetadata();
//文件存储入OSS
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, OSSStorePath, new ByteArrayInputStream(fileBytes));
//inline:直接在应用(例如浏览器)中打开Object。
//attachment:将Object下载到本地。附加filename可预设Object保存在
metadata.setHeader(OSSHeaders.CONTENT_DISPOSITION, "attachment");
putObjectRequest.setMetadata(metadata);
// 上传文件。
ossClient.putObject(putObjectRequest);
return bucketUrl + OSSStorePath;
} catch (Exception ex) {
log.error("文件上传OSS失败:" + ex.getMessage());
return "";
} finally {
// 关闭OSSClient。
if (ossClient != null) {
ossClient.shutdown();
}
}
}
/**
* 删除
*
* @param OSSStorePath
*/
public static void Delete(String OSSStorePath) {
// 生成OSSClient
OSS ossClient = null;
try {
ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
//删除文件
ossClient.deleteObject(bucketName, OSSStorePath.replace(endpoint, ""));
} catch (Exception ex) {
log.error("OSS删除文件失败:" + ex.getMessage());
} finally {
// 关闭OSSClient。
if (ossClient != null) {
ossClient.shutdown();
}
}
}
/**
* oss附件下载
*
* @param OSSStorePath
* @param fileName
* @param response
*/
@GetMapping("/download")
public void download(HttpServletRequest request, HttpServletResponse response) {
String fileUrl = request.getParameter("fileUrl");
String fileName = request.getParameter("fileName");
if (StringUtils.isEmpty(fileUrl)) {
return;
}
if (StringUtils.isEmpty(fileName)) {
fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
}
try {
OSSUtils.download(fileUrl, fileName, response);
} catch (UnsupportedEncodingException e) {
throw new BizException("文件下载失败" + e.getMessage());
}
}
public static void download(String OSSStorePath, String fileName,
HttpServletResponse response) throws UnsupportedEncodingException {
response.setContentType("application/force-download");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
// 生成OSSClient
OSS ossClient = null;
InputStream bis = null;
OutputStream bos = null;
try {
ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
//下载文件
OSSObject object = ossClient.getObject(bucketName, OSSStorePath.replace(bucketUrl, ""));
bis = object.getObjectContent();
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[1024];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception ex) {
log.error("OSS文件下载失败:" + ex.getMessage());
} finally {
// 关闭OSSClient。
if (ossClient != null) {
ossClient.shutdown();
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
log.error("文件流关闭失败:" + e.getMessage());
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
log.error("文件流关闭失败:" + e.getMessage());
}
}
}
}
从oss获取文件并转换成字节数组
/**
*从oss获取文件并转换成字节数组
*
*/
public static byte[] downloadByte(String OSSStorePath) throws IOException {
// 生成OSSClient
OSS ossClient = null;
InputStream bis = null;
ByteArrayOutputStream bos = null;
byte[] bytes = null;
try {
ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
OSSObject object = ossClient.getObject(bucketName, OSSStorePath);
bis = object.getObjectContent();
bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
byte[] dataBytes;
while ((len = bis.read(buffer)) != -1 ) {
bos.write(buffer, 0, len);
}
bos.flush();
bytes = bos.toByteArray();
} catch (Exception ex) {
log.error("OSS文件下载失败:" + ex.getMessage());
} finally {
// 关闭OSSClient。
if (ossClient != null) {
ossClient.shutdown();
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
log.error("文件流关闭失败:" + e.getMessage());
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
log.error("文件流关闭失败:" + e.getMessage());
}
}
}
return bytes;
}