The Artima Developer Community
Sponsored Link

Java Answers Forum
building a browser with java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
micca

Posts: 1
Nickname: micca
Registered: Apr, 2002

building a browser with java Posted: Apr 1, 2002 8:04 AM
Reply to this message Reply
Advertisement
hi...

im trying to build a little http client with java... right now i have 2 things that give me a headache... what im basically trying to do is to "download" the apache logo -- apache_pb.gif -- from my apache server, which i then want to store in a new GIF-file called "pic.gif"...

here is my code:

import java.io.*;
import java.net.*;

class test
{
private Socket s;
PrintWriter toServer;

public test()
{
try
{
byte[] b;
int i;

File outputFile = new File("pic.gif");
FileOutputStream toFile = new FileOutputStream(outputFile);

s = new Socket ("127.0.0.1",80);

BufferedInputStream fromServer = new BufferedInputStream(s.getInputStream());

toServer = new PrintWriter(s.getOutputStream(), true);
toServer.println("GET /apache_pb.gif HTTP/1.0 \n");

while((i=fromServer.read(b))!=-1)
toFile.write(b);

toFile.close();

}
catch( IOException ie )
{
System.out.println("not connected\n");
}

}

public static void main(String[] args)
{
new test();
}

}

if i open this newly created "pic.gif"-file with NOTEPAD, i get the following content:

HTTP/1.1 200 OK
Date: Mon, 01 Apr 2002 15:07:23 GMT
Server: Apache/1.3.22 (Win32)
Last-Modified: Tue, 02 Jul 1996 22:18:16 GMT
ETag: "0-916-31d9a028"
Accept-Ranges: bytes
Content-Length: 2326
Connection: close
Content-Type: image/gif

GIF89a ? ????????????ssskkkZZZ!?B?1 ?R?????B?? ???? ??????1 ?c ?? ?? ??J??{????? ??J?? c????Rc?
.
.
<rest of picture data>
.

the problem now is, that when i cut off the server-header manually (with notepad), and then save the file to disk and try to open it with MSIE, MSIE will refuse to display it... and this although the original GIF-file (apache-pb.gif) seems to be 100% identical to the copy (pic.gif), when compared visually in notepad, and 100% identical in size when compared in ms explorer... furthermore, if i take a look at the properties of the GIF-file in MSIE, it will tell me that the file-size is -1 Byte and that it is not a GIF-file... obviously i am doing something wrong.... can anybody tell me what my problem is??

my 2nd problem is, is there a nice way how i can cut off the server header?

i would really appreciate it, if somebody could help me...

Topic: Using wrapper classes Previous Topic   Next Topic Topic: Methodical Dilemma

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use