Results 1 to 7 of 7
  1. #1
    MichaelH is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default Socket HTTP-Server

    Hello!

    I would like to download a file from a HTTP-Server. I have the client and with my client I can download for example the index.html file from a server. Now I would like to download a file from my own HTTP-Server which is in the same directory with the HTTP-Server. The problem is I can only download files which are defined in my HTTP-Server class.

    Java Code:
    import java.io.*;
    import java.net.*;
    
    public class HTTPServer {
            
          public static void main(String[] args) throws IOException {
    
             ServerSocket servsocket = new ServerSocket(5050);
             while (true) {
               System.out.println("HTTP-Server started....");
    
             Socket sock = servsock.accept();
             System.out.println("Connection accepted..." + sock);
    
             File data = new File ("index.html");  
             byte[] bytearray = new byte [(int)data.length()];
             FileInputStream inputStream = new FileInputStream(data);
             BufferedInputStream bis = new BufferedInputStream(inputStream);
             bis.read(bytearray,0, bytearray.length);
             OutputStream outputStream = sock.getOutputStream();
             System.out.println("send...");
             outputStream.write(bytearray,0,bytearray.length);
             outputStream.flush();
             sock.close();
         }
       }
     }
    That is the part where you can see which file I would like to download.

    Java Code:
    File data = new File ("index.html");
    Now, I don't want to download only one file. If I have 2 files in the same directory where the HTTP-Server is then I would like to download both files without to change to define the name of the file in

    Java Code:
    File data = new File ("index.html");

    Thanks for your help!

  2. #2
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    I don't understand your question. If you want to send 2 files in the stream, then just write both files. What are you having trouble with?

  3. #3
    MichaelH is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    In my directory are more than two files. If I delete or upload a file in my directory then I have to fix the Java-Code too. That's my problem.

  4. #4
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Oh you want to check if the file exists? Easy: file.exists() returns true if it exists, false otherwise

  5. #5
    MichaelH is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    No. For example:

    In my directory are the files text1, text2, text3, text4 and my HTTP-Server.

    When I start my Client I send a request to my HTTP-Server to get text1 for example.

    The problem is that I have always to fix my HTTP-Server code if I want to get another file from my HTTP-Server.

    If I want to get file text1 then I have to write:

    Java Code:
    File data = new File ("text1");
    If I want to get file text2 then I have to write:

    Java Code:
     File data = new File ("text2");
    and so on.

    There must be an easier way to do this without to fix my HTTP-Server Code.

  6. #6
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Does the file that you are trying to find change every time you run the program? Is there any reason you can't get an input of the file name, check if it exists, if it does then return the file else start over and ask for input again?

    Then your snippet would look like
    Java Code:
    Scanner in = new Scanner(System.in);
    String fileName = in.nextLine();
    File data = new File (fileName);
    It might get a little annoying if you are trying to run the same file more often than other files but unless you make a GUI that gives you the option of selecting the file from a list you might have to make a compromise.
    Last edited by Dark; 05-06-2011 at 03:52 PM.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  7. #7
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

Similar Threads

  1. How to serv HTTP/1.1 404 Not Found from socket?
    By zardos in forum Networking
    Replies: 1
    Last Post: 03-01-2011, 03:06 PM
  2. persistent http web server help!!
    By adel.ejjeh in forum Networking
    Replies: 0
    Last Post: 01-18-2011, 11:29 AM
  3. HTTP requests through proxy server
    By manolowar in forum Networking
    Replies: 3
    Last Post: 01-20-2009, 04:52 AM
  4. Run servlet in HTTP server
    By jithan in forum New To Java
    Replies: 25
    Last Post: 10-16-2008, 10:10 AM
  5. Does any file in an FTP server ends up in an HTTP server?
    By islamfunny in forum CLDC and MIDP
    Replies: 4
    Last Post: 08-15-2008, 04:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •