使用OSS云存储上传文件
本帖最后由 wushaominkk 于 2019-7-12 14:13 编辑不多说,直接上代码:
这是我创建的OSS工具类:
@Component
public class OssUtils {
private static String accessKeyId;
private static String accessKeySecret;
private static String bucketName;
//外网
private static String bucketOuter;
//内网
private static String bucketIntranet;
private static String endpoint;
public static String ossdir;
private staticOSSClient ossClientStatic;
public OssUtils(@Value("${oss.accessKeyId}") String accessKeyId,
@Value("${oss.accessKeySecret}") String accessKeySecret,
@Value("${oss.bucketName}") String bucketName,
@Value("${oss.bucketOuter}")String bucketOuter,
@Value("${oss.bucketIntranet}")String bucketIntranet,
@Value("${oss.endpoint}") String endpoint,
@Value("${oss.ossdir}") String ossdir ) {
this.accessKeyId = accessKeyId;
this.accessKeySecret=accessKeySecret;
this.bucketName=bucketName;
this.bucketOuter=bucketOuter;
this.bucketIntranet=bucketIntranet;
this.endpoint=endpoint;
this.ossdir=ossdir;
ossClientStatic = new OSSClient(endpoint, accessKeyId, accessKeySecret);
}
/**
* 上传到OSS服务器 如果同名文件会覆盖服务器上的
*
* @Param fileName 文件名称 包括后缀名
* @param instream 文件流
* @Return 出错返回"" ,唯一MD5数字签名
*/
public static String upload(String fileName, InputStream instream) {
String resultStr = "";
try {
// 创建上传Object的Metadata
ObjectMetadata objectMetadata = new ObjectMetadata();
// 上传文件 (上传文件流的形式)
PutObjectResult putResult = ossClientStatic.putObject(bucketName, ossdir+fileName, instream, objectMetadata);
// 解析结果
resultStr = putResult.getETag();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (instream != null) {
instream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return resultStr;
}
}
下面是上传文件的接口
/**
* 文件上传接口
* @param request
* @return
*/
@PostMapping("/upload")
@ApiOperation(value = "上传文件", httpMethod = "POST", notes = "上传文件")
public JsonResult upload(@ApiParam(value="上传文件",required=true)HttpServletRequest request) {
try {
logger.info("**************上传文件开始 : [{}]::",request);
long startTime = System.currentTimeMillis();
List<String> files = new ArrayList<>();
logger.info("************** 创建解析器 *****************" );
CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
logger.info("************** 上传的文件内容 isMultipart*****************::[{}]::", resolver.isMultipart(request));
if (resolver.isMultipart(request)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
String path = StringUtils.appendIfMissing(StringUtils.stripEnd(StringUtils.trimToEmpty(request.getParameter("path")), "/"), "/");
Iterator iter = multiRequest.getFileNames();
while (iter.hasNext()) {
MultipartFile file = multiRequest.getFile(iter.next().toString());
if (file != null) {
InputStream inputStream = file.getInputStream();
String originalName = file.getOriginalFilename();
String extension = originalName.substring(originalName.lastIndexOf('.'));
String fileName = IdUtil.uuid() + extension;
OssUtils.upload(fileName,inputStream);
String url =OssUtils.ossdir+fileName;
files.add(url);
}
}
}
long endTime = System.currentTimeMillis();
logger.debug("**************文件上传耗时:{} ms", endTime - startTime);
return JsonResult.OK(files);
} catch (BuzEx e) {
logger.error("**************上传时发生异常***********::[{}]::", e.getMessage());
return new JsonResult(e.getCode().getCode(), null, e.getCode().getMessage());
} catch(Exception e) {
logger.error("**************上传时发生异常***********::[{}]::", e.getMessage());
return JsonResult.build(BuzCode.C_FILE_ERROR);
}
} 没看懂。。 看懂了一丢丢 有个问题呢 上传oss 那个bucketName是可以创建多个?
页:
[1]