Results 1 to 7 of 7
Thread: Socket HTTP-Server
- 05-03-2011, 07:25 PM #1
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
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.
That is the part where you can see which file I would like to download.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(); } } }
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 inJava Code:File data = new File ("index.html");
Java Code:File data = new File ("index.html");
Thanks for your help!
- 05-03-2011, 10:05 PM #2
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?
- 05-03-2011, 10:23 PM #3
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
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.
- 05-03-2011, 10:32 PM #4
Oh you want to check if the file exists? Easy: file.exists() returns true if it exists, false otherwise
- 05-04-2011, 09:58 AM #5
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
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:
If I want to get file text2 then I have to write:Java Code:File data = new File ("text1");
and so on.Java Code:File data = new File ("text2");
There must be an easier way to do this without to fix my HTTP-Server Code.
- 05-06-2011, 03:46 PM #6
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
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.Java Code:Scanner in = new Scanner(System.in); String fileName = in.nextLine(); File data = new File (fileName);
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.
- 05-06-2011, 08:45 PM #7
Similar Threads
-
How to serv HTTP/1.1 404 Not Found from socket?
By zardos in forum NetworkingReplies: 1Last Post: 03-01-2011, 03:06 PM -
persistent http web server help!!
By adel.ejjeh in forum NetworkingReplies: 0Last Post: 01-18-2011, 11:29 AM -
HTTP requests through proxy server
By manolowar in forum NetworkingReplies: 3Last Post: 01-20-2009, 04:52 AM -
Run servlet in HTTP server
By jithan in forum New To JavaReplies: 25Last Post: 10-16-2008, 10:10 AM -
Does any file in an FTP server ends up in an HTTP server?
By islamfunny in forum CLDC and MIDPReplies: 4Last Post: 08-15-2008, 04:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks