java socket 保存的图片为什么打不开?问题出在哪?求大神指点
用socket保存一个网页上的验证码图片到本地,打开后显示 无法预览 是为什么啊?代码如下:import *;
import *;
public class HTTPClient{
String host="reg.email.
int port=80;
Socket socket;
public void createSocket()throws Exception{
socket=new Socket(host, port);
}
public void communicate()throws Exception{
StringBuffer sb=new StringBuffer("GET /unireg/call.do?cmd=register.verifyCode&env=369398958183&t=1351924543383 HTTP/1.1\r\n");
sb.append("Host: reg.email.\r\n");
sb.append("Accept: */*\r\n");
sb.append("Accept-Language: zh-cn\r\n");
sb.append("Accept-Encoding: gzip, deflate\r\n");
sb.append("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)\r\n");
sb.append("Connection: Keep-Alive\r\n\r\n");
OutputStream socketOut=socket.getOutputStream();
socketOut.write(sb.toString().getBytes());
socket.shutdownOutput();
InputStream socketIn=socket.getInputStream();
ByteArrayOutputStream buffer=new ByteArrayOutputStream();
byte[] buff=new byte[1024];
int len=-1;
while((len=socketIn.read(buff))!=-1){
buffer.write(buff,0,len);
}
socket.close();
FileOutputStream fos=new FileOutputStream(new File("c:\\1.jpg"));
OutputStreamWriter osw=new OutputStreamWriter(fos);
osw.write((new String(buffer.toByteArray())).split("\r\n\r\n")[1]);
osw.flush();
osw.close();
}
public static void main(String arch[])throws Exception{
HTTPClient client=new HTTPClient();
client.createSocket();
();
}
}
求指点!!!