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