具有相同名称的相同文件即使在提供不同的URL时也被下载 [英] Same file with same name getting downloaded even when provided with different URLs

查看:101
本文介绍了具有相同名称的相同文件即使在提供不同的URL时也被下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含列表视图的活动,并通过意图在onitemclicklistener中单击不同的列表项发送不同的url
在单击列表项目时打开的单一视图活动中,我有一个按钮下载图像

I have a activity that contains a listview and sends different url when different list item is clicked via intent in onitemclicklistener In the singleitemview activity which is opened when list item is clicked i have a button to download images

问题是只有1个图像被下载甚至不同的网址通过不同的列表项点击提供

The problem is only 1 image is getting downloaded even different URLs are provided through different list item clicks

我认为这正在发生,因为只提供一个这样的输出流

I think this is happening because of providing only 1 outputstream like this

                OutputStream output = new FileOutputStream("/sdcard/download/loadedfile.png");

如何更改这个,以便每个不同的图像都使用自己的文件名下载

how to change this so each different image gets downloaded with its own file name

listview的onitemclicklistener

public void onItemClick(AdapterView<?> p1, View view, int position, long p4)
{

       Intent intent = new Intent(ProActivity.this, SingleItemView.class);
      intent.putExtra("download",
                        (codelist.get(position).getDownloadCode()));
              staetActivity(intent);
      }

我的下载课程

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



    /**
     * Before starting background thread
     * Show Progress Bar Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        showDialog(progress_bar_type);
    }

    /**
     * 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();
            // this will be useful so that you can show a tipical 0-100% progress bar
            int lenghtOfFile = conection.getContentLength();

            // download the file
            InputStream input = new BufferedInputStream(url.openStream(), 8192);

            // Output stream
            OutputStream output = new FileOutputStream("/sdcard/download/loadedfile.png");

            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                // After this onProgressUpdate will be called
                publishProgress(""+(int)((total*100)/lenghtOfFile));

                // writing data to file
                output.write(data, 0, count);
            }

            // flushing output
            output.flush();

            // closing streams
            output.close();
            input.close();

        } catch (Exception e) {
            //e.printStackTrace();

            Log.e("Error: ","Error Message ::" +  e.getMessage());


        }

        return null;
    }

    /**
     * Updating progress bar
     * */
    protected void onProgressUpdate(String... progress) {
        // setting progress percentage
        pDialog.setProgress(Integer.parseInt(progress[0]));
    }

    /**
     * After completing background task
     * Dismiss the progress dialog
     * **/
    @Override
    protected void onPostExecute(String downurl) {
        // dismiss the dialog after the file was downloaded


        dismissDialog(progress_bar_type);

        Toast.makeText(SingleItemView.this,
                       getString(R.string.download_complete),
                       Toast.LENGTH_SHORT).show();


    }

}

1

onclick方法为我的下载按钮在singlitemview活动

Intent i = getIntent();
file_url = i.getStringExtra("download");
public void downloadnow(){

  // different url is recived from different list item click via intent
    String downurl = file_url;

    new DownloadFileFromURL().execute(downurl);
}


推荐答案

尝试更改文件名 loadedfile.png 下载文件名。

Try to change file name loadedfile.png to downloaded file name.

如下所示:

String fileName = url.substring( url.lastIndexOf('/')+1, url.length() );
OutputStream output = new FileOutputStream("/sdcard/download/" + filename);

这篇关于具有相同名称的相同文件即使在提供不同的URL时也被下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆