0
class DownloadFileAsync extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub

            int count;
            String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" ;


            String[] fileName = params;
            String a = fileName.toString();
            String b = a.substring(20,25);
            destination+=b;


            try {

                URL url = new URL(params[0]);
                URLConnection conexion = url.openConnection();
                conexion.connect();

                int lengthofFile = conexion.getContentLength();
                Log.d("ANDRO_ASYNC", "Length of file: " + lengthofFile);

                InputStream input = new BufferedInputStream(url.openStream());
                OutputStream output = new FileOutputStream(destination);

                byte data[] = new byte[1024];

                long total = 0;

                while ((count = input.read(data)) != -1) {
                    total += count;
                    publishProgress("" + (int) ((total * 100) / lengthofFile));
                    output.write(data, 0, count);
                }

                output.flush();
                output.close();
                input.close();
            } catch (Exception e) {
                // TODO: handle exception
            }
            return null;
        }

my param variable will contain the url link which is "http://ledeveloper.in/ep123BCA.pdf" and i want to cut that url..the desire string which i want "ep123BCA.pdf".

But when i run this code the substring Method will always give me a different sub string how to solve that problem.

please help and thanx in advance.

2
  • 1
    use split method String[] parts= url.split("/"); it will split the url into 3 or 4 parts, last one is the file name , this is a one way Commented Jan 25, 2017 at 11:29
  • not working for me that give me a same garbage name of the downloaded file. :( Commented Jan 25, 2017 at 12:30

5 Answers 5

2

Use lastIndexOf to get the file name from the URL:

String b = a.substring(a.lastIndexOf('/') + 1);
Sign up to request clarification or add additional context in comments.

Comments

0

You can use this.

fileName=a.substring(a.lastIndexOf("/")+1);

Comments

0

Another solution is (if the first part of the url never change)

a.substring(22)

This should split the string this way:

the part cut out = http://ledeveloper.in/
a = ep123BCA.pdf

3 Comments

It is a solution, but it's not flexible enough.
That is most certainly correct :)
giving a garbage name of the file.
0
String url = "http://ledeveloper.in/ep123BCA.pdf";
         String file = url.substring(url.lastIndexOf('/')+1);

Comments

0
filename = a.substring(a.lastIndexOf("/")+1);

This line replace with your code.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.