java实现阿里云OSS实现上传文件
阿里云OSS api上传文档链接--> 简单上传
配置文件application.properties
# oss
oss.accessKeyId=
oss.accessKeySecret=
oss.endpoint=oss-cn-shanghai.aliyuncs.com
oss.domain=http://mayikt-foodie.oss-cn-shanghai.aliyuncs.com
oss.bucket=mayikt-foodie
oss.objectName=foodie-dev/images
@Api(value = "用户信息接口", tags = {"用户信息相关接口"})
@RestController
@RequestMapping("userInfo")
public class CenterUserController extends BaseController {
private static final Logger LOG = LoggerFactory.getLogger(CenterUserController.class);
@Value("${oss.accessKeyId}")
private String accessKeyId;
@Value("${oss.accessKeySecret}")
private String accessKeySecret;
@Value("${oss.endpoint}")
private String endpoint;
@Value("${oss.bucket}")
private String bucket;
@Value("${oss.domain}")
private String ossDomain;
@Value("${oss.objectName}")
private String objectName;
public static final String BUSINESS_NAME = "文件上传";
@ApiOperation(value = "用户头像修改", notes = "用户头像修改", httpMethod = "POST")
@PostMapping("uploadFace")
public IMOOCJSONResult uploadFace(
@ApiParam(name = "userId", value = "用户id", required = true)
@RequestParam String userId,
@ApiParam(name = "file", value = "用户头像", required = true)
MultipartFile file,
HttpServletRequest request, HttpServletResponse response) throws Exception {
LOG.info("上传文件开始");
if (file != null) {
String fileName = file.getOriginalFilename();
LOG.info("fileName#####{}" + fileName);
if (StringUtils.isNoneBlank(fileName)) {
String fileNameArr[] = fileName.split("\.");
String suffix = fileNameArr[fileNameArr.length - 1];
if (!suffix.equalsIgnoreCase("png") &&
!suffix.equalsIgnoreCase("jpg") &&
!suffix.equalsIgnoreCase("jpeg")) {
return IMOOCJSONResult.errorMsg("图片格式不正确!");
}
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
InputStream inputStream = file.getInputStream();
String myObjectName = objectName + "/" + userId + "/" + userId + "." + suffix;
ossClient.putObject(bucket, myObjectName, inputStream);
ossClient.shutdown();
System.out.println("图片存储地址为:" + ossDomain + myObjectName);
}
}
return IMOOCJSONResult.ok();
}
}
以上是 java实现阿里云OSS实现上传文件 的全部内容, 来源链接: utcz.com/z/517809.html