【问题标题】:how to generate a identified filename from url?如何从 url 生成识别的文件名?
【发布时间】:2012-05-22 21:26:10
【问题描述】:

现在我必须下载一个 url 已知的文件。下载操作完成后,我需要将其保存到 SD 卡中。问题是我应该在下载之前知道文件是否存在。因此,我计划使用从 url 生成的已识别文件名保存文件。所以当我得到网址时,我可以计算出他对应的文件名。我应该使用哪种算法?

顺便说一句,我使用的是 JAVA。

也许,我没有把我的要求说清楚。从网址“www.yahoo.com/abc.png”获取文件名“abc.png”不是我需要的。因为“www.google.com/abc.png”会产生相同的文件名。我需要从 url 生成一个唯一的文件名。

【问题讨论】:

    标签: java android


    【解决方案1】:

    完整的示例工作......我几天前尝试过自己...... 我相信它会有所帮助..

    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;
        }
    }
    

    【讨论】:

    • 我想你误会了。我知道如何从 url 下载文件。需要的是如何在下载完成后给出一个可识别的文件名。
    • 没有这样的方法...您需要知道您从url下载的文件名...在这种情况下您可以直接将该名称应用于文件..
    猜你喜欢
    • 2015-04-02
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    • 2021-11-22
    • 2014-02-28
    • 1970-01-01
    • 2017-03-19
    相关资源
    最近更新 更多