Получить Загрузка Имя файла и расширение в Android

Я хочу скачать файлы с сервера, ссылки на мой сервер такие:

http://10.30.170.46/portal/fileLoader.php?code=351c4b27e3673c16557c231487997885


поэтому я не знаю, что это за имя и расширение файла! как я могу получить имя файла и расширение?
я загружаю свои файлы с этим кодом: Использование:

new DownloadFileFromURL().execute("http://10.30.170.46/portal/fileLoader.php?code=351c4b27e3673c16557c231487997885");


Учебный класс:

    class DownloadFileFromURL extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread
     * Show Progress Bar Dialog
     * */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @SuppressLint("NewApi")
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //showDialog(progress_bar_type);
        NotifInstaDown("Please Wait...", "Downloading");
    }

    /**
     * Downloading file in background thread
     * */
    @Override
    protected String doInBackground(String... f_url) {
        int count;
        try {
            URL url = new URL(f_url[0]);
            URLConnection conection = url.openConnection();
            conection.connect();
            int lenghtOfFile = conection.getContentLength();
            InputStream input = new BufferedInputStream(url.openStream(), 8192);

                    output = new FileOutputStream("/sdcard/Circulars/" + fileName);
                    byte data[] = new byte[1024];
                    long total = 0;
                    while ((count = input.read(data)) != -1) {
                        total += count;
                        publishProgress(""+(int)((total*100)/lenghtOfFile));
                        output.write(data, 0, count);
                    }
                    output.flush();
                    output.close();
                    input.close();
        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }

        return null;

    }

    /**
     * Updating progress bar
     * */
    protected void onProgressUpdate(String... progress) {
        // setting progress percentage
    }

    /**
     * After completing background task
     * Dismiss the progress dialog
     * **/
    @Override
    protected void onPostExecute(String file_url) {
        //dismissDialog(progress_bar_type);
        removeNotification();
    }
}

1 ответ

Попробуй это

            URL url;
            String filename = null;
            try {
                url = new URL("YOR_URL");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.connect();
            conn.setInstanceFollowRedirects(false); 

            try {
                for(int i = 0; i < 10; i++)
                {
                    url = new URL(conn.getHeaderField("Location")); 
                    conn = (HttpURLConnection) url.openConnection();
                    conn.connect();
                    conn.setInstanceFollowRedirects(false);
                }
            } catch (Exception e) {
            }

            String depo = conn.getHeaderField("Content-Disposition");
            String depoSplit[] = depo.split(";");
            int size = depoSplit.length;
            for(int i = 0; i < size; i++)
            {
                if(depoSplit[i].startsWith("filename="))
                {
                    filename = depoSplit[i].replace("filename=", "").replace("\"", "").trim();

                    i = size;
                }
            }
            } catch (MalformedURLException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
            }
Другие вопросы по тегам