Android 尝试从带有空格的URL下载PDF时的FileNotFoundExecution

Android 尝试从带有空格的URL下载PDF时的FileNotFoundExecution,android,url,Android,Url,我尝试使用以下代码下载并最终查看PDF文件。文件的URL如下所示: http://www.example.com/directory/something.example.com 此文件为.pdf 我尝试过用%20替换空格,我尝试过UrlEncoder.encode,无论我在编码URL时遇到什么FileNotFoundException或MalformedURLException。例外情况示例: java.io.FileNotFoundException: http://www.example.c

我尝试使用以下代码下载并最终查看PDF文件。文件的URL如下所示:

http://www.example.com/directory/something.example.com 此文件为.pdf

我尝试过用%20替换空格,我尝试过UrlEncoder.encode,无论我在编码URL时遇到什么FileNotFoundException或MalformedURLException。例外情况示例:

java.io.FileNotFoundException: http://www.example.com/directory/something.example.com 此文件为.pdf

java.io.FileNotFoundException: http://www.example.com/directory/something.example.com%20This%20File.pdf

java.net.MalformedURLException:未找到协议: http%3A%2F%2Fwww.example.com%目录%2Fsomething.example.com+This+File.pdf

如果我将这些路径复制到任何浏览器中,它都可以正常下载

        File file;
        try
        {
            String urlString = 
              "http://www.example.com/directory/something.example.com This File.pdf"
            URL url = new URL(urlString);
            //URL url = new URL(URLEncoder.encode(urlString, "UTF-8"));
            HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);
            urlConnection.connect();

            file = new File(getExternalFilesDir(null), "test.pdf");
            FileOutputStream fileOutput = new FileOutputStream(file);
            InputStream inputStream = urlConnection.getInputStream();

            byte[] buffer = new byte[1024 * 1024];
            int bufferLength;
            while ((bufferLength = inputStream.read(buffer)) > 0)
            {
                fileOutput.write(buffer, 0, bufferLength);
            }

            fileOutput.flush();
            fileOutput.close();
            inputStream.close();
            return file.getAbsolutePath();
        }
        catch (Exception e)
        {
            Log.e(getClass().getSimpleName(), e.getMessage(), e);
            return "";
        }
如果服务器响应404错误代码,将返回异常java.io.FileNotFoundException。有时错误代码和返回的数据不匹配。您可以通过以下方式检查并获取返回的任何数据:

boolean isError = urlConnection.getResponseCode() >= 400;
InputStream inputStream =  = isError ? urlConnection.getErrorStream() : urlConnection.getInputStream();
如果您正在连接到非标准端口,则可以通过向请求中添加以下标头来修复它:

urlConnection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
urlConnection.setRequestProperty("Accept","*/*");

你的尝试是错误的。以下是javadoc:

java.io.FileNotFoundException: 此文件为.pdf 空格未编码,因此与文件不匹配

java.io.FileNotFoundException: %20是有效的url符号,因此这也与您的文件不匹配

java.net.MalformedURLException:未找到协议: http%3A%2F%2Fwww.example.com%目录%2Fsomething.example.com+This+File.pdf com之后的斜杠编码不正确。导致错误编码的url

使用UrlEncoder,您应该对文件名进行编码并将其与URL连接起来,而不是对整个URL字符串进行编码并使用UTF-8编码。任何非UTF-8的产品都不能保证有效。 如果无法手动编码空间,则将其通过UrlEncoder传递。
您是否已将空格替换为+?当我在浏览器中尝试此操作时,它甚至不起作用。Chrome、Firefox等将空格替换为%20,这样就可以了。文件名是something.example.com This file.pdf?我猜您在实际代码中的字符串后面并没有缺少分号。此外,您发布的三个示例中都有错误。请参阅我的更新答案以了解问题所在。urlConnection.GetInputStream仅通过URLEncoder传递文件名会引发异常http://www.example.com/directory/something.example.com+这个+File.pdf文件服务器无法识别为该文件,因此我下载了一个错误页面。如果我不能控制服务器,我如何解决这个问题?这很奇怪。以前,当您传入错误的文件名时,会出现“未找到文件”异常。改变了什么?在我的3个示例中,除了案例3中错误编码的URL外,我从未传入过以“+”分隔的文件名。