Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从android应用程序下载文件_Android_Download - Fatal编程技术网

从android应用程序下载文件

从android应用程序下载文件,android,download,Android,Download,我在android应用程序中下载文件时遇到了一个奇怪的问题,所有没有空间的文件都可以下载,但当我的文件名中有空间时,文件将无法下载,例如: 将不会下载,但此链接: http:../DIV/Bon de Commande.pdf 将下载: http:../DIV/POLITIQUE_QUALITE_V6.doc 这是我下载文件的方式: protected String downloadfile(String... sUrl) { InputStream input = null;

我在android应用程序中下载文件时遇到了一个奇怪的问题,所有没有空间的文件都可以下载,但当我的文件名中有空间时,文件将无法下载,例如:

将不会下载,但此链接:

http:../DIV/Bon de Commande.pdf

将下载:

http:../DIV/POLITIQUE_QUALITE_V6.doc

这是我下载文件的方式:

protected String downloadfile(String... sUrl) {
        InputStream input = null;
        OutputStream output = null;
        HttpURLConnection connection = null;
        try {
            URL url = new URL(sUrl[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            // expect HTTP 200 OK, so we don't mistakenly save error report
            // instead of the file
            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                return "Server returned HTTP " + connection.getResponseCode()
                        + " " + connection.getResponseMessage();
            }

            // this will be useful to display download percentage
            // might be -1: server did not report the length
            int fileLength = connection.getContentLength();
            SharedPreferences myPreference= PreferenceManager.getDefaultSharedPreferences(getContext());
            String path=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/Document" ;
            String Fichename=sUrl[0].replace(myPreference.getString("lientelecharge", ""), "");
            String filePath=path+"/"+Fichename;
            File file = new File(filePath);
            if(file.exists()) {

            }else{
                // download the file
                input = connection.getInputStream();
                File folder = new File(path);
                boolean success = true;
                if (!folder.exists()) {
                    success = folder.mkdir();
                }


                output = new FileOutputStream(path+"/"+Fichename);

                byte data[] = new byte[4096];
                long total = 0;
                int count;
                while ((count = input.read(data)) != -1) {

                    total += count;
                    if (fileLength > 0) // only if total length is known
                    output.write(data, 0, count);
                }
            }

        } catch (Exception e) {
            return e.toString();
        } finally {
            try {
                if (output != null)
                    output.close();
                if (input != null)
                    input.close();
            } catch (IOException ignored) {
            }

            if (connection != null)
                connection.disconnect();
        }
        return null;
    }

任何帮助都将不胜感激

Petrus是正确的-您必须对字符串进行URL编码,如:

String addressToGo = URLEncoder.encode("www.123.com/55 U.doc", "utf-8");

可以在以下位置找到更多编码字符串的方法(我最喜欢的方法是没有额外的库):

将空格替换为%20,这样应该可以工作,或者您可以在下载文件时解码我们的路径。我想您可能需要对文件名进行URL编码。请参阅类URLEncoderi获取错误格式化的异常。。。使用URLEncoder,解决方案将是lien=lien.replaceAll(“,“%20”);%20是url编码的空间符号,所以它完全有意义,但我仍然会使用一些正确的编码方法,因为在应用程序使用的未来可能会有url,而不仅仅是空间,但也有一些不允许使用url的字符。问题是,使用urlencoder时,空格将被+替换,因此使用urlencoder的解决方案将是urlencoder.encode(链接,“UTF-8”)。替换(“+”,“%20”)尝试添加建议的允许字符,如提供的链接中所示:允许的私有静态最终字符串。\u urichars=“@#&=*+-,:!”/“%”;