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

这篇博客详细介绍了如何在Java项目中集成阿里云OSS SDK,包括pom.xml添加依赖,配置文件设置,读取属性文件,判断文件是否存在,上传文件,获取文件访问URL以及删除文件的操作步骤。同时,提供了业务逻辑层的方法实现。
摘要由CSDN通过智能技术生成

一:pom添加以来jar

com.aliyun.oss

aliyun-sdk-oss

2.8.3

当然,也能下载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 classOssUtil {protected static Logger logger = LogManager.getLogger(OssUtil.class);public staticString getConfig(String key) {

String value= "";

OssUtil propertiesUtil= newOssUtil();

InputStream in= null;

Properties props= newProperties();

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);returnvalue;

}

}

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

@Overridepublic booleanexistFileInOss(String fileName) {//TODO Auto-generated method stub//创建OSSClient实例

OSSClient ossClient = newOSSClient(endpoint, accessKeyId, accessKeySecret);//Object是否存在

boolean found =ossClient.doesObjectExist(bucketName, fileName);//关闭client

ossClient.shutdown();returnfound;

}

五:上传方法

@Overridepublic voiduploadFile(MultipartFile file,String fileId) {//String fileName =file.getOriginalFilename();//创建OSSClient实例

OSSClient ossClient = newOSSClient(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();

}

六:获取文件访问地址

@OverridepublicString getFileUrl(String fileId) {//服务器端生成url签名字串

OSSClient Server = newOSSClient(endpoint, accessKeyId, accessKeySecret);

Date expiration= null;

expiration= new Date(System.currentTimeMillis()+1800000);//logger.debug("请求的fileid: " + fileId);

GeneratePresignedUrlRequest request = newGeneratePresignedUrlRequest(bucketName, fileId, HttpMethod.GET);//设置过期时间

request.setExpiration(expiration);//生成URL签名(HTTP GET请求)

URL signedUrl =Server.generatePresignedUrl(request);//logger.debug("oss文件url: " + signedUrl);

returnsignedUrl.toString();

}

七:删除文件

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

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

public@ResponseBody String Upload(HttpServletRequest request, HttpServletResponse response,

BsPayBussiness bussinessm, @RequestParam("file") MultipartFile file) {

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值