SpringBoot上传文件到阿里云OSS
Posted killer-leon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot上传文件到阿里云OSS相关的知识,希望对你有一定的参考价值。
/**
* 方法描述:文件上传
*
* @author leon 2017年7月25日 上午10:54:37
* @param request
* @param response
* @return
*/
@RequestMapping(value = "/upload", method = RequestMethod.POST )
@ResponseBody
public ResultModel<List<String>> upload(HttpServletRequest request, HttpServletResponse response)
ResultModel<List<String>> resultModel = new ResultModel<List<String>>();
try
// 创建一个通用的多部分解析器
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
List<String> files = new ArrayList<>();
// 判断 request 是否有文件上传,即多部分请求
if (multipartResolver.isMultipart(request))
// 转换成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
// 取得request中的所有文件名
Iterator<String> iter = multiRequest.getFileNames();
while (iter.hasNext())
// 取得上传文件
MultipartFile file = multiRequest.getFile(iter.next());
if (file != null)
// 取得当前上传文件的文件名称
String fileName = file.getOriginalFilename();
// 如果名称不为空,说明该文件存在,否则说明该文件不存在
if (fileName.trim() != "")
File newFile = new File(fileName);
FileOutputStream outStream = new FileOutputStream(newFile); // 文件输出流用于将数据写入文件
outStream.write(file.getBytes());
outStream.close(); // 关闭文件输出流
file.transferTo(newFile);
// 上传到阿里云
files.add(AliosSUtil.upload(newFile));
newFile.delete();
resultModel.setReturnValue(files);
catch (ValidateException e)
resultModel.setSuccessed(false);
resultModel.setErrorCode(e.getMessage());
catch (Exception e)
resultModel.setSuccessed(false);
resultModel.setErrorCode("system error");
log.error(e.getMessage());
e.printStackTrace();
return resultModel;
public class AliOSSUtil
private final static Logger log = Logger.getLogger(AliOSSUtil.class);
private static String END_POINT = "oss-cn-shanghai.aliyuncs.com";
private static String ACCESS_KEY_ID = "xxx";
private static String ACCESS_KEY_SECRET = "xxx";
private static String BUCKET_NAME = "xxx-image";
// 文件访问域名
private static String FILE_HOST = "https://file.xxx.com/";
/**
* 方法描述:上传文件
*
* @author leon 2016年12月16日 上午11:40:34
* @param file 文件对象
* @return
*/
public static String upload(File file)
if (file == null)
return null;
// 创建OSS客户端
OSSClient ossClient = new OSSClient(END_POINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
try
// 判断文件容器是否存在,不存在则创建
if (!ossClient.doesBucketExist(BUCKET_NAME))
ossClient.createBucket(BUCKET_NAME);
CreateBucketRequest createBucketRequest = new CreateBucketRequest(BUCKET_NAME);
createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
ossClient.createBucket(createBucketRequest);
// 创建文件路径
String fileUrl = DateUtil.dateTo8String2(new Date()) + "/" + UuidUtil.getUuidByJdk(false);
// 上传文件
PutObjectResult result = ossClient.putObject(new PutObjectRequest(BUCKET_NAME, fileUrl, file));
if (null != result)
return FILE_HOST + fileUrl;
catch (OSSException oe)
log.error(oe.getMessage());
catch (ClientException ce)
log.error(ce.getMessage());
finally
// 关闭OSS服务,一定要关闭
ossClient.shutdown();
return null;
/**
* 方法描述:上传文件
*
* @author leon 2016年12月26日 下午3:33:13
* @param inputStream 文件流
* @return
* @throws FileNotFoundException
*/
public static String upload(InputStream inputStream) throws FileNotFoundException
if (inputStream == null)
return null;
// 创建OSS客户端
OSSClient ossClient = new OSSClient(END_POINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
try
// 判断文件容器是否存在,不存在则创建
if (!ossClient.doesBucketExist(BUCKET_NAME))
ossClient.createBucket(BUCKET_NAME);
CreateBucketRequest createBucketRequest = new CreateBucketRequest(BUCKET_NAME);
createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
ossClient.createBucket(createBucketRequest);
// 创建文件路径
String fileUrl = DateUtil.dateTo8String2(new Date()) + "/" + UuidUtil.getUuidByJdk(false);
// 上传文件
PutObjectResult result = ossClient.putObject(new PutObjectRequest(BUCKET_NAME, fileUrl, inputStream));
if (null != result)
return FILE_HOST + fileUrl;
catch (OSSException oe)
log.error(oe.getMessage());
catch (ClientException ce)
log.error(ce.getMessage());
finally
// 关闭OSS服务,一定要关闭
ossClient.shutdown();
return null;
maven依赖的jar包
<!-- https://mvnrepository.com/artifact/com.aliyun.oss/aliyun-sdk-oss -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.aliyun.oss/aliyun-sdk-oss -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
以上是关于SpringBoot上传文件到阿里云OSS的主要内容,如果未能解决你的问题,请参考以下文章
springboot+vue+elementui+阿里云oss上传文件
springboot+vue+elementui+阿里云oss上传文件
vue+Springboot上传oss阿里云并回显到前端页面