简体   繁体   English

如何从URL生成一个识别的文件名?

[英]how to generate a identified filename from url?

Now I have to download a file whose url has known. 现在,我必须下载一个URL已知的文件。 I need to save it to SD card when download action finished. 下载操作完成后,我需要将其保存到SD卡。 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. 因此,我计划使用从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. 顺便说一句,JAVA是我正在使用的。

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. 我不需要从网址“ www.yahoo.com/abc.png”中获取文件名“ abc.png”。 Because "www.google.com/abc.png" results the same filename. 因为“ www.google.com/abc.png”会产生相同的文件名。 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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