0

I have a strange issue in downloading files in my android application all the files without space can be downloaded but when I have a space in my filename the file will not be downloaded for example:

Will not be download but this link:

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

will be downloaded:

http:..../DIV/POLITIQUE_QUALITE_V6.doc

This how I download file:

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;
    }

Any help would be appreciated

チーズパン
  • 2,752
  • 8
  • 42
  • 63
hamza437
  • 249
  • 1
  • 3
  • 12

1 Answers1

0

Petrus is right - you have to urlencode string like:

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

More ways to encode the string can be found at (my favourite one is without extra libraries): URL encoding in Android

Community
  • 1
  • 1
adomas.m
  • 383
  • 2
  • 12
  • i get the exception mal-formatted ... with URLEncoder the solution will be lien = lien.replaceAll(" ", "%20"); – hamza437 Apr 01 '16 at 10:12
  • %20 is urlencoded space symbol, so it totally makes sense, but I'd still go with some method of encoding it properly as there might be url's in the future of app use where it won't be only space, but some other non url allowed characters too. – adomas.m Apr 01 '16 at 10:15
  • the problem is that with urlencoder the spaces will be replaced by + so the solution with urlencoder will be URLEncoder.encode(link, "UTF-8").replace("+", "%20") – hamza437 Apr 01 '16 at 10:42
  • Try to add the proposed allowed chars like in the link provided: private static final String ALLOWED_URI_CHARS = "@#&=*+-_.,:!?()/~'%"; – adomas.m Apr 01 '16 at 10:53