Bootstrap

图片上传HTML

alioss

sky:
  jwt:
    # 设置jwt签名加密时使用的秘钥
    admin-secret-key: itcast
    # 设置jwt过期时间
    admin-ttl: 7200000
    # 设置前端传递过来的令牌名称
    admin-token-name: token
  alioss:
    endpoint: ${sky.alioss.endpoint}
    access-key-id: ${sky.alioss.access-key-id}
    access-key-secret: ${sky.alioss.access-key-secret}
    bucket-name: ${sky.alioss.bucket-name}

alioss

alioss:
  endpoint: oss-cn-beijing.aliyuncs.com
  access-key-id: LTAI5tD64jiY7ex4UJCVr5hp
  access-key-secret: eSpmy0Ke3rvMjNG8oTbMw3Zjy60HgK
  bucket-name: sky-itcast-ywq132

ossConfiguration

/**
 * 配置类,用于创建aliossutil对象
 */
@Configuration
@Slf4j
public class ossConfiguration {
    @Bean//将返回值交给ioc容器管理,成为ioc容器的bean对象
    @ConditionalOnMissingBean//保证容器只有一个对象
    public AliOssUtil aliOssUtil (AliOssProperties aliOssProperties) {
       log.info("开始创建阿里云文件上传工具对象,{}",aliOssProperties);
        return new AliOssUtil(aliOssProperties.getEndpoint()
                ,aliOssProperties.getAccessKeyId()
                ,aliOssProperties.getAccessKeySecret()
                ,aliOssProperties.getBucketName());


    }
}

CommonController

/**
 * 通用接口
 */
@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file) {
        log.info("文件上传: { }", file);
        try {
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //截取原始文件名后缀  jpg
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));

            //构建新的文件夹名称
            String objectName = UUID.randomUUID().toString() + extension;
            //文件夹的请求路径
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);

            return Result.success(filePath);
        } catch (IOException e) {
            log.error("文件上传失败");
        }
        return null;
    }
}
;