将我的小程序链接到服务器目录以从那里接收或保存文件?

发布于 2024-08-20 20:34:44 字数 59 浏览 1 评论 0原文

我正在寻找一个代码来保存在小程序中创建的文件,通常是文本文件,我想将它们保存在服务器目录上,我该怎么做。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原谅过去的我 2024-08-27 20:34:44

以下是如何发送字符串的示例。事实上,任何对象都可以发送到此方法,只要它是可序列化的并且同一版本的对象存在于applet 和servlet 上。

从小程序发送

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

在服务器端检索

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

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);
        }
   }
尤怨 2024-08-27 20:34:44

没有人会把这个放在盘子里递给你。您必须在小程序中编写代码才能与服务器建立套接字连接并发送数据。解决此问题的一种方法是通过 HTTP 推送数据,并使用诸如 commons- 之类的库httpclient。这需要您的服务器处理适当的 HTTP 动词。

还有许多其他选择,正确的选择取决于您要解决的问题的细节。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文