gradle读取绝对路径的文件中的配置参数

有时候我们为了保护一些密码或者密钥,会把生产的密码放在打包机的目录下,打包的时候再通过gradle读取,这样能很好的保护好密码或者密钥的流失!

新建fileConfigs.properties文件放在任意的目录下

#fileConfigs测试
FILE_CONFIGS_KEY=fileConfigsKey

 

 

在app的build.gradle 里面定义方法

def getPropertiesFromFile() {
    //可以使用绝对路径,也可以使用相对路径
    //  /Users/xuefeng.kuang/Documents/kxf/fileConfigs.properties
    //  E:\kxf\fileConfigs.properties
    def versionFile = file('../fileConfigs.properties')
    Properties properties = new Properties()
    if (versionFile.canRead()) {
        properties.load(new FileInputStream(versionFile))
    } else {
        properties['FILE_CONFIGS_KEY'] = "null! Could not find fileConfigs.properties!"
        //也可以直接抛异常
//        throw new GradleException("Could not find fileConfigs.properties!")
    }
    logger.quiet("properties==${properties}")
    return properties
}

def myProperties = getPropertiesFromFile()

 

使用时:

buildConfigField("String", "FILE_CONFIGS_KEY", "\"" + myProperties['FILE_CONFIGS_KEY'] + "\"")

 接着在代码中引用

Log.i("MainActivity", "FILE_CONFIGS_KEY== " + BuildConfig.FILE_CONFIGS_KEY);

输出日志

2021-09-06 17:52:18.873 22407-22407/com.kxf.androidtestdemo I/MainActivity: FILE_CONFIGS_KEY== fileConfigsKey

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
文件上传到阿里云对象存储(OSS)的Spring Boot应用程序,您可以按照以下步骤操作: 1. 首先,确保您已经在阿里云上创建了一个OSS存储桶,并且具有相应的访问密钥。 2. 在您的Spring Boot项目,添加阿里云Java SDK的依赖项。您可以在Maven或Gradle文件添加以下依赖项: ```xml <!-- Maven 依赖 --> <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.10.2</version> </dependency> ``` 3. 创建一个OSS客户端对象,并配置访问密钥、区域和存储桶名称。您可以在应用程序的配置文件存储这些配置信息,并在需要时读取它们。例如,在application.properties添加以下配置: ```properties aliyun.oss.accessKeyId=your-access-key-id aliyun.oss.accessKeySecret=your-access-key-secret aliyun.oss.endpoint=your-oss-endpoint aliyun.oss.bucketName=your-bucket-name ``` 4. 在您的Spring Boot应用程序创建一个文件上传接口或控制器。您可以使用Spring MVC或Spring WebFlux来处理文件上传请求。以下是一个使用Spring MVC的示例: ```java import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @RestController public class FileUploadController { @Value("${aliyun.oss.bucketName}") private String bucketName; @PostMapping("/upload") public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) { try { // 创建OSS客户端 OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 上传文件到OSS存储桶 ossClient.putObject(bucketName, file.getOriginalFilename(), file.getInputStream()); // 关闭OSS客户端 ossClient.shutdown(); return ResponseEntity.ok("File uploaded successfully!"); } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to upload file!"); } } } ``` 这个控制器将接收一个名为"file"的文件参数,并将其上传到指定的OSS存储桶。 5. 启动您的Spring Boot应用程序,并使用任何HTTP客户端(如Postman)发送文件上传请求。确保请求正确地指向您的上传接口,并携带一个名为"file"的文件参数文件将被上传到阿里云对象存储的指定存储桶。您可以根据自己的需求,进一步处理上传后的文件路径或其他操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值