I' m looking for a code to save the files created in a applet normally text files i want to save them on a server directory how can i do so.
views:
13answers:
2
A:
No one is going to hand you this on a plate. You have to write code in your applet to make a socket connection back to your server and send the data. One way to approach this is to push the data via HTTP, and use a library such as commons-httpclient. That requires your server to handle the appropriate HTTP verb.
There are many other options, and the right one will depend on the fine details of the problem you are trying to solve.
bmargulies
2010-02-12 13:59:07
+1
A:
Here is an example of how to send a String
. In fact any Object
can be sent this method so long as it's serializable and the same version of the Object exists on both the applet and the servlet.
To send from the applet
public void sendSomeString(String someString) { ObjectOutputStream request = null; try { URL servletURL = new URL(getCodeBase().getProtocol(), getCodeBase().getHost(), getCodeBase().getPort(), "/servletName"); // open the connection URLConnection con = servletURL.openConnection(); con.setDoOutput(true); con.setUseCaches(false); con.setRequestProperty("Content-Type", "application/octet-stream"); // send the data request = new ObjectOutputStream( new BufferedOutputStream(con.getOutputStream())); request.writeObject(someString); request.flush(); // performs the connection new ObjectInputStream(new BufferedInputStream(con.getInputStream())); } catch (Exception e) { System.err.println("" + e); } finally { if (request != null) { try { request.close(); } catch (Exception e) { System.err.println("" + e); }; } } }
To retrieve on the server side
@Override public void doPost(HttpServletRequest request, HttpServletResponse response) { try { // get the input stream ObjectInputStream inputStream = new ObjectInputStream( new BufferedInputStream(request.getInputStream())); String someString = (String)inputStream.readObject(); ObjectOutputStream oos = new ObjectOutputStream( new BufferedOutputStream(response.getOutputStream())); oos.flush(); // handle someString.... } catch (SocketException e) { // ignored, occurs when connection is terminated } catch (IOException e) { // ignored, occurs when connection is terminated } catch (Exception e) { log.error("Exception", e); } }
Pool
2010-02-14 16:16:14