Java:将文件(XML)从网络服务器发送到另一台服务器

发布于 2024-11-16 10:16:13 字数 1220 浏览 3 评论 0原文

我有一个简单的问题,关于使用 Java(struts2 框架)将文件(XML 文件)从我的 web 应用程序服务器发送到另一台服务器

希望有人可以看一下我的代码,因为我不可能检查代码是否有效 - 另一台服务器(必须接收文件)仍未实施。我必须尽可能正确地准备我的 web 应用程序服务器来发送文件。

我有一个 XML 文件路径,以及由 spring 框架填充的服务器地址和端口。

查看互联网上的一些示例以及这个很棒的网站中的其他一些问题,我尝试编写一个简单的代码将我的文件发送到给定的地址。 这是代码:

private String server;
private Integer port;

// getters and settlers methods for server and port properties

public void sendXML(String fileName) throws Exception{
    try{
        Socket socket = new Socket(server, port);

        File file = new File(fileName);

        FileInputStream fis = new FileInputStream(file);

        OutputStream os = socket.getOutputStream();

        byte [] bytearray  = new byte [(int)file.length()];
        BufferedInputStream bis = new BufferedInputStream(fis);
        bis.read(bytearray,0,bytearray.length);
        os.write(bytearray,0,bytearray.length);
        os.flush();
        socket.close();

    }
    catch(IOException e){
        e.printStackTrace();
    }

}

因此,如果有人可以查看我的代码并告诉我您是否认为它不起作用,我将非常感激。如果您认为还有其他更好的方法可以做到这一点,我也将不胜感激。

谢谢你们,你们总是非常非常乐于助人;)

问候,

Aleix

I have a simple question about sending a file (XML file) from my webapp server to another server with Java (struts2 framework).

I hope someone can give a look to my code, because it is impossible for me to check if the code will work - the other server (the one that have to receive the file) is still not implemented. And I have to prepare my webapp server the most correct possible to send the file.

I have an XML file path, and the server address and the port its filled by the spring framework.

Looking at some examples in internet and also in some other questions in this awesome site, I have tried to write a simple code to send my file to the given address. This is the code:

private String server;
private Integer port;

// getters and settlers methods for server and port properties

public void sendXML(String fileName) throws Exception{
    try{
        Socket socket = new Socket(server, port);

        File file = new File(fileName);

        FileInputStream fis = new FileInputStream(file);

        OutputStream os = socket.getOutputStream();

        byte [] bytearray  = new byte [(int)file.length()];
        BufferedInputStream bis = new BufferedInputStream(fis);
        bis.read(bytearray,0,bytearray.length);
        os.write(bytearray,0,bytearray.length);
        os.flush();
        socket.close();

    }
    catch(IOException e){
        e.printStackTrace();
    }

}

So, I will be very grateful if someone can give a look to my code and tell me if you think that it will not work. If you think that there is another better way to do it I also would be grateful to know it.

Thank you people, you are always really really helpful ;)

Regards,

Aleix

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

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

发布评论

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

评论(2

天生の放荡 2024-11-23 10:16:13

我建议您使用 HTTP 而不是原始套接字。它将处理超时、分块、编码等问题。

查看 commons http 库(以前称为http-client),它将节省您编写自己的代码。

I suggest you use HTTP rather than raw sockets. It will deal with timeouts, chunking, encoding, etc.

Have a look at the commons http library (formerly known as http-client), it will save you writing your own code.

风柔一江水 2024-11-23 10:16:13

我已经了解了如何使用 Apache HttpClient4 和 HttpCore4 库通过 HTTP 来完成此操作,并且我已经编写了这段代码,您认为它可以正常工作吗?非常感谢!

private String server;
//private Integer port;

// getter and settler methods for server property

public void sendXML(String fileName) throws Exception{
    try{
        File file = new File(fileName);
        FileEntity entity = new FileEntity(file, "text/xml; charset=\"UTF-8\"");
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost method = new HttpPost(server);
        method.setEntity(entity);
        HttpResponse response = httpclient.execute(method);
    }
    catch(IOException e){
        e.printStackTrace();
    }
}

I have look how to do it trough HTTP with the Apache HttpClient4 and HttpCore4 libraries and I have wrote this code, you think it would work properly? Thank you very much!

private String server;
//private Integer port;

// getter and settler methods for server property

public void sendXML(String fileName) throws Exception{
    try{
        File file = new File(fileName);
        FileEntity entity = new FileEntity(file, "text/xml; charset=\"UTF-8\"");
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost method = new HttpPost(server);
        method.setEntity(entity);
        HttpResponse response = httpclient.execute(method);
    }
    catch(IOException e){
        e.printStackTrace();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文