上傳文件到阿里雲OSS對象存儲,查詢訪問地址,刪除文件

 

一:pom添加以來jar

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

 

  固然,也能下載jar導入項目;服務器

二:關於文件上傳的參數配置

新建 properties文件ide

三:在業務邏輯層獲取阿里雲對象

  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:屬性文件的讀取方法;url

  

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) {
}
相關文章
相關標籤/搜索