代码之家  ›  专栏  ›  技术社区  ›  user5894647

即使提供了不同的URL,也可以下载相同名称的相同文件

  •  2
  • user5894647  · 技术社区  · 9 年前

    我有一个包含列表视图的活动,当通过onitemclicklistener中的intent点击不同的列表项时,它会发送不同的url 在单击列表项时打开的singleitemview活动中,我有一个下载图像的按钮

    问题是,即使通过不同的列表项点击提供了不同的URL,也只有1张图片被下载

    我认为这是因为只提供了一个这样的输出流

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

    如何更改此设置,以便每个不同的图像都以其自己的文件名下载

    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.

    singlitemview活动中下载按钮的onclick方法

    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);
    }
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   Vasily Kabunov Yashvir yadav    9 年前

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

    类似于:

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