开发者

Java Load image from URLConnection

I have a URLConnection, that access a web page.

URL url = new URL("https://domain");
   con = url.openConnection();
   con.setDoOutput(true);

Then i sent some data to the server using con.setRequestProperty()

I get the response cookies fro ma specified field using

String headerValue = con.getHeaderField(6);

I also get the html and parse an image url from there. But here is a problem. I can get this image only by sending cache data back to the server ,when i acces my image.

So i open a new connection

URL url1 = new URL("https://domain/image); 
    URLConnection con1 = url1.openConnection();

I send the cookies back to the server con1.setRequestProperty("Cookie", headerValue);

And finally i try to acces the image using BufferedInputStream and then creating an iamge in a JLabel

BufferedInputStream in = new BufferedInputStream(con1.getInputStream());
       ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
       int c;
       while ((c = in.read()) != -1) {
         byteArrayOut.write(c);
       }


       Image image = Toolkit.getDefaultToolkit().createImage(
               byteArrayOut.toByteArray());

    label.setIcon(new ImageIcon(image));

The problem is this seems to not work. Is it another way to get an file from a server through a URlConnection? Error code

Server returned HTTP response code: 400 for URL: https://domain/image
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
开发者_运维技巧

Thanks in advance


Found the error. Case closed. Used this code to split the cookies string.

String temp = headerValue.substring(0, (len1-17));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜