简体   繁体   English

在 php 页面中从 java DataOutputStream 接收 output

[英]receive output from java DataOutputStream in a php page

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.我有一个 java 小程序,我用它来将文件发送回我的服务器,在服务器端我想在 php 页面上接收它。

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. 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.在关于使用 SOAP 的评论后,我想到了使用 cURL 传输 XML 数据一次。 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 http://www.lornajane.net/posts/2008/Accessing-Incoming-PUT-Data-from-PHP

basically you can access the PUT data in php by using基本上你可以使用 php 访问 PUT 数据

file_get_contents("php://input")

so now it works awesomely所以现在它工作得非常好

i used a lot of times Soap Messages to get Data From PHP to Java that works fine我多次使用 Soap 消息从 PHP 到 Java 获取数据,效果很好

So Use PHP as Webservice and Communicate via SOAP所以使用 PHP 作为 Web 服务并通过 SOAP 进行通信

Setup a WSDL File设置 WSDL 文件

Generate Java Stubs and Skeletons http://download.oracle.com/javase/6/docs/technotes/tools/share/wsimport.html 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 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并在 php 中执行您的逻辑

Then use the Java Stubs to call the server and transmit your data然后使用 Java Stubs 调用服务器并传输您的数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM