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.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here is an example of how to send a
String. In fact anyObjectcan 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); } }