I'd like to know how to transfer files (like sounds, images, ...) from client to server in a easy way ?<br>I know I have to use either CGI, servlet or RMI. But could you give me a hint ? Many thanks. Alex.
If you have all the necessary rights on your server, you dont have to use cgi, servlets or rmi. you can just make a connection. heres an example:<br><br>**** Client side: make sure you import all the right packages...<br><br>/** sends a java object to the reciever and returns whatever object the server sent back. */<br> public Object sendObject(Object o) throws Exception, UnknownHostException, ConnectException {<br> Object objectFromServer = null;<br> socket = new Socket(server, port);<br> ObjectOutputStream objOut = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));<br> objOut.writeObject(o);<br> objOut.flush();<br> <br> ObjectInputStream objIn = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));<br> objectFromServer = objIn.readObject();<br> <br> objIn.close();<br> objOut.close();<br> <br> try { socket.close(); }<br> catch (IOException ioe1) {/*Do nothing*/}<br> <br> <br> return objectFromServer;<br> }<br><br>to send something, call sendObject(new yourObject())<br>remember to make sure that your class is serializable.<br><br>and on the server side:<br><br>import java.net.*;<br>import java.io.*;<br>import java.util.*;<br><br>public class Server {<br> private ServerSocket serverSocket = null; <br> <br> public static void main(String[] args) throws IOException {<br> System.out.println("Initiating Server..."<br> Server s = new Server(); <br> try {<br> s.initServer();<br> } catch (Exception e) { System.out.println(e.toString());}<br> }<br> <br> /** Initializes the server.<br> */<br> public void initServer() throws IOException {<br> try {<br> serverSocket = new ServerSocket(5700); // the port<br> } catch (IOException e) {<br> System.out.println("Another application is listening to port 5700.\n"+e);<br> System.exit(0);<br> }<br> <br> // infinite loop<br> boolean listening = true;<br> while (listening) {<br> Socket s = serverSocket.accept();<br> ServerThread st = new ServerThread(s, this);<br> st.start();<br> }<br> serverSocket.close();<br> }<br>}<br><br>class ServerThread extends Thread {<br> private Socket socket = null;<br> private Server server;<br> <br> /** constructor. */<br> public ServerThread(Socket socket, Server server) {<br> super("ServerThread"<br> this.socket = socket;<br> this.server = server;<br> }<br> <br> /** <br> * Here, I take the socket given to me in the <br> * constructor and I extract the object sent to me from the client.<BR><br> */<br> public void run() {<br> Object objectFromClient = null;<br> try {<br> ObjectInputStream objIn = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));<br> objectFromClient = objIn.readObject();<br><br> if (objectFromClient.getClass().equals(String.class)) {<br> System.out.println(">> String object recieved>>"+objectFromClient.toString());<br> reply("String object recieved by server"<br> }<br> join();<br> } <br> catch (StreamCorruptedException sce) {<br> System.err.println(sce.toString());<br> }<br> catch (Exception e) {<br> e.printStackTrace();<br> }<br> }<br><br> /**<br> * takes the recieved object and sends it to the client.<br> */<br> private void reply(Object obj) {<br> try {<br> ObjectOutputStream objOut = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));<br> objOut.writeObject(obj);<br> objOut.flush();<br> objOut.close();<br> }<br> catch (Exception e) {<br> e.printStackTrace();<br> }<br> }<br>}<br><br><br>Of course you can do it with CGI, RMI or Servlets- if you find this stuff interesting you should give Objectspace Voyager a look, or if you write in several languages, you should try CORBA.<br><br>Good luck<br>Mattias<br>
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.