简体   繁体   中英

how to generate a identified filename from url?

Now I have to download a file whose url has known. I need to save it to SD card when download action finished. The problem is I should know whether the file is existed before downloading. So I plan to save the file with a identified filename which is generated from url. So when I get the url I can calculate his corresponding filename. Which algorithm should I use?

BTW, JAVA is what I'm using.

Maybe, I have not told my requirement clearly. Fetch the filename "abc.png" from url "www.yahoo.com/abc.png" is not what I need. Because "www.google.com/abc.png" results the same filename. I need to generate a unique filename from url.

full example working ...i tried myself some days ago.. im sure it will help..

package com.imagedownloader;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

public class ImageDownloaderActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Bitmap bitmap=DownloadImage("http://www.allindiaflorist.com/imgs/arrangemen4.jpg");
        ImageView img =(ImageView)findViewById(R.id.imageView1);
        img.setImageBitmap(bitmap);

    }


    private Bitmap DownloadImage(String URL) {
        // TODO Auto-generated method stub
        Bitmap bitmap=null;
        InputStream in=null;
        try {

        in=OpenHttpConnection(URL);
        bitmap=BitmapFactory.decodeStream(in);

            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return bitmap;
    }

    private InputStream OpenHttpConnection(String stingurl) throws IOException {
        // TODO Auto-generated method stub
        InputStream in=null;
        int response=-1;

            URL url = new URL(stingurl);
            URLConnection conn=url.openConnection();



            if(!(conn instanceof HttpURLConnection))
                    throw new IOException("not and http exception");

            try{

                HttpURLConnection httpconn=(HttpURLConnection)conn;
                httpconn.setAllowUserInteraction(false);
                httpconn.setInstanceFollowRedirects(true);
                httpconn.setRequestMethod("GET");
                httpconn.connect();

                response=httpconn.getResponseCode();
                if(response==HttpURLConnection.HTTP_OK)
                {
                    in=httpconn.getInputStream();

                }

            }
            catch(Exception ex)
            {throw new IOException("Error connecting");   }
        return in;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM