java 文件上传和下载 oss

话不多说 直接粘代码

先从 阿里云上下载下来对应的java的sdk

https://help.aliyun.com/document_detail/32008.html?spm=5176.87240.400427.45.84ec4614YTOtmw

 

简单的文件上传和下载

@Slf4j
public class UploadFile {

    @Value("${aliyun.endpoint}")
    private static String endpoint;
    @Value("${aliyun.accessKeyId}")
    private static String accessKeyId;
    @Value("${aliyun.accessKeySecret}")
    private static String accessKeySecret;
    @Value("${aliyun.bucketName}")
    private static String bucketName;
   /* public static RestResult enclosure(MultipartFile configuration,String uploadFileName){
        RestResult restResult=new RestResult();
        restResult.setSuccess(false);
        if (configuration.isEmpty()) {
            restResult.setCode("上传失败,请选择文件");
            return restResult;
        }
        try {
            String fileName = configuration.getOriginalFilename();
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
            String uploadDir= ResourceUtils.getURL("classpath:").getPath();
//+"static/"+uploadFileName+"/"
            File dest = new File(uploadDir);
            if(!dest.exists()){
                dest=new File("");
            }
            fileName=format.format(new Date())+fileName;
            File file=new File(dest.getAbsolutePath(),"static/"+uploadFileName+"/");

            if(!file.exists()){
                file.mkdirs();
            }
            File file1=new File(file,fileName);

            //保存 excel表
            configuration.transferTo(file1);
            //保存数据到excel表
            restResult.setSuccess(true);
            restResult.setCode(fileName);
            return restResult;
        } catch (Exception e) {
            log.info(e.getMessage());
        }
        restResult.setCode("上传失败!!!!!!!!");
        return restResult;
    }*/
/**
*
直接通过MultipartFile  进行上传

*/

    public static RestResult enclosure(MultipartFile configuration,String uploadFileName){
        RestResult restResult=new RestResult();
        restResult.setSuccess(false);
        if (configuration.isEmpty()) {
            restResult.setCode("上传失败,请选择文件");
            return restResult;
        }
        try {
            String fileName = configuration.getOriginalFilename();
            SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");

            fileName=format.format(new Date())+fileName;
//根据不同的时间创建不同的文件夹 文件名可以自己在设定 使用简单的时间的方式 防止覆盖
            String uploadDir= "static/"+uploadFileName+"/"+format.format(new Date())+"/"+fileName;
            //保存 excel表
            InputStream inputStream = configuration.getInputStream();
            //保存数据到excel表

            //上传数据到 阿里云服务器
            OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
            ossClient.putObject(new PutObjectRequest(bucketName, uploadDir, inputStream));
            Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
            //回显访问url
            String url = ossClient.generatePresignedUrl(bucketName, uploadDir, expiration).toString();
            restResult.setSuccess(true);
            restResult.setCode(url);
            return restResult;
        } catch (Exception e) {
            log.info(e.getMessage());
        }
        restResult.setCode("上传失败!!!!!!!!");
        return restResult;
    }


}

 

下载很简单 通过 url直接访问即可

 

 

你可能感兴趣的:(微服务)