java调用阿里云上传和下载(项目亲测)

pom文件导入依赖就不贴了,直接到官网贴最新的就行:https://help.aliyun.com/document_detail/32009.html?spm=a2c4g.11186623.6.800.a53c6d39J1Mz1D

  /**
     * 设定参数
     * */
    @Autowired
    private  LangContext langContext;

  	//这些数据都要改成自己的
    private  String endpoint = "http://oss-cn-beijing.aliyuncs.com";//阿里云地址
    private  String bucketName = "skyvis-spys";//存储在哪个文件夹
    private  String accessKeyId = "xxx";//
    private  String accessKeySecret = "xxx";//
    private String region = "oss-cn-beijing";//地区
    private String patsStyle =null;//下载地址???忘了

    /**
     * 文件上传
     * @param localUrl:本地文件路径
     * @param ossUrl:oss路径
     * */
    public  String SetFileOss(String localUrl, String ossUrl){
        try {
            log.info("进入oss上传方法,localUrl="+localUrl+",ossUrl="+ossUrl);
            // 创建OSSClient实例。
            log.info("参数endpoint=\""+endpoint+"\",accessKeyId=\""+accessKeyId+"\",accessKeySecret=\""
                    +accessKeySecret+"\",bucketName=\""+bucketName+"\""+"region=\""+region+"\""+",pats-style="+"\""+patsStyle+"\"");
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, ossUrl, new File(localUrl));
            // 上传文件
            ossClient.putObject(putObjectRequest);
            // 关闭OSSClient。
            ossClient.shutdown();
            //设置过期时间
            Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
            //获取存储url
            String url = ossClient.generatePresignedUrl(bucketName,ossUrl,expiration).toString();
            if (url != null && url != "") {
                log.info("上传成功,oss的url是:"+url);
                return url;
            } else {
                log.info("上传失败,oss的url是:"+url);
                return url;
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage(),e);
            return "400";
        }finally {
            log.info("离开oss方法");
        }
    }
    /**
     * 文件下载
     * @param localUrl:本地文件路径
     * @param ossUrl:oss路径
     * */
    public String downloadFile(String localUrl,String ossUrl){
        log.info("进入oss下载方法,localUrl="+localUrl+",ossUrl="+ossUrl);
        // 创建OSSClient实例。
        log.info("参数endpoint=\""+endpoint+"\",accessKeyId=\""+accessKeyId+"\",accessKeySecret=\""
                +accessKeySecret+"\",bucketName=\""+bucketName+"\""+"region=\""+region+"\""+",pats-style="+"\""+patsStyle+"\"");
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        //下载文件到本地
        try {
            ossClient.getObject(new GetObjectRequest(bucketName, ossUrl), new File(localUrl));
            return "200";
        }catch (Exception e){
            e.printStackTrace();
            log.error("异常:",e);
            return "400";
        }finally {
            ossClient.shutdown();
        }
    }

本人项目亲测可用

你可能感兴趣的:(java,java)