码迷,mamicode.com
首页 > Web开发 > 详细

上传文件到阿里云OSS对象存储,查询访问地址,删除文件

时间:2018-08-31 19:18:42      阅读:1227      评论:0      收藏:0      [点我收藏+]

标签:获取文件   property   https   error   boolean   inpu   对象   jar   try   

 

一:pom添加以来jar

    <dependency>
        <groupId>com.aliyun.oss</groupId>
        <artifactId>aliyun-sdk-oss</artifactId>
        <version>2.8.3</version>
    </dependency>

 

  当然,也能下载jar导入项目;

二:关于文件上传的参数配置

新建 properties文件

技术分享图片

三:在业务逻辑层获取阿里云对象

  1:先获取属性文件

final static String endpoint = OssUtil.getConfig("contract");
    final static String accessKeyId = OssUtil.getConfig("accessKeyIdContract");
    final static String accessKeySecret = OssUtil.getConfig("accessKeySecretContract");
    final static String bucketName = OssUtil.getConfig("bucketName4C");

 

  2:属性文件的读取方法;

  

public class OssUtil {
    protected static Logger logger = LogManager.getLogger(OssUtil.class);

    public static String getConfig(String key) {
        String value = "";
        OssUtil propertiesUtil = new OssUtil();
        InputStream in = null;
        Properties props = new Properties();
        in = propertiesUtil.getClass().getResourceAsStream("/oss.properties");
        try {
            props.load(in);
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("getConfig io error:", e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e2) {
                    e2.printStackTrace();
                    logger.error("close io error:", e2);
                }
            }
        }
        value = props.getProperty(key);
        logger.info("property info :" + key + ":" + value);
        System.out.println(value);
        return value;
    }
    
}

 

四:判断你要上传的文件是不是存在

@Override
    public boolean existFileInOss(String fileName) {
        // TODO Auto-generated method stub
        // 创建OSSClient实例
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        // Object是否存在
        boolean found = ossClient.doesObjectExist(bucketName, fileName);
        // 关闭client
        ossClient.shutdown();
        return found;
    }

 

五:上传方法

    @Override
    public void uploadFile(MultipartFile file,String fileId) {
        //String fileName =file.getOriginalFilename(); 
        // 创建OSSClient实例
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        // 上传文件流
        try {
            ossClient.putObject(bucketName, fileId, file.getInputStream());
        } catch (OSSException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 关闭client
        ossClient.shutdown();
    }

 

六:获取文件访问地址

@Override
    public String getFileUrl(String fileId) {
        //服务器端生成url签名字串
        OSSClient Server  = new OSSClient(endpoint,  accessKeyId, accessKeySecret);
        Date expiration = null;
        expiration = new Date(System.currentTimeMillis()+1800000);
        //logger.debug("请求的fileid: " + fileId);
        GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, fileId, HttpMethod.GET);
        //设置过期时间
        request.setExpiration(expiration);
        // 生成URL签名(HTTP GET请求)
        URL signedUrl = Server.generatePresignedUrl(request);
        //logger.debug("oss文件url: " + signedUrl);
        return signedUrl.toString();
    }

 

七:删除文件

// 删除文件
        //ossClient.deleteObject(bucketName, "文件的全路径,如:cia/merged.pdf");  

 

八:控制台接收文件,调用业务层

public @ResponseBody String Upload(HttpServletRequest request, HttpServletResponse response,
            BsPayBussiness bussinessm, @RequestParam("file") MultipartFile file) {
}

 

  

 

上传文件到阿里云OSS对象存储,查询访问地址,删除文件

标签:获取文件   property   https   error   boolean   inpu   对象   jar   try   

原文地址:https://www.cnblogs.com/GH0522/p/9567150.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!