在 php 页面中接收来自 java DataOutputStream 的输出

发布于 2024-11-25 10:48:46 字数 1728 浏览 2 评论 0原文

我有一个 java 小程序,我用它来将文件发送回我的服务器 - 在服务器端我想在 php 页面上接收该文件。

下面是正在发送的java代码,在php方面我已经检查了全局数组并且我有通过URL传递的数据,但没有文件数据。我确实对此进行了搜索和抓挠,因此感谢任何帮助。

String strURL = sendToURL + "?ACTION=POST&LEN=" + imgBytes.length + "&Fname=picture.png";
    try{
        URL urlServlet = new URL(strURL);
        URLConnection sCon = urlServlet.openConnection();
        sCon.setDoInput(true);
        sCon.setDoOutput(true);
        if (sCon.getAllowUserInteraction()) {
            sCon.setAllowUserInteraction(true);
        }
        sCon.setUseCaches(false);
        sCon.setDefaultUseCaches(false);
        sCon.setRequestProperty("Content-Type", "text/html");
        sCon.setRequestProperty("Connection", "Keep-Alive");
        sCon.setConnectTimeout(transferTimeout);
        sCon.setReadTimeout(transferTimeout);
        DataOutputStream out = new DataOutputStream(sCon.getOutputStream());

        int index = 0;
        size = 1024;
        do {
            if (index + size > imgBytes.length) {
                size = imgBytes.length - index;
            }
            out.write(imgBytes, index, size);
            index += size;
        } while (index < imgBytes.length);

        out.write(imgBytes);
        out.flush();
        out.close();

已解决 - 正如经常发生的那样,经过几天的战斗,一个人发布了一个问题,几分钟后就提出了解决方案。

在关于使用 SOAP 的评论之后我开始思考,我记得以前使用过 cURL 来传输 XML 数据。经过几次搜索后,我发现了一个更简单且非常优雅的解决方案。

http://www.lornajane.net/posts/2008 /Accessing-Incoming-PUT-Data-from-PHP

基本上你可以通过使用访问 php 中的 PUT 数据

file_get_contents("php://input")

,所以现在它工作得非常好

I have a java applet which I am using to send a file back to my server with - on the server end I want to receive this on a php page.

Below is the java code which is doing the sending, on the php side of things I have checked the global arrays and I have the data passed by the URL, but not the file data. I have really searched and scratched on this one so any help appreciated.

String strURL = sendToURL + "?ACTION=POST&LEN=" + imgBytes.length + "&Fname=picture.png";
    try{
        URL urlServlet = new URL(strURL);
        URLConnection sCon = urlServlet.openConnection();
        sCon.setDoInput(true);
        sCon.setDoOutput(true);
        if (sCon.getAllowUserInteraction()) {
            sCon.setAllowUserInteraction(true);
        }
        sCon.setUseCaches(false);
        sCon.setDefaultUseCaches(false);
        sCon.setRequestProperty("Content-Type", "text/html");
        sCon.setRequestProperty("Connection", "Keep-Alive");
        sCon.setConnectTimeout(transferTimeout);
        sCon.setReadTimeout(transferTimeout);
        DataOutputStream out = new DataOutputStream(sCon.getOutputStream());

        int index = 0;
        size = 1024;
        do {
            if (index + size > imgBytes.length) {
                size = imgBytes.length - index;
            }
            out.write(imgBytes, index, size);
            index += size;
        } while (index < imgBytes.length);

        out.write(imgBytes);
        out.flush();
        out.close();

SOLVED - as so often happens one posts a question after days of battling and mere minutes later a solution presents.

I got thinking after the comment about using SOAP that I remembered using cURL for transferring XML data once before. a few searches later and I came across a much simpler and very elegant solution.

http://www.lornajane.net/posts/2008/Accessing-Incoming-PUT-Data-from-PHP

basically you can access the PUT data in php by using

file_get_contents("php://input")

so now it works awesomely

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

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

发布评论

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

评论(1

云归处 2024-12-02 10:48:46

我多次使用 Soap 消息从 PHP 获取数据到 Java,效果很好

所以使用 PHP 作为 Web 服务并通过 SOAP 进行通信

设置 WSDL 文件

生成 Java 存根和框架
http://download.oracle.com/javase/6 /docs/technotes/tools/share/wsimport.html

将 WSDL 加载到 php 脚本中
http://www.php.net/manual/de/book.soap.php

   $soapClient = new SoapClient("blahblah.wsdl"); 

并在 php 中执行逻辑

,然后使用 Java Stubs 调用服务器并传输数据

i used a lot of times Soap Messages to get Data From PHP to Java that works fine

So Use PHP as Webservice and Communicate via SOAP

Setup a WSDL File

Generate Java Stubs and Skeletons
http://download.oracle.com/javase/6/docs/technotes/tools/share/wsimport.html

Load WSDL into a php skript
http://www.php.net/manual/de/book.soap.php

   $soapClient = new SoapClient("blahblah.wsdl"); 

And do your logic in the php

Then use the Java Stubs to call the server and transmit your data

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