Hi, I an new to
java.
I am trying to retrieving a file content from a given server and file path.
The file could be a text, html or image file.
I tried reading the content as bytes but the image will not display:
The following is a segment of my code:
byte[] b = new byte[1024];
Socket socket = new Socket(hostname,80);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
out.println("GET /" + file + " HTTP/1.1");
out.println("Host: " + hostname);
out.println();
out.flush();
DataInputStream reader = new DataInputStream(socket.getInputStream());
int i = 10;
File outputfile = new File(outFileName);
FileOutputStream outfile = new FileOutputStream(outputfile);
while ((i = reader.read(b)) != -1) {
outfile.write(b,0, i);
}
I was able to read the contents for text and html but not image file, I do not know why?!. In addition, my output file will contain the http response code which I want to omit but could not
as I am reading everything as bytes, I would not know the particular position the http response code ends to decide which to write and what item not to write.
Can anyone help with my 2 questions?