开发者

linking my applet to a server dirctory to recieve or save a file from there?

开发者 https://www.devze.com 2022-12-19 20:01 出处:网络
I\' m looking for a code to save the files created in a applet normall开发者_如何转开发y text files i want to save them on a server directory how can i do so.Here is an example of how to send a String

I' m looking for a code to save the files created in a applet normall开发者_如何转开发y text files i want to save them on a server directory how can i do so.


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);
        }
   }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号