如何从网络上下载图像时处理URL空? [英] How to handle empty space in url when downloading image from web?

查看:867
本文介绍了如何从网络上下载图像时处理URL空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个项目,该URL有时可以有空格中它(并不总是)例如:www.google.com/例如/ test.jpg放在有时www.google.com/example/test.jpg

I'm working on a project where the url sometimes can have empty spaces in it (not always) example: www.google.com/ example/test.jpg and sometimes www.google.com/example/test.jpg.

我的code:

     try {
            URL url = new URL(stringURL);
            URLConnection conexion = url.openConnection();
            conexion.connect();

            // downlod the file
            InputStream input = new BufferedInputStream(url.openStream(), 8192);
            OutputStream output = new FileOutputStream(fullPath.toString());

            byte data[] = new byte[1024];

            while ((count = input.read(data)) != -1) {
                output.write(data, 0, count);
            }
            output.flush();
            output.close();
            input.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

这是此行失败:InputStream的输入=新的BufferedInputStream(url.openStream(),8192);

It's this line that fails: InputStream input = new BufferedInputStream(url.openStream(), 8192);

有:java.io.FileNotFoundException

with a: java.io.FileNotFoundException.

我trye​​d为en code中的具体线路,但这里是踢球:服务器需要以找到该文件的空白,所以我需要有空莫名其妙。我能找到的文件(jpg),如果我使用Firefox浏览器。
任何帮助非常AP preciated。

I've tryed to encode the specific line but here is the kicker: the server needs the empty space " " in order to find the file, so I need to have the empty space somehow. I can find the file (jpg) if i use firefox browser. Any help much appreciated.

编辑更新:
现在好了,我已经tryed为en code中的URL的主机部分为UTF-8后,每单位和我同时使用+和20%的空白tryed。现在我可以管理DL的文件,但它会是错误的,因此它不能被读取。

Edit update: Well now I've tryed to encode every single bit after the host part of the url to utf-8 and I've tryed using both + and %20 for blank space. Now I can manage to DL the file but it will be faulty so it can't be read.

编辑UPDATE2:
我现在已经把20%的错误,即工作。

Edit update2: I had made a mistake with %20, that works.

推荐答案

好吧,我解决了这个头痛的问题。

Okay I solved the headache.

首先,我用:

completeUrl.add(URLEncoder.encode(finalSeperated[i], "UTF-8"));

有关的网址的每一部分/

For every part of the url between "/"

然后我用:

    ArrayList<String> completeUrlFix = new ArrayList<String>();
    StringBuilder newUrl = new StringBuilder();
    for(String string : completeUrl) {
        if(string.contains("+")) {
            String newString = string.replace("+", "%20");
            completeUrlFix.add(newString);
        } else {
            completeUrlFix.add(string);
        }
    }

    for(String string : completeUrlFix) {
        newUrl.append(string);
    }

要建立一个适当的urlString。

To build a proper urlString.

这部作品的原因是因为HTTP需要20%。请参见故障百分号编码空间由Powerlord的Java 评论

The reason this works is because http needs %20. See Trouble Percent-Encoding Spaces in Java comment by Powerlord

这篇关于如何从网络上下载图像时处理URL空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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