从 php 页面中的 java DataOutputStream 接收输出 [英] receive output from java DataOutputStream in a php page
问题描述
我有一个 java 小程序,我用它来将文件发送回我的服务器 - 在服务器端我想在一个 php 页面上接收它.
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 方面,我检查了全局数组,并且我有通过 URL 传递的数据,但不是文件数据.我已经真正搜索过并对此进行了研究,因此感谢任何帮助.
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.
在我记得之前使用 cURL 传输 XML 数据的有关使用 SOAP 的评论后,我开始思考.几次搜索后,我发现了一个更简单、更优雅的解决方案.
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/帖子/2008/Accessing-Incoming-PUT-Data-from-PHP
基本上你可以通过使用
file_get_contents("php://input")
所以现在效果很好
推荐答案
我使用了很多次 Soap Messages 从 PHP 到 Java 的数据,效果很好
i used a lot of times Soap Messages to get Data From PHP to Java that works fine
所以使用 PHP 作为 Webservice 并通过 SOAP 进行通信
So Use PHP as Webservice and Communicate via SOAP
设置 WSDL 文件
生成 Java 存根和骨架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
将 WSDL 加载到 php 脚本中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");
并在 php 中执行您的逻辑
And do your logic in the php
然后使用 Java Stubs 调用服务器并传输您的数据
Then use the Java Stubs to call the server and transmit your data
这篇关于从 php 页面中的 java DataOutputStream 接收输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!