HTTP URL Address Encoding in Java

前端 未结 26 1363
醉酒成梦
醉酒成梦 2020-11-22 01:35

My Java standalone application gets a URL (which points to a file) from the user and I need to hit it and download it. The problem I am facing is that I am not able to encod

26条回答
  •  粉色の甜心
    2020-11-22 02:18

    I develop a library that serves this purpose: galimatias. It parses URL the same way web browsers do. That is, if a URL works in a browser, it will be correctly parsed by galimatias.

    In this case:

    // Parse
    io.mola.galimatias.URL.parse(
        "http://search.barnesandnoble.com/booksearch/first book.pdf"
    ).toString()
    

    Will give you: http://search.barnesandnoble.com/booksearch/first%20book.pdf. Of course this is the simplest case, but it'll work with anything, way beyond java.net.URI.

    You can check it out at: https://github.com/smola/galimatias

提交回复
热议问题