从套接字读取图像 [英] Read Image from Socket

查看:68
本文介绍了从套接字读取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
通过Java套接字读取图像文件

Possible Duplicate:
Read Image File Through Java Socket

void readImage() throws IOException
{
    socket = new Socket("upload.wikimedia.org", 80);


    DataOutputStream bw = new DataOutputStream(new DataOutputStream(socket.getOutputStream()));
    bw.writeBytes("GET /wikipedia/commons/8/80/Knut_IMG_8095.jpg HTTP/1.1\n");
    bw.writeBytes("Host: wlab.cs.bilkent.edu.tr:80\n\n");

    DataInputStream in = new DataInputStream(socket.getInputStream());

    File file = new File("imgg.jpg");
    file.createNewFile();
    DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
    int count;
    byte[] buffer = new byte[8192];
    while ((count = in.read(buffer)) > 0)
    {
      dos.write(buffer, 0, count);
      dos.flush();
    }
    dos.close();
    System.out.println("image transfer done");

    socket.close();     
}

-创建一个套接字 -创建输出流 -请求包含图像的页面 -将套接字读取到输入流 -写入文件

-Create a socket -Create output stream -Request the page that includes image -Read socket to an input stream -Write to file

我正在尝试从套接字读取图像. 但这不起作用.

I am trying to read an image from socket. But it is not working.

似乎可以读取并且图像已打开,但是看不到

It seems to read and the image is opened but can not be seen

问题出在哪里?

推荐答案

您需要跳过HTTP标头以获取正确的图像.

You need to skip HTTP headers to get correct image.

我今天已经回答了这个问题,请看:通过Java套接字读取图像文件

I've already answered to this question today, look at: Read Image File Through Java Socket

第二个问题是,您试图从Wikipedia接收没有引用者的图像,而Wikipedia限制这样做(您每次都拒绝访问).尝试使用其他图片网址(例如Google图片).

The second problem, that you are trying to receive an image from wikipedia without referer and wikipedia restrict to do that (you receiving access denied every time). Try to use another image URL (google image for example).

这篇关于从套接字读取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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