Springboot图片上传服务器快速搭建(接入阿里云)

Springboot图片上传服务器快速搭建(接入阿里云)

POM

<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>

创建阿里云参数

public class ossConfig {
	//这里建议使用参数文件的形式,这里只是方便就好
    public static String endpoint = "http://oss-cn-xxxxxx.aliyuncs.com";
    public static String accessKeyId = "xxxxxx";
    public static String accessKeySecret = "xxxxxxxx";
    public static String bucketName = "存储空间名称";
    public static String fileHost = "存储路径";
}

创建上传工具类


public class AliyunOSSUtil {
    public static String upload(File file){
        String endpoint=ossConfig.endpoint;
        String accessKeyId=ossConfig.accessKeyId;
        String accessKeySecret=ossConfig.accessKeySecret;
        String bucketName=ossConfig.bucketName;
        String fileHost=ossConfig.fileHost;
 
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = format.format(new Date());
 
        if(null == file){
            return null;
        }
 
        OSSClient ossClient = new OSSClient(endpoint,accessKeyId,accessKeySecret);
        try {
            //容器不存在,就创建
            if(! ossClient.doesBucketExist(bucketName)){
                ossClient.createBucket(bucketName);
                CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
                createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
                ossClient.createBucket(createBucketRequest);
            }
            //创建文件路径
            String fileUrl = fileHost+"/"+(dateStr + "/" + UUID.randomUUID().toString().replace("-","")+"-"+file.getName());
            //上传文件
            PutObjectResult result = ossClient.putObject(new PutObjectRequest(bucketName, fileUrl, file));
            //设置权限 这里是公开读
            ossClient.setBucketAcl(bucketName,CannedAccessControlList.PublicRead);
            if(null != result){
                return fileUrl;
            }
        }catch (OSSException oe){
            System.out.println(oe.getMessage());
        }catch (ClientException ce){
            System.out.println(ce.getMessage());
        }finally {
            //关闭
            ossClient.shutdown();
        }
        return null;
    }

}

创建接口调用

@RestController
@RequestMapping("upload")
public class upLoadController {
    /**
     * 文件上传
     * @param file
     */
    @RequestMapping(value = "uploadBlog",method = RequestMethod.POST)
    public String uploadBlog(MultipartFile file){
        String uploadUrl = null;
        try {
            if(null != file){
                String filename = file.getOriginalFilename();
                if(!"".equals(filename.trim())){
                    File newFile = new File(filename);
                    FileOutputStream os = new FileOutputStream(newFile);
                    os.write(file.getBytes());
                    os.close();
                    file.transferTo(newFile);
                    //上传到OSS
                    uploadUrl= AliyunOSSUtil.upload(newFile);
                }
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        //返回路径回前端
        return  uploadUrl;
    }
}

你可能感兴趣的:(Springboot图片上传服务器快速搭建(接入阿里云))