【问题标题】:How to get resources directory path using java如何使用java获取资源目录路径
【发布时间】:2019-03-12 08:13:15
【问题描述】:

我正在黄瓜中测试测试,我想将文件从 testData 上传到 S3 存储桶:

String bucket = bucketname+ "/ADL";
String ActualFilesPathForComparison = Environment.getInstance().getValue(DATAINPUTPATH);
temp = ActualFilesPathForComparison+inputPath+ File.separator+ file;
s3.uploadFile(bucket, file, new File (temp)); 

public void uploadFile(String bucketName, String fileKeyName, File localFilePath) {
    try {
        this.s3.putObject((new PutObjectRequest(bucketName, fileKeyName, localFilePath)).withCannedAcl(CannedAccessControlList.PublicRead));
    } catch (Exception var5) {
        throw new RuntimeException("Upload file failed.", var5);
    }
}

我有这个文件:

src\main\resources\testData\testInputsFile\testLZInputUnZippedFiles\Log.csv

当我运行测试时,我从调试中得到:

localFilePath = testData\testInputsFile\testLZInputUnZippedFiles\Log_WithHeader.csv

得到例外:

com.amazonaws.SdkClientException: Unable to calculate MD5 hash: testData\testInputsFile\testLZInputUnZippedFiles\Log_WithHeader.csv (The system cannot find the path specified)

我应该修复什么?我想避免从 src 复制文件。

【问题讨论】:

    标签: java amazon-s3 file-upload cucumber


    【解决方案1】:

    要访问src/main/resources/ 中名为“my.properties”的文件,您只需:

    File propertiesFile = new File(this.getClass().getClassLoader().getResource("my.properties").getFile());
    

    【讨论】:

    • 我收到错误 - 必需:java.io.File Found: java.lang.String
    • @Tamar 我错过了new File() 构造函数。查看更新的答案。
    猜你喜欢
    • 2016-09-26
    • 2013-10-25
    • 2013-01-06
    • 2011-04-24
    • 2013-01-01
    • 2020-12-09
    相关资源
    最近更新 更多