3

我正在尝试通过套接字从服务器下载图像。我的代码工作正常,但是当我下载图像时,尺寸正确但图像无法打开。我不知道我做错了什么。有什么建议吗?谢谢

     Socket socket = new Socket(servername, 80);
     DataOutputStream bw = new DataOutputStream(new DataOutputStream(socket.getOutputStream()));
    bw.writeBytes("GET "+filename+" HTTP/1.1\n");
    bw.writeBytes("Host: "+servername+":80\n\n");

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


    OutputStream dos = new FileOutputStream("testtttt.jpg");
    int count;
    byte[] buffer = new byte[2048];
    while ((count = in.read(buffer)) != -1)
    {
      dos.write(buffer, 0, count);
      dos.flush();
    }
    dos.close();
    System.out.println("image transfer done");

    socket.close();     
   }
4

2 回答 2

2

您需要在所有请求的 \n 之前添加一个 \r,此外,您应该将输出流刷新到套接字。

Socket socket = new Socket(servername, 80);
DataOutputStream bw = new DataOutputStream(socket.getOutputStream());
bw.writeBytes("GET "+filename+" HTTP/1.1\r\n");
bw.writeBytes("Host: "+servername+":80\r\n\r\n");
bw.flush();

此外,您将在请求中获得一些 HTTP 响应标头。显然这是您不想要的信息,您的回复将如下所示:

HTTP/1.1 200 OK
Date: Thu, 14 Nov 2013 18:39:47 GMT
Server: Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7
Accept-Ranges: bytes
ETag: W/"2956-1374616977919"
Last-Modified: Tue, 23 Jul 2013 22:02:57 GMT
Content-Type: image/png;charset=UTF-8
Content-Length: 2956

‰JPG....heres your image data

我刚刚编写了这个方法来摆脱发送的 HTTP 标头。这个想法是在 \r\n\r\n 发生之前不写入任何数据。该序列表示标头响应的结束,并且在此之前的任何数据都不是我们的图像。我知道有一种更干净的方法可以做到这一点,但这种方法对我来说写起来很快:)

OutputStream dos = new FileOutputStream("c:\\testtttt.jpg");
int count;
byte[] buffer = new byte[2048];
boolean eohFound = false;
while ((count = in.read(buffer)) != -1)
{
    if(!eohFound){
        String string = new String(buffer, 0, count);
        int indexOfEOH = string.indexOf("\r\n\r\n");
        if(indexOfEOH != -1) {
            count = count-indexOfEOH-4;
            buffer = string.substring(indexOfEOH+4).getBytes();
            eohFound = true;
        } else {
            count = 0;
        }
    }
  dos.write(buffer, 0, count);
  dos.flush();
}
in.close();
dos.close();

您还可以在这里找到像您这样的另一个问题:通过套接字手动发送 HTTP 请求

于 2013-11-14T18:51:05.290 回答
1

我的声誉不足以评论,所以必须开始一个新的答案。
ug__ 的回答很好,但是这条线

buffer = string.substring(indexOfEOH+4).getBytes();

有一些问题,缓冲区将被破坏。例如,

byte[] before = new byte[]{(byte)0xf1, (byte)0xf2, (byte)0xf3, (byte)0xf4};
String str = new String(before, 0, before.length);
byte[] after = str.getBytes();

before并且after不会相同。

所以我稍微修改了ug__的代码:

OutputStream dos = new FileOutputStream("test.jpg");
int count, offset;
byte[] buffer = new byte[2048];
boolean eohFound = false;
while ((count = in.read(buffer)) != -1)
{
    offset = 0;
    if(!eohFound){
        String string = new String(buffer, 0, count);
        int indexOfEOH = string.indexOf("\r\n\r\n");
        if(indexOfEOH != -1) {
            count = count-indexOfEOH-4;
            offset = indexOfEOH+4;
            eohFound = true;
        } else {
            count = 0;
        }
    }
  dos.write(buffer, offset, count);
  dos.flush();
}
in.close();
dos.close();
于 2015-04-25T06:21:28.690 回答