我有一个用Java编写的客户端程序。我需要创建一个Socket,通过HTTP 1.1请求图像URL并接收图像,然后将其写入磁盘。
但是,我无法通过Socket部分实现接收图像。
这是我的代码:
public static BufferedImage HTTP11GETImage()
{
String response = "";
BufferedImage image = null;
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
int imageLenght;
try
{
echoSocket = new Socket("oracleimg.com", 80);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
out.println("GET /us/assets/oralogo-small.gif HTTP/1.1");
out.println("HOST: oracleimg.com");
out.println("");
//Code to recieve Image over Socket with HTTP protocol
}
catch (Exception e)
{
System.exit(1);
}
try
{
out.close();
in.close();
echoSocket.close();
out = null;
in = null;
echoSocket = null;
}
catch (Exception ex)
{
}
return image;
}
我尝试了很多东西,但所有这些都没有成功......
一件重要的事情是我被限制使用java的URL& HTTP库。我应该手动完成......
此外,该方法也可以返回任何其他类...
如何使用HTTP协议从Socket获取图像?