存储在HTTPS服务器上的图像,如何找回它们? [英] Images stored on a HTTPS server, how to retrieve them?

查看:170
本文介绍了存储在HTTPS服务器上的图像,如何找回它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我用来访问样本的jpg文件的样本code,但连接后,我没有能看到文件的长度也不是下载的SD卡文件!(ISN T accessign HTTP连接和HTTPS引黄联接一样吗?)

Below is a sample code that I use to access a sample jpg file on , but after connection, I don't get to see the length of the file nor is the file downloaded on the sdcard!.(isn't accessign HTTP connection and HTTPS connetion same?)

下面是我使用示例code。 URL =htt​​ps://calomel.org/calomel_footer.jpg

Here is the sample code that I use. url = "https://calomel.org/calomel_footer.jpg"

private Bitmap getImageBitmap(String url) {
    Bitmap bm = null;
    try {
        URL aURL = new URL(url);
        URLConnection conn = aURL.openConnection();
        Log.i("My_App", "Content Type = "+URLConnection.guessContentTypeFromName(url));
        conn.connect();
        Log.i("My_App", "Content Length = "+conn.getContentLength());
        Log.i("My_App", "Content Type = "+conn.getContentType());
        FileOutputStream fos = new FileOutputStream(new File("/mnt/sdcard/calomel_footer.jpg"));
        InputStream is = conn.getInputStream();
        byte buf[]=new byte[1024];
        int len;
        while((len=is.read(buf))>0) {
            fos.write(buf,0,len);
        }
        fos.close();
        BufferedInputStream bis = new BufferedInputStream(is);
        bm = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();

   } catch (Exception e) {
       Log.e("My_App", "Error getting bitmap", e);
   }
   return bm;
}

和我得到显示为-1和内容类型2为空的lenght。

and I get the lenght displayed as -1 and Content Type2 as null.

此外,当我看到我的文件存储在/mnt/sdcard/calomel_footer.jpg我能看到0字节的文件。

Also when I see my file stored under the "/mnt/sdcard/calomel_footer.jpg" I get to see 0 bytes file.

谁能帮我解决?

推荐答案

首先,在图像 https://开头calomel.org/calomel_footer.jpg 不加载我。我看到一个消息,说[图像https://calomel.org/calomel_footer.jpg无法显示,因为它包含错误。]。

First off, the image at https://calomel.org/calomel_footer.jpg does not load for me. I see a message saying [The image "https://calomel.org/calomel_footer.jpg" cannot be displayed because it contains errors.].

其次,你都可以从网络服务器的输入流,而你正在写一个通过文件输出流。
做到这一点之后,你正试图再次读取相同的Web服务器的输入流(你是通过BitmapFactory做)。这些流是唯一的好一次。你想要做的是一样的东西:

Secondly, you are getting the input stream from the webserver, and you are writing that through the file output stream. After that is done, you are attempting to read the same webserver input stream again (which you are doing through the BitmapFactory). These streams are only good once. What you want to do is something like:

while((len=is.read(buf))>0) {
        fos.write(buf,0,len);
    }
    fos.close();
    FileInputStream fis = new FileInputStream(myFile); // myFile is the File that you wrote the FileOutputStream to.
    BufferedInputStream bis = new BufferedInputStream(fis);
    bm = BitmapFactory.decodeStream(bis);

这篇关于存储在HTTPS服务器上的图像,如何找回它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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