在Android中下载文件名称和扩展名

时间:2017-02-25 07:26:59

标签: android filenames android-download-manager downloadfileasync

我想从服务器下载文件,我的服务器链接是这样的:

  

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 个答案:

答案 0 :(得分:0)

试试这个

            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) {
            }