简体   繁体   中英

%3d instead of = in file path, then i try to open file from resources

I write some tests and to get absolute path from relative path i use this function

    private def getAbsolutePath(filePath: String): String = {
        getClass.getResource(filePath).getFile
    } 

and then i do:

println(getAbsolutePath("/parquetIncrementalProcessor/withPartitioning/"))
println(getAbsolutePath("/parquetIncrementalProcessor/withPartitioning/own_loading_id=1/partition_column=test/"))

i get:

/Users/19658296/csp-fp-snaphot/library/target/scala-2.11/test-classes/parquetIncrementalProcessor/withPartitioning/
/Users/19658296/csp-fp-snaphot/library/target/scala-2.11/test-classes/parquetIncrementalProcessor/withPartitioning/own_loading_id%3d1/partition_column%3dtest/

As you can see, instead of =, I get some strange symbol. At the same time, when I try to read these files with a park, he can read the path without %3d, and with %3d he gets the error "Path does not exist".

How can I fix this?

Seems like its URL encoded, maybe because using stuff from files and resources are designed to work with Universal Resource Locators. You can URLDecode it like so:

import java.net.URLDecoder
def getAbsolutePath(filePath: String): String = {
    val path = getClass.getResource(filePath).getFile
    URLDecoder.decode(path, "UTF-8")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM