Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用HttpsURLConnection获取图像的GET调用_Java_Rest - Fatal编程技术网

Java 使用HttpsURLConnection获取图像的GET调用

Java 使用HttpsURLConnection获取图像的GET调用,java,rest,Java,Rest,我正在调用一些外部web服务,我能够使用以下代码成功获取JSON响应: StringBuffer response = new StringBuffer(); URL obj = new URL(rest_url); HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); con.setDoOutput(true); con.setRequestMethod("GET"); BufferedReader br

我正在调用一些外部web服务,我能够使用以下代码成功获取JSON响应:

StringBuffer response = new StringBuffer();
URL obj = new URL(rest_url); 
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

con.setDoOutput(true);
con.setRequestMethod("GET");

BufferedReader br = new BufferedReader(new InputStreamReader(
        (con.getInputStream())));

while ((output = br.readLine()) != null) {
    response.append(output);
}
现在,我有一个外部web服务,它返回一个图像


如何使用HttpsURLConnection进行GET调用以获取此图像?

您可以使用下面的方法从url保存图像

public static void saveImageFromURL(String imageUrl, String destinationFile) throws IOException {
    URL url = new URL(imageUrl);
    InputStream is = url.openStream();
    OutputStream os = new FileOutputStream(destinationFile);

    byte[] b = new byte[4096];
    int length;

    while ((length = is.read(b)) != -1) {
        os.write(b, 0, length);
    }

    is.close();
    os.close();
}

希望能有所帮助。

外部web服务有什么问题?请您发布您试图访问该服务的确切源代码好吗?您好,web服务没有问题。它现在返回一个PNG图像而不是文本。所以我只想知道我是否需要在我的代码中做任何不同的事情来获取图像。你好!如果服务通过HTTPS公开其资源,并且不需要任何身份验证、授权等,那么提供的源代码应该可以正常工作。但是代码将数据读入字符串(
StringBuffer
)。为什么不将数据作为字节数组读取?准则的最终目的是什么?要获取文件并将其保存到本地文件系统,只需编写
FileWriter=newfilewriter(“file_path.png”)
并将内容写入文件
writer.write(response.toString())
并关闭它
writer.close()谢谢大家的帮助。Toukea Tatsi给出的答案非常有效。
    public void fetchImage() throws IOException{
                 URL obj = new URL(rest_url); 
                 HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

                 con.setDoOutput(true);
                 con.setRequestMethod("GET");
                 //specify your image file destination path
                 File destinationFile=new File("/destinationPath");
                 FileOutputStream outputStream=new FileOutputStream(destinationFile);
                 InputStream inputStream=con.getInputStream();
                 //start stream copy using a buffersize. here i am using a beffer size=1024
                 copyStream(inputStream, outputStream, 1024);
                 outputStream.close();
                 inputStream.close();
            }   

    /*copy an inputStream to an OutputStream using a specific buffer size*/
    public static OutputStream copyStream(InputStream is, OutputStream os, int buffer_size) throws IOException{


                            byte[] bytes = new byte[buffer_size];

                            for (; ; ) {
                                // Read byte from input stream

                                int count = is.read(bytes, 0, buffer_size);
                                if (count == -1)
                                    break;

                                // Write byte to output stream
                                os.write(bytes, 0, count);
                            }

                        return os;
                    }