首頁  >  問答  >  主體

java - 下载文件的时候 ,文件名中包含 中括号, 导致下载失败!

1.使用Xutil 进行的文件下载
2.只要文件中有 中括号,就会下载失败,另起一个名字, 可以下载成功

爆出的异常为:

java.net.URISyntaxException: Illegal character in path at index 68...
at libcore.net.UriCodec.validate(UriCodec.java:63)
at java.net.URI.parseURI(URI.java:402)

异常详细如下:

08-16 10:06:50.183 31228-31228/ E/URIBuilder.<init>(L:55): Illegal character in path at index 68: http://***cn:8099/sczh//upload/5f534d87a3e148048b275ea5858f27f9[202]再试一下.doc
                                                                              java.net.URISyntaxException: Illegal character in path at index 68: http://**h//upload/5f534d87a3e148048b275ea5858f27f9[202]再试一下.doc
                                                                                  at libcore.net.UriCodec.validate(UriCodec.java:63)
                                                                                  at java.net.URI.parseURI(URI.java:402)
                                                                                  at java.net.URI.<init>(URI.java:204)
                                                                                  at com.lidroid.xutils.http.client.util.URIBuilder.<init>(URIBuilder.java:53)
                                                                                  at com.lidroid.xutils.http.client.HttpRequest.setURI(HttpRequest.java:156)
                                                                                  at com.lidroid.xutils.http.client.HttpRequest.<init>(HttpRequest.java:60)
                                                                                  at com.lidroid.xutils.HttpUtils.download(HttpUtils.java:297)
                                                                                  at com.lidroid.xutils.HttpUtils.download(HttpUtils.java:263)
                                                                                  at com.fenghua.traffic.activity.GongGaoInfoActivity.onClick(GongGaoInfoActivity.java:193)
                                                                                  at android.view.View.performClick(View.java:5200)
                                                                                  at android.view.View$PerformClick.run(View.java:21163)
                                                                                  at android.os.Handler.handleCallback(Handler.java:739)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                  at android.os.Looper.loop(Looper.java:148)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5459)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
08-16 10:06:50.187 31228-31650/com.fenghua.traffic E/RetryHandler.retryRequest(L:91): retry error, curr request is null
08-16 10:06:50.199 31228-31228/com.**.**E/error: com.lidroid.xutils.exception.HttpException: java.io.IOException: Target host must not be null, or set in parameters. scheme=null, host=null, path=--java.io.IOException: Target host must not be null, or set in parameters. scheme=null, host=null, path=
阿神阿神2763 天前1255

全部回覆(3)我來回復

  • 天蓬老师

    天蓬老师2017-04-18 09:49:20

    Illegal character in path at index 68: http://**h//upload/5f534d87a3e148048b275ea5858f27f9[202]再试一下.doc

    URIEncoder 肯定可以解決這個異常。
    樓主在編碼之後如果還出現錯誤,請仔細檢查錯誤訊息噢。就比如說上面的URL是否真的完全編碼。

    回覆
    0
  • 阿神

    阿神2017-04-18 09:49:20

    URL裡面特殊字元要做轉義的,例如用URLEncoder

    回覆
    0
  • 高洛峰

    高洛峰2017-04-18 09:49:20

    最主要就是這幾句啦,如果你手機訪問還是出錯,那麼基本可以肯定是手機瀏覽器的緩存了,你試著在地址後面跟上一個隨機字符串再測試一下。

    response.setHeader("Content-Type","application/octet-stream; charset=UTF-8");
    String filepath = "/var/www/web/upload/5f534d87a3e148048b275ea5858f27f9[202]再试一下.doc";
    File file = new File(filepath);
    String filename = URLEncoder.encode(file.getName(), "utf-8");
    response.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");

    回覆
    0
  • 取消回覆