Friday, 13 September 2013

Java. BufferedInputStream working with images

Java. BufferedInputStream working with images

I'm trying to write server and client sides on java. So, client side sends
request like GET / HTTP/1.0, server side responses(if file exists) like
HTTP/1.0 200 OK, put in header content-type and content length and writes
to the BufferedOuputStream the stream from FileInputStream. Server side:
String endLine = "\r\n";
File f = new File(fileName);
FileInputStream fstream;
fstream = new FileInputStream(f);
response = "HTTP/1.0 200 OK" + endLine;
header = "Content-type: "+ contentType + endLine + "Content-length: "
+ f.length() + endLine + endLine;
bout.write(response.getBytes());
bout.write(header.getBytes());
int lol;
while((lol = fstream.read(buffer)) != -1) {
bout.write(buffer,0,lol);
}
System.out.println("Message sent");
bout.flush();
socket.close();
Client side:
byte[] res = new byte[bufferSize];
int got;
int i=0;
int temp = 0;
int j = 0;
while((got = bis.read(res))!=-1){
for(j=0;j<res.length;j++){
//dividing from header
if(res[j]=='\n'&&res[j-1]=='\r'&&res[j-2]=='\n'&&res[j-3]=='\r'){
temp = j+1;
}
}
fout.write(res,temp,got-temp);
i++;
}
So, with .html files it works fine, but with images...

No comments:

Post a Comment